HarmonyOS 支持rsa使用公钥解密吗

HarmonyOS  支持rsa使用公钥解密吗

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

支持。可以参考:

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

//私钥加密 
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-08-13 21:45:54
相关问题
HarmonyOS RSA如何用解密
465浏览 • 0回复 待解决
rsa 解密初始化报错
96浏览 • 1回复 待解决
如何使用RSA加密明文数据
361浏览 • 1回复 待解决
如何实现RSAPK加密一段文字
533浏览 • 1回复 待解决
HarmonyOSRSA解密相关
336浏览 • 0回复 待解决
HarmonyOS RSA解密问题
343浏览 • 1回复 待解决
RSA加密,使用自有私钥解密报错
120浏览 • 1回复 待解决
HarmonyOS rsa解密速度过于缓慢
434浏览 • 1回复 待解决
使用32字节秘解密后报错
1572浏览 • 1回复 待解决
RSA导入外部密钥实现加解密
704浏览 • 1回复 待解决