当前位置:   article > 正文

【HarmonyOS NEXT】服务端下发RSA公钥字符串,前端需要对明文数据进行加密,如何对这个公钥进行转换_鸿蒙 rsa 加密

鸿蒙 rsa 加密

 

解答

传入公钥导入,然后加密,具体代码参考如下:

  1. let base = new util.Base64Helper();
  2. let pubKey = "HIHHBI*****qQSDFSDFSD";//公钥
  3. let asyKeyGenerator = cryptoFramework.createAsyKeyGenerator("RSA1024");
  4. let cipher = cryptoFramework.createCipher("RSA1024|PKCS1"); //创建一个 Cipher (解密)对象
  5. //引入外部的公钥加密
  6. let publicKeyDataBlob:cryptoFramework.DataBlob = { data: base.decodeSync(pubKey) };
  7. let keyGenPromise: cryptoFramework.KeyPair = await asyKeyGenerator.convertKey(publicKeyDataBlob, null);
  8. await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, keyGenPromise.pubKey, null);
  9. let put: cryptoFramework.DataBlob = { data: stringToUint8Array("加密内容sadad") };
  10. const finalRes = await cipher.doFinal(put)let result = base.encodeToStringSync(finalRes.data);
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/314063
推荐阅读
相关标签
  

闽ICP备14008679号