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
待解决
相关问题
实现一次非对称RSA非对称加解密
706浏览 • 1回复 待解决
3DES如何根据指定KEY、IV生成SymKey对称秘钥?
364浏览 • 1回复 待解决
是否有支持非对称加密的三方库?
735浏览 • 1回复 待解决
RSA非对称加密-@hms-security/agoh-crypto
77浏览 • 1回复 待解决
[Fail]ExecuteCommand need connect-key?
8860浏览 • 2回复 待解决
ArkTS生成密钥问题有哪些?
78浏览 • 1回复 待解决
HUKS用户认证通过PIN生成密钥
672浏览 • 1回复 待解决
cryptoFramework中对于对称密钥算法只支持3DES,不在支持DES算法吗?
533浏览 • 1回复 待解决
密钥库huks生成的sm2密钥场景下,获取签名所需userid值的方法。
446浏览 • 1回复 待解决
使用hdc命令提示[Fail]ExecuteCommand need connect-key
37936浏览 • 7回复 待解决
HarmonyOS 4.0 hdc使用hdc list targets 报empty,hdc install提示[Fail]ExecuteCommand need connect-key?
6165浏览 • 1回复 待解决
HarmonyOS获取UDID 提示fail
9218浏览 • 1回复 待解决
获取token失败,报错信息 get token fail
265浏览 • 1回复 待解决
HarmonyOS request.uploadFile 报错 empty error message
83浏览 • 1回复 待解决
根据SM2密钥二进制数据,生成密钥对,公钥支持什么格式,pem和der都支持吗?
463浏览 • 1回复 待解决
编译报错hvigor ERROR: Error, Schema validate failed
847浏览 • 1回复 待解决
编译报错:hvigor ERROR: Error, Schema validate failed.
1262浏览 • 1回复 待解决
报错:Error while Deploying HAP。
11772浏览 • 2回复 待解决
HarmonyOS 使用Preferences时报错崩溃 Error code 15500000
240浏览 • 1回复 待解决
冷启动报错Error message
1691浏览 • 1回复 待解决
运行HarmonyOS工程持续报错error: ACCESS_ERROR, code: 109.怎么回事啊?
3987浏览 • 2回复 待解决
公钥应满足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