HarmonyOS RSA私钥加密公钥解密

无法解密成功,下面是方法,包含待解密串和公钥,公钥应该是2048的,麻烦帮忙正确解密出结果

rsaDecrypt(message: string, callback) {
  message = "aTc2-LTeM7Dsxo-2fuHeoC-k0pGU89jKm6xghw-gGllwyRz-q5ybwBc6wjIazfj14_3aWW1GzuG3mW6Qxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx9CHMqw9yjtYxAOwh-C_IVtt9euYfBzFqJTbgJgSWjHzFSUXG9za5ZyW8qt4YPTtXzCSu-uTUfpoxDoMGqS23rQTkiRACDGBVOC66jhTKMOgRfOcCXWqP6FuC25eRohHVMBc3r3AU_e5d-ObvbI_IaZGU4M6RV_Ptx0aQi3nA9rf94HlbouaAxy9xofHyIzXWt8vtAGt6PsEgAugikA";
  const PUBLICKEY_DEBUG = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiOpWXDGVxiq2ON0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxMbyrKNxhD840oMThOmFdLUsK8Aq8_akTzNQFg3bxiaf4vQxnDwyVWjeZD-7rFD4-NeX7pUPgxKpyHDUe8lmHceuHB_YktGN4Ewwk6-ofV0dOF8-BgrMtjZJ1dP_BgtR6haO013R61awGH1hJCz76477ykJzmbILbAcZ2Fr9Ki_UrJWOCFw1IQzadAcrPJk09N0p26KsM2pp2F-7TPVTKSsaXhQF5esN6aec5SwlNeIHu5rgCW93atHEvwdh6bBa3bKYzc2EMCJlgJF3UTgkPx3hyxL8Uut5KVGTwIDAQAB";
  const RSA512_PRIMES_2: string = 'RSA1024|PRIMES_2';
  const RSA512_PKCS1: string = 'RSA1024|PKCS1';

  let rsaGenerator = cryptoFramework.createAsyKeyGenerator(RSA512_PRIMES_2);
  let cipher = cryptoFramework.createCipher(RSA512_PKCS1);
  let that = new util.Base64Helper();
  // let priKey = that.decodeSync(RSA_DECRYPT_KEY);
  let pubKey = new Uint8Array(buffer.from(PUBLICKEY_DEBUG, 'base64url').buffer);
  let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKey };
  rsaGenerator.convertKey(pubKeyBlob, null, (err, keyPair) => {
    if (err) {
      Logger.error(TAG, "convertKey: error." + (err as BusinessError).code);
      return;
    }
    cipher.init(cryptoFramework.CryptoMode.DECRYPT_MODE, keyPair.pubKey, null, (err, data) => {
      try {
        // let newMessage = that.decodeSync(message);
        let newMessage = new Uint8Array(buffer.from(message, 'base64url').buffer);

        let input: cryptoFramework.DataBlob = { data: newMessage };
        cipher.doFinal(input, (err, data) => {
          if (err) {
            Logger.error(TAG, "cipher doFinal." + (err as BusinessError).code);
            return;
          }
          Logger.info(TAG, "DecryptOutPut is " + data.data);
          let result = this.uint8ArrayToString(data.data);
          Logger.info(TAG, "result is " + result);
          callback(result)
        })
      } catch (err) {
        Logger.info(TAG, "cipher init error: " + (err as BusinessError).code);
        return err;
      }
    })
  })
}
HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Excelsior_abit

私钥加密,公钥解密,公钥解密的部分,需要按照如下的代码进行编写:

//私钥加密
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;
}

使用rsa签名,对要加密的数据长度有限制,小于等于密钥长度-11个字节

分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS 私钥加密解密问题
78浏览 • 1回复 待解决
HarmonyOS RSA解密
16浏览 • 1回复 待解决
HarmonyOS RSA如何用解密
666浏览 • 0回复 待解决
HarmonyOS 支持rsa使用解密
499浏览 • 1回复 待解决
HarmonyOS 关于RSA解密问题
33浏览 • 1回复 待解决
RSA加密,使用自有私钥解密报错
268浏览 • 1回复 待解决
rsa 解密初始化报错
309浏览 • 1回复 待解决
如何使用RSA加密明文数据
496浏览 • 1回复 待解决
如何实现RSAPK加密一段文字
638浏览 • 1回复 待解决
HarmonyOS RSA加密中,公私钥问题
19浏览 • 1回复 待解决
HarmonyOS RSA加密的base64秘转换
20浏览 • 1回复 待解决
HarmonyOS 加密导入RSA1024秘对Demo
23浏览 • 1回复 待解决