rsa 公钥解密初始化报错

使用rsa的公钥解密初始化报错401,具体代码如下:

const RSA512_PKCS1: string = 'RSA512|PKCS1'; 
const RSA_BODY_KEY:string='MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCykN5sbKMPPc3S3YpuU9qbO9FFnk/lBliWEfBoccvk7oAt1KEKrqCyGRhuXi9UeUZmMVDbormv7InPy+oVgteJyQ6smHdmmDJuDl9VB0GbVouqTTYJVE/myK/dmDzySf6FUfzNROWeEIBWCGdnI7xI6ja1Ed9+scfFF+O7RQqn0QIDAQAB' 
 
let datainfo='RIeGeqVUFztbyS848YwmhJIhW4V2fKeX8QyiqQQiwprb6jSWLa7wACUqJQCB+qH00QQFLISiailyLI3yK5K/cIt6IBTumvUb9GdP8K8uqqPCSCU4tgPUOEWznC4F5KU8Ft5sHALuiXWvEUpMRwREIPS8dVPEy6YEzdHdqC65LSM=' 
 
let cipherModel: CipherModel = new CipherModel(); 
let infoStr=await cipherModel.rsaDecryptByPublic(datainfo,RSA_BODY_KEY) 
rsaDecryptByPublic(message: string,publicKey:string):Promise<string> { 
  let rsaGenerator = cryptoFramework.createAsyKeyGenerator(RSA512_PRIMES_2); 
  let cipher = cryptoFramework.createCipher(RSA512_PKCS1); 
  let that = new util.Base64Helper(); 
  let pubKey = that.decodeSync(publicKey); 
  let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKey }; 
  let promiseResult=new Promise<string>((resolve: Function, reject: Function)=>{ 
    rsaGenerator.convertKey(pubKeyBlob, null, (err, keyPair) => { 
      if (err) { 
        Logger.error("convertKey: error." + (err as BusinessError).code); 
        reject(err); 
        return 
      } 
      cipher.init(cryptoFramework.CryptoMode.DECRYPT_MODE, keyPair.pubKey, null, async (err, data) => { 
        try { 
          let newMessage = that.decodeSync(message); 
          let cipherText: cryptoFramework.DataBlob = { data: newMessage }; 
          let cipherTextSplitLen = 128; // RSA密钥每次加密生成的密文字节长度计算方式:密钥位数/8 
          let decryptText = new Uint8Array(); 
          for (let i = 0; i < cipherText.data.length; i += cipherTextSplitLen) { 
            let updateMessage = cipherText.data.subarray(i, i + cipherTextSplitLen); 
            let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage }; 
            // 将密文按128字节进行拆分解密,得到原文后进行拼接 
            let updateOutput = await cipher.doFinal(updateMessageBlob); 
            let mergeText = new Uint8Array(decryptText.length + updateOutput.data.length); 
            mergeText.set(decryptText); 
            mergeText.set(updateOutput.data, decryptText.length); 
            decryptText = mergeText; 
          } 
          let result = this.uint8ArrayToString(decryptText); 
          resolve(result) 
        } catch (err) { 
          Logger.info(TAG, "cipher init error: " + (err as BusinessError).code); 
          return err; 
        } 
      }) 
    }) 
  }); 
  return promiseResult 
} 
HarmonyOS
2024-11-11 09:22:19
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

​参考文档:​https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/crypto-rsa-asym-encrypt-decrypt-pkcs1-0000001774120466-V5

相关文档参考RSA的签名和签名恢复:​https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/crypto-sign-sig-verify-overview-0000001821000089-V5#ZH-CN_TOPIC_0000001881178025__%E7%AD%BE%E5%90%8D%E6%A8%A1%E5%BC%8F%E4%B8%BAonlysign

以下是公钥解密的demo:​

//私钥加密 
async function signMessagePromise(priKey: cryptoFramework.PriKey) { 
  let signAlg = "RSA1024|PKCS1|NoHash|OnlySign"; 
  let signer = cryptoFramework.createSign(signAlg); 
  await signer.init(priKey); 
  let signData = await signer.sign({data:stringToUint8Array("私钥加密")}); 
  return signData; } 
// 公钥解密 
async function verifyMessagePromise(signMessageBlob: cryptoFramework.DataBlob, pubKey: cryptoFramework.PubKey) { 
  let verifyAlg = "RSA1024|PKCS1|NoHash|Recover"; 
  let verifier = cryptoFramework.createVerify(verifyAlg); 
  await verifier.init(pubKey); 
  let rawSignData = await verifier.recover(signMessageBlob); 
  console.log("rsa puk data " + uint8ArrayToString(rawSignData?.data)) return rawSignData; } 
// 字节流转成可理解的字符串 
export function uint8ArrayToString(array:Uint8Array) { 
  // 将UTF-8编码转换成Unicode编码 let out: string = ""; 
  let index: number = 0; 
  let len: number = array.length; 
  while (index < len) { 
    let character = array[index++]; 
    switch(character >> 4) { 
      case 0: 
      case 1: 
      case 2: 
      case 3: 
      case 4: 
      case 5: 
      case 6: 
      case 7: 
        out += String.fromCharCode(character); 
        break; 
      case 12: 
      case 13: 
        out += String.fromCharCode(((character & 0x1F) << 6) | (array[index++] & 0x3F)); 
        break; 
      case 14: out += String.fromCharCode(((character & 0x0F) << 12) | ((array[index++] & 0x3F) << 6) | ((array[index++] & 0x3F) << 0)); 
        break; 
      default: break; 
    } 
  } 
  return out; 
} 
// 字符串转成字节流 
export function stringToUint8Array(str: string) { 
  return new Uint8Array(buffer.from(str,'utf-8').buffer); 
}
分享
微博
QQ
微信
回复
2024-11-11 17:33:07
相关问题
HarmonyOS RSA如何用解密
593浏览 • 0回复 待解决
HarmonyOS 支持rsa使用解密
457浏览 • 1回复 待解决
如何使用RSA加密明文数据
433浏览 • 1回复 待解决
如何实现RSAPK加密一段文字
592浏览 • 1回复 待解决
HarmonyOS Component初始化问题
395浏览 • 1回复 待解决
没法调试“已初始化”分支
185浏览 • 1回复 待解决
HarmonyOS 应用RDB初始化示例
393浏览 • 1回复 待解决
初始化安全相机的方法
384浏览 • 1回复 待解决
关于初始化EGL环境相关问题
628浏览 • 1回复 待解决
RSA加密,使用自有私钥解密报错
207浏览 • 1回复 待解决
是否提供提前初始化webview的能力
2285浏览 • 1回复 待解决
如何初始化OceanBase服务器环境?
3665浏览 • 1回复 待解决
HarmonyOS泛型类的属性如何初始化
436浏览 • 1回复 待解决