HarmonyOS RSA如何用公钥解密

RSA使用私钥加密后,如何用公钥解密

HarmonyOS
2024-08-13 15:22:33
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

参考示例如下:

//私钥加密 
async function signMessagePromise(priKey: cryptoFramework.PriKey) { 
  let signAlg = "RSA1024||PKCS1|SHA256|MGF1_SHA256|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|SHA256|MGF1_SHA256|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 stringToUint8Array(str: string) { 
  return new Uint8Array(buffer.from(str,'utf-8').buffer); 
} 
 
// 字节流转成可理解的字符串 
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; 
}

参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/crypto-sign-sig-verify-overview-V5#ZH-CN_TOPIC_0000001847209732__%E7%AD%BE%E5%90%8D%E6%A8%A1%E5%BC%8F%E4%B8%BAonlysign

分享
微博
QQ
微信
回复
2024-08-13 21:06:10
相关问题
HarmonyOS 支持rsa使用解密
315浏览 • 1回复 待解决
如何使用RSA加密明文数据
314浏览 • 1回复 待解决
如何实现RSAPK加密一段文字
479浏览 • 1回复 待解决
HarmonyOS RSA解密问题
306浏览 • 1回复 待解决
HarmonyOSRSA解密相关吗
292浏览 • 0回复 待解决
HarmonyOS rsa解密速度过于缓慢
357浏览 • 1回复 待解决
RSA导入外部密钥实现加解密
665浏览 • 1回复 待解决
实现一次非对称RSA非对称加解密
922浏览 • 1回复 待解决
使用32字节秘解密后报错
1535浏览 • 1回复 待解决