HarmonyOS 鸿蒙Next RSA/ECB/PKCS1Padding加密模式的解决方案

发布于 1周前 作者 zlyuanteng 最后一次编辑是 5天前 来自 鸿蒙OS

HarmonyOS 鸿蒙Next RSA/ECB/PKCS1Padding加密模式的解决方案

1 回复

更多关于HarmonyOS 鸿蒙Next RSA/ECB/PKCS1Padding加密模式的解决方案的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


请参考如下代码:
public static async add(str: string, publicKey: string): Promise<string> {

  let result = ``;

  try {

  let asyKeyGenerator = cryptoFramework.createAsyKeyGenerator("RSA1024");

  let cipher = cryptoFramework.createCipher("RSA|ECB|PKCS1"); //创建一个 Cipher (加密)对象

  //引入外部的公钥加密

  let publicKeyDataBlob = { data: base.decodeSync(publicKey) };

  let keyGenPromise: cryptoFramework.KeyPair = await asyKeyGenerator.convertKey(publicKeyDataBlob, null);

  await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, keyGenPromise.pubKey, null);

  let put: cryptoFramework.DataBlob = { data: stringToUint8Array(str) };

  const finalRes = await cipher.doFinal(put)

  result = base.encodeToStringSync(finalRes.data);

} catch (err) {

  console.log(err.message)

}

return result;

}
回到顶部