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
2024-08-13 14:56:59
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple

公钥应满足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

async function rsa(){ 
  let Base64 = new util.Base64Helper() 
  let keyGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024') 
  let a = Base64.decodeSync("MIIBIjANBgkqhkiG9w0BAQEFAAOxxxxIBCgKCAQEApMnEp7TS/Fv+/TY5KfKie3g3yAd2pzHrwVSn2kDzzcMhUHYkpqlLX1DhO7sYXFaa/N+tKZPpFRYY7KaToRkxoOCU7Urd7gMYMZwvWoC6UIPyR+xxxrieGFDC966x3D+xxx+tOU4rT1CgDn6cWDdjcX6zeyt3VkENrgkCc4YoEx5ME8pC4HMcvG43O5PDXaO8vyjDfOBbwm/u/0ZgBZrawsArkJ8GZZWnwheMjuZCZwFk8WfrQgRzkbsqfPHQxiIwqmf16QN6uArvpk3Ka+nwU3ur/fDo6/iUxxxxxxxxxxxxxxxXpyvGJnwF/OUcaMaSbCmdOJFQIDAQAB") 
  let keyPair = await keyGenerator.convertKey({ data:a },null) 
  let cipher = cryptoFramework.createCipher('RSA|PKCS1') 
  await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, keyPair.pubKey, null) 
  let inputBlob: cryptoFramework.DataBlob = {data: stringToUint8Array("加密的信息")} 
  let blob = await cipher.doFinal(inputBlob) 
  return Base64.encodeToString(blob.data) 
} 
export function stringToUint8Array(str: string) { 
  return new Uint8Array(buffer.from(str,'utf-8').buffer); 
}
分享
微博
QQ
微信
回复
2024-08-13 21:43:08
相关问题
实现一次非对称RSA非对称加解密
1100浏览 • 1回复 待解决
HarmonyOS 生成SM2密钥报错
79浏览 • 1回复 待解决
是否有支持非对称加密的三方库?
1059浏览 • 1回复 待解决
RSA非对称加密-@hms-security/agoh-crypto
552浏览 • 1回复 待解决
[Fail]ExecuteCommand need connect-key?
9914浏览 • 2回复 待解决
ArkTS生成密钥问题有哪些?
359浏览 • 1回复 待解决
HUKS用户认证通过PIN生成密钥
1044浏览 • 1回复 待解决
获取token失败,报错信息 get token fail
839浏览 • 1回复 待解决