HarmonyOS 鸿蒙Next rsa 加解密
HarmonyOS 鸿蒙Next rsa 加解密 rsa 如何使用公钥加密, 转字符串为秘钥对象
密钥规格:
ASY_KEY_NAME_RSA_3072
: ‘RSA1024’;ALG_NAME_RSA_3072
: ‘RSA|ECB|PKCS1’;priKey
: new Uint8Array(); //用于临时保存pubKey
: new Uint8Array(); //用于临时保存
// 生成非对称密钥对
public static async generateRsaKeyPair(): Promise<KeyPair> { let keyPair: KeyPair = new KeyPair(); try { let asyKeyGenerator = cryptoFramework.createAsyKeyGenerator(this.ASY_KEY_NAME_RSA_3072); const tempKeyPair = await asyKeyGenerator.generateKeyPair(); keyPair = { publicKey: base.encodeToStringSync(tempKeyPair.pubKey.getEncoded().data), privateKey: base.encodeToStringSync(tempKeyPair.priKey.getEncoded().data) } } catch (err) { console.error(err) } return keyPair; }
// 加密
public static async add(str: string, publicKey: string): Promise<string> { let result = ‘’; try { let asyKeyGenerator = cryptoFramework.createAsyKeyGenerator(this.ASY_KEY_NAME_RSA_3072); //创建一个 Cipher (解密)对象 let cipher = cryptoFramework.createCipher(this.ALG_NAME_RSA_3072); //引入外部的公钥加密 const 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; }
// 解密
public static async rsaDecrypt(message: string | Uint8Array, privateKey: string): Promise<string> { let result = ‘’; try { let asyKeyGenerator = cryptoFramework.createAsyKeyGenerator(this.ASY_KEY_NAME_RSA_3072); const privateKeyDataBlob = { data: base.decodeSync(privateKey) }; const keyPair = await asyKeyGenerator.convertKey(null, privateKeyDataBlob); let cipher = cryptoFramework.createCipher(this.ALG_NAME_RSA_3072); //创建一个 Cipher (解密)对象 await cipher.init(cryptoFramework.CryptoMode.DECRYPT_MODE, keyPair.priKey, null); let bytes: Uint8Array = null; if (typeof message === ‘string’) { bytes = base.decodeSync(message); } else { bytes = message; } const finalRes = await cipher.doFinal({ data: bytes }) result = uint8ArrayToString(finalRes.data); } catch (err) { console.error(err.code) } return result; }
// 公共方法
const base = new util.Base64Helper();
// 字符串转成字节流 function stringToUint8Array(str: string) { return new Uint8Array(buffer.from(str,‘utf-8’).buffer); }
// 字节流转成可理解的字符串 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; }
更多关于HarmonyOS 鸿蒙Next rsa 加解密的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
public static async encryptMessageReturnKeyPair(publicKey: string, text: string) {
let cipher = cryptoFramework.createCipher('RSA2048|PKCS1');
let base64Helper = new util.Base64Helper()
let pubData = base64Helper.decodeSync(publicKey)
let pubKeyBlob: cryptoFramework.DataBlob = { data: pubData };
let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA2048|PRIMES_2');
let keyPair = rsaGenerator.convertKeySync(pubKeyBlob, null)
await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, keyPair.pubKey, null)
let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(text, 'utf-8').buffer) };
let encryptData = await cipher.doFinal(plainText)
let newPubKeyBlob: cryptoFramework.DataBlob = { data: encryptData.data };
//此处两个参数一个是公钥,一个是私钥,不满足的你的需求,自己修改一下
return rsaGenerator.convertKeySync(newPubKeyBlob, null)
}
针对帖子标题“HarmonyOS 鸿蒙Next rsa 加解密”的问题,以下是一个简洁且专业的回答:
在HarmonyOS鸿蒙Next系统中,RSA加解密操作通常依赖于系统内置的加密库或框架来实现。这些库或框架提供了高效的API接口,用于执行RSA算法的各种操作,包括密钥生成、加密、解密、签名和验证等。
在鸿蒙系统中进行RSA加解密时,开发者需要首先导入相关的加密库(如果系统未默认包含)。然后,通过调用库中的API来生成RSA密钥对,使用公钥进行加密操作,以及使用私钥进行解密操作。需要注意的是,RSA加密通常用于传输对称加密的密钥,而不是直接加密大块数据,因为RSA加密的性能相对较低。
此外,鸿蒙系统可能还提供了硬件级别的加密支持,这可以进一步提高RSA加解密的效率和安全性。开发者在编写相关代码时,应参考鸿蒙系统的官方文档和API指南,以确保正确、安全地使用RSA加解密功能。
如果开发者在鸿蒙系统中实现RSA加解密时遇到问题,建议检查密钥的正确性、API调用的准确性以及系统日志中的错误信息。如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html。