HarmonyOS ArkTS不支持RSA公钥解密吗,有其他ohpm库可以用吗?

关于使用crytoFramework库中的RSA用私钥加密或者公钥解密会报错。https://developer.huawei.com/consumer/cn/forum/topic/0202138475424398205

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { buffer, util } from '@kit.ArkTS';
@Entry
@Component
struct RSAReverseCrypto {
  @State message: string = '点击开始';
  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            RsaReverse()
          })
      }
      .width('100%') }
    .height('100%')
  }
}
// 字符串转成字节流
export function stringToUint8Array(str: string) {
  return new Uint8Array(buffer.from(str,'utf-8').buffer);
}
// Uint8Array转成String
function uint8ArrayToString(input: Uint8Array): string {
  let textDecoder = util.TextDecoder.create('utf-8');
  return textDecoder.decodeWithStream(input);
}
// 公钥解密
function verifyMessage(pubKey: cryptoFramework.PubKey,signMessageBlob: cryptoFramework.DataBlob) {
  let verifyAlg = "RSA2048|PKCS1|NoHash|Recover";
  let verifier = cryptoFramework.createVerify(verifyAlg);
  verifier.initSync(pubKey);
  let rawSignData = verifier.recoverSync(signMessageBlob);
  console.log("rsa puk data " + uint8ArrayToString(rawSignData?.data))
  return rawSignData;
}
//私钥加密
function signMessage(priKey: cryptoFramework.PriKey, plainText:cryptoFramework.DataBlob) {
  let signAlg = "RSA2048|PKCS1|NoHash|OnlySign";
  let signer = cryptoFramework.createSign(signAlg);
  signer.initSync(priKey);
  let signData = signer.signSync({data:plainText.data});
  return signData;
}
// 密钥转换
function genKeyPairByData(pubKeyData: Uint8Array, priKeyData: Uint8Array) {
  let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKeyData };
  let priKeyBlob: cryptoFramework.DataBlob = { data: priKeyData };
  let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024');
  let keyPair = rsaGenerator.convertKeySync(pubKeyBlob, priKeyBlob);
  console.info('convertKey success'); return keyPair;
}
//Rsa 加解密
function RsaReverse() {
  let pkData = "公钥";
  let skData = "私钥";
  let base64 = new util.Base64Helper();
  let pubKeyBlob = base64.decodeSync(pkData)
  let priKeyBlob = base64.decodeSync(skData);
  let keyPair = genKeyPairByData(pubKeyBlob, priKeyBlob);
  let pubKey = keyPair.pubKey
  let priKey = keyPair.priKey
  let message = "This is a test";
  let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) };
  let encryptText = signMessage(priKey, plainText);
  let decryptText = verifyMessage(pubKey, encryptText);
  console.info('decrypt plainText: ' + buffer.from(decryptText!.data).toString('utf-8'));
}
分享
微博
QQ
微信
回复
2天前
相关问题
HarmonyOS 支持rsa使用解密
562浏览 • 1回复 待解决
HarmonyOS RSA解密
158浏览 • 1回复 待解决
HarmonyOS RSA私钥加密解密
272浏览 • 1回复 待解决
HarmonyOS RSA如何用解密
775浏览 • 0回复 待解决
HarmonyOSRSA如何用解密
97浏览 • 0回复 待解决
HarmonyOS 关于RSA解密问题
231浏览 • 1回复 待解决
rsa 解密初始化报错
436浏览 • 1回复 待解决
HarmonyOS ArkTS不支持正则匹配
127浏览 • 1回复 待解决
HarmonyOS RSA解密相关
659浏览 • 0回复 待解决
HarmonyOS 可以用仓颉开发HarmonyOS
195浏览 • 1回复 待解决
HarmonyOS image不支持mask
183浏览 • 1回复 待解决
HarmonyOS @State不支持HashMap
67浏览 • 1回复 待解决
HarmonyOS color文件不支持rgba
153浏览 • 1回复 待解决
HarmonyOS Object不支持 ... 展开符
401浏览 • 1回复 待解决
HarmonyOS ets不支持匿名类
354浏览 • 2回复 待解决
HarmonyOS 私钥加密解密问题
148浏览 • 1回复 待解决