HarmonyOS 鸿蒙Next SM2密钥问题
HarmonyOS 鸿蒙Next SM2密钥问题
let keyGenAlg = "SM2_256";
let generator = cryptoFramework.createAsyKeyGenerator(keyGenAlg);
let keyPairTest = await generator.generateKeyPair();
console.log('公钥',keyPairTest.pubKey.getEncoded().data)
console.log('公钥原始',JSON.stringify(keyPairTest.pubKey))
console.log('私钥',keyPairTest.priKey.getEncoded().data)
根据以上代码生成了密钥进行了查看,按道理SM2 256意味着密钥长度为256也就是32个字节
那为什么以上方法生成的公钥是91,私钥是51呢。还是说识别方法不对?
当传入32位的密钥以
let keyPair = generator.convertKeySync(null, privateKeyBlob);
的方式去获取密钥时又为什么会报错呢
更多关于HarmonyOS 鸿蒙Next SM2密钥问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
参考sm2-256 私钥是64位的demo:
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { buffer, util } from '@kit.ArkTS';
@Entry
@Component
export struct SM2256 {
aboutToAppear(): void {
}
// 加密消息
async encryptMessagePromise(publicKey: cryptoFramework.PubKey, plainText: cryptoFramework.DataBlob) {
let cipher = cryptoFramework.createCipher('SM2_256|SM3');
await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, publicKey, null);
let encryptData = await cipher.doFinal(plainText);
return encryptData;
}
// 解密消息
async decryptMessagePromise(privateKey: cryptoFramework.PriKey, cipherText: cryptoFramework.DataBlob) {
let decoder = cryptoFramework.createCipher('SM2_256|SM3');
await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, privateKey, null);
let decryptData = await decoder.doFinal(cipherText);
return decryptData;
}
async convertStrToPubKey(keyStr: string): Promise<cryptoFramework.PubKey> {
const pubKeyStr = keyStr.startsWith('04') ? keyStr.slice(2) : keyStr
const pkPart1 = pubKeyStr.slice(0, pubKeyStr.length / 2)
const pkPart2 = pubKeyStr.slice(pubKeyStr.length / 2)
const pk: cryptoFramework.Point = {
x: BigInt('0x' + pkPart1),
y: BigInt('0x' + pkPart2),
}
const pubKeySpec: cryptoFramework.ECCPubKeySpec = {
params: cryptoFramework.ECCKeyUtil.genECCCommonParamsSpec('NID_sm2'),
pk: pk,
algName: 'SM2',
specType: cryptoFramework.AsyKeySpecType.PUBLIC_KEY_SPEC
}
const keypairGenerator = cryptoFramework.createAsyKeyGeneratorBySpec(pubKeySpec)
return await keypairGenerator.generatePubKey()
}
async convertStrToPriKey(keyStr: string): Promise<cryptoFramework.PriKey> {
const sk = BigInt('0x' + keyStr)
const priKeySpec: cryptoFramework.ECCPriKeySpec = {
params: cryptoFramework.ECCKeyUtil.genECCCommonParamsSpec('NID_sm2'),
sk: sk,
algName: 'SM2',
specType: cryptoFramework.AsyKeySpecType.PRIVATE_KEY_SPEC
}
const keypairGenerator = cryptoFramework.createAsyKeyGeneratorBySpec(priKeySpec)
return await keypairGenerator.generatePriKey()
}
build() {
Row() {
Button("createAsyKeyGenerator").onClick(async () => {
let generator = cryptoFramework.createAsyKeyGenerator(""SM2_256"");
let keyPair = generator.generateKeyPairSync();
let priKeyData = keyPair.priKey.getEncoded().data
let pubKeyData = keyPair.pubKey.getEncoded().data
let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("123", 'utf-8').buffer) };
let encryptText = await this.encryptMessagePromise(keyPair.pubKey, plainText)
let decryptText = await this.decryptMessagePromise(keyPair.priKey, encryptText)
})
Button("网站生成的测试").onClick(async () => {
// 64位
let priKey = "75085381c66ac32b45299e2e020e86ba76a0c5ec495d050d2816eba7bd5e6ce9"
// 128 位
let pubKey = "04a1ed2cb2f678994badca91af895b29db610771f001ab6f412fcd50d971a93cd7a2179b56ffb9c264919be36ad603c223bedc16ceb16f0dfbcd869c5b15ecf0c4"
let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("123", 'utf-8').buffer) };
const pk = await this.convertStrToPubKey(pubKey)
const sk = await this.convertStrToPriKey(priKey)
let encryptText = await this.encryptMessagePromise(pk, plainText)
console.log('123加密后的信息'+JSON.stringify(encryptText))
let decryptText = await this.decryptMessagePromise(sk, encryptText)
console.log('123解密后的信息'+JSON.stringify(decryptText))
let messageDecrypted = buffer.from(decryptText.data).toString('utf-8');
console.log(messageDecrypted)
})
}
}
更多关于HarmonyOS 鸿蒙Next SM2密钥问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
失败
- 状态: 复制
- 错误代码: 深色代码主题
- 描述: 生成失败
针对HarmonyOS(鸿蒙)Next SM2密钥问题,以下是专业且直接的回答:
HarmonyOS Next在密钥管理方面,对于SM2算法的支持是系统安全性的重要一环。SM2是一种基于椭圆曲线公钥密码体制的国家标准算法,广泛应用于数字签名、密钥协商和加密等领域。
若你在使用HarmonyOS Next时遇到SM2密钥问题,可能涉及密钥生成、导入、导出或使用过程中的错误。首先,请确保你的系统环境、库文件及依赖项均已更新至最新版本,以支持SM2算法的所有特性。
其次,检查你的代码逻辑,确保密钥的生成、存储、加载及使用过程符合SM2算法的标准规范。例如,密钥的格式、长度以及加密解密过程中的参数设置等均需正确无误。
此外,若你使用的是第三方库或框架来处理SM2密钥,请确认该库或框架与HarmonyOS Next的兼容性,并查阅相关文档以了解是否有已知的兼容性问题或限制。
如果问题依旧没法解决,请联系官网客服。官网地址是:https://www.itying.com/category-93-b0.html ,他们将为你提供更专业的技术支持和解决方案。