HarmonyOS 生成非对称密钥报错 Error: convert key fail.
下面这段生成公钥的代码publicData应该传什么?传错了之后从提示看不出来原因Error: convert key fail.
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { buffer, util } from '@kit.ArkTS';
export class CECryptoUtil {
public static stringToUint8Array(content: string): Uint8Array {
let textEncoder = new util.TextEncoder();
let buffer = new ArrayBuffer(10000);
let result = new Uint8Array(buffer);
result = textEncoder.encodeInto(content);
console.info("buffer is ", result)
return result
}
static async rsaEncrypt(publicKey: string, content: string) {
let publicData = CECryptoUtil.stringToUint8Array(publicKey)
// let keyPair = await CECryptoUtil.genKeyPublicByData(publicData);
let keyPair = await CECryptoUtil.genKeyPublicByData(new Uint8Array([48, 129, 159, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 3, 129, 141, 0, 48, 129, 137, 2, 129, 129, 0, 197, 64, 10, 198, 14, 110, 65, 92, 206, 35, 28, 123, 153, 24, 134, 255, 145, 74, 42, 173, 40, 215, 146, 58, 143, 46, 10, 195, 154, 160, 69, 196, 220, 152, 179, 44, 111, 200, 84, 78, 215, 73, 210, 181, 12, 29, 70, 68, 36, 135, 153, 89, 230, 202, 130, 212, 111, 243, 234, 92, 131, 62, 145, 50, 73, 48, 104, 245, 46, 70, 45, 157, xxx, 143, 140, 162, xxxx, 216xxx 220, 49, 121, 142, 194, 33, 223, 201, 0, 16, 163, 210, 240, 118, 92, 147, 121, 220, 17, 114, 24, 52, 125, 135, 176, 88, 21, 83, 86, 17, 156, 88, 250, 48, 79, 86, 128, 248, 105, 208, 133, 140, 13, 153, 164, 191, 136, 164, 44, 53, 2, 3, 1, 0, 1]));
console.info("keyPair.pubKey.format is ", keyPair.pubKey.format)
let cipher = cryptoFramework.createCipher('RSA1024|PKCS1');
await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, keyPair.pubKey, null);
let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(content, 'utf-8').buffer) };
let encryptData = await cipher.doFinal(plainText);
let base64 = new util.Base64Helper();
let encodeString = base64.encodeToStringSync(encryptData.data)
return encodeString;
}
static async genKeyPublicByData(pubKeyData: Uint8Array) {
let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKeyData };
let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024');
try {
let keyPair = await rsaGenerator.convertKey(pubKeyBlob, null);
return keyPair;
}
catch (err) {
console.info("convertKey ", err)
}
let keyPair = await rsaGenerator.convertKey(pubKeyBlob, null);
return keyPair
}
}
HarmonyOS
赞
收藏 0
回答 1
待解决
相关问题
#鸿蒙学习大百科#对称密钥和非对称密钥的区别是什么?
328浏览 • 0回复 待解决
实现一次非对称RSA非对称加解密
1100浏览 • 1回复 待解决
使用AES对称密钥(CBC模式)加解密,生成symKey失败
304浏览 • 1回复 待解决
HarmonyOS 生成SM2密钥对报错
79浏览 • 1回复 待解决
移动端如何生成密钥进行对称加密,和服务器交互?
241浏览 • 1回复 待解决
3DES如何根据指定KEY、IV生成SymKey对称秘钥?
659浏览 • 1回复 待解决
是否有支持非对称加密的三方库?
1059浏览 • 1回复 待解决
RSA非对称加密-@hms-security/agoh-crypto
552浏览 • 1回复 待解决
HarmonyOS 使用 cryptoFramework.createCipher 创建RSA非对称加密问题
0浏览 • 1回复 待解决
HarmonyOS SM2非对称加密生成ASN.1密文转C1C3C2标准密文出错
12浏览 • 1回复 待解决
[Fail]ExecuteCommand need connect-key?
9914浏览 • 2回复 待解决
ArkTS生成密钥问题有哪些?
359浏览 • 1回复 待解决
HarmonyOS 使用3DES对称密钥如何进行 CBC模式加解密
8浏览 • 1回复 待解决
密钥库生成sm2密钥,密钥对不能同时支持签名和加解密
277浏览 • 1回复 待解决
HUKS用户认证通过PIN生成密钥
1044浏览 • 1回复 待解决
使用hdc命令提示[Fail]ExecuteCommand need connect-key
43567浏览 • 8回复 待解决
HarmonyOS 在使用hmac时其中生成对称密钥时未指定hmac而是用的aes,请问这样存在什么安全问题呢?
7浏览 • 1回复 待解决
cryptoFramework中对于对称密钥算法只支持3DES,不在支持DES算法吗?
809浏览 • 1回复 待解决
HarmonyOS SM2 密钥生成怎么和其他平台对齐
12浏览 • 1回复 待解决
密钥库huks生成的sm2密钥场景下,获取签名所需userid值的方法。
806浏览 • 1回复 待解决
获取token失败,报错信息 get token fail
839浏览 • 1回复 待解决
编译报错hvigor ERROR: Error, Schema validate failed
1456浏览 • 1回复 待解决
编译报错:hvigor ERROR: Error, Schema validate failed.
2719浏览 • 1回复 待解决
公钥应满足ASN.1语法、X.509规范、DER编码格式,私钥应满足ASN.1语法、PKCS#8规范、DER编码格式。https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-cryptoframework-V5#convertkey-3