HarmonyOS 鸿蒙Next RSA加密,如何将下列java的加密方法转换成鸿蒙对应的方法?

发布于 1周前 作者 songsunli 来自 鸿蒙OS

HarmonyOS 鸿蒙Next RSA加密,如何将下列java的加密方法转换成鸿蒙对应的方法?

开发android对应的鸿蒙app版本,碰到rsa加密问题,请问下如何将下面的java版本的RSA加密转换成鸿蒙版本?谢谢各位老师指点!a

public static PublicKey loadPublicKey(String publicKeyStr) throws Exception
{
try
{
byte[] buffer = Base64Utils.decode(publicKeyStr);
KeyFactory keyFactory = KeyFactory.getInstance(“RSA”);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
return (RSAPublicKey) keyFactory.generatePublic(keySpec);
} catch (NoSuchAlgorithmException e)
{
throw new Exception(“无此算法”);
} catch (InvalidKeySpecException e)
{
throw new Exception(“公钥非法”);
} catch (NullPointerException e)
{
throw new Exception(“公钥数据为空”);
}
}

public static byte[] encryptData(byte[] data, PublicKey publicKey) { try { Cipher cipher = Cipher.getInstance(“RSA/ECB/PKCS1Padding”);
cipher.init(Cipher.ENCRYPT_MODE, publicKey); return cipher.doFinal(data); } catch (Exception e) { e.printStackTrace(); return null; } }
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>

android项目中会先 loadPublicKey方法传入一个key,再调用encryptData方法传入需要加密的内容。



关于HarmonyOS 鸿蒙Next RSA加密,如何将下列java的加密方法转换成鸿蒙对应的方法?的问题,您也可以访问:https://www.itying.com/category-93-b0.html 联系官网客服。

3 回复
您好,欢迎使用三方库eftool,里面有RSASync工具类有丰富的方法,并且还有诸多UI组件
public static async getPublicKey(key: string): Promise<cryptoFramework.KeyPair> {
let base64Helper = new util.Base64Helper();
//把服务端公钥进行base64解码
let cerData = base64Helper.decodeSync(key)
// 公钥转换为Uint8Array,然后包装为DataBlob类型
let pubKeyBlob: cryptoFramework.DataBlob = { data: cerData };
// 创建RSA key生成器
let rsaGenerator = cryptoFramework.createAsyKeyGenerator("RSA2048");
// 将公钥包装数据pubKeyBlob转换成密钥对类型KeyPair
let keyPair = await rsaGenerator.convertKey(pubKeyBlob, null);
return keyPair;
}

/*//**
* [@Description](/user/Description): RSA分段加密
* [@param](/user/param) pubKey cryptoFramework.PubKey
* [@param](/user/param) plainText 待加密明文
*/
public static async encryptRSA(pubKey: cryptoFramework.PubKey, plainText: string): Promise<string> {
//创建 Cipher对象
let cipher = cryptoFramework.createCipher("RSA2048|PKCS1");
//包装要加密的明文
let plainTextBlob: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(plainText, 'utf-8').buffer) };
//初始化加密模式,指定密钥keyPair.pubKey
await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, pubKey, null);
let plainTextSplitLen = RSAUtil.RSA_MAX_ENCRYPT_SIZE;
let cipherText = new Uint8Array();
//进行分段加密
for (let i = 0; i < plainTextBlob.data.length; i += plainTextSplitLen) {
let updateMessage = plainTextBlob.data.subarray(i, i + plainTextSplitLen);
let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage };
//将原文按plainTextSplitLen字符进行拆分,循环调用doFinal进行加密,使用2048bit密钥时,每次加密生成RSA_MAX_ENCRYPT_SIZE字节长度的密文
let updateOutput = cipher.doFinalSync(updateMessageBlob);
let mergeText = new Uint8Array(cipherText.length + updateOutput.data.length);
mergeText.set(cipherText);
mergeText.set(updateOutput.data, cipherText.length);
cipherText = mergeText;
}
let cipherBlob: cryptoFramework.DataBlob = { data: cipherText };
let base = new util.Base64Helper();
// 返回加密后的字符串
let encodeString = base.encodeToStringSync(cipherBlob.data);
return encodeString;
}
[@ohos](/user/ohos)/crypto-js  试下这个三方插件
回到顶部