中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
将java中RSA加密代码翻译成 arkts 代码时,无法确定正确的API。希望提供正确的arkts代码示例。
微信扫码分享
//加解密分开示例: //加密: public static async add(str: string, publicKey: string): Promise<string> { let result = ''; try { let asyKeyGenerator = cryptoFramework.createAsyKeyGenerator(this.ASY_KEY_NAME_RSA_3072); const publicKeyDataBlob = { data: this.decodeToUint8Array(publicKey) }; let cipher = cryptoFramework.createCipher(this.ALG_NAME_RSA_3072); //创建一个 Cipher (解密)对象 let put: cryptoFramework.DataBlob = { data: this.stringToUint8Array(str) }; let globalKeyPair: cryptoFramework.KeyPair = await asyKeyGenerator.convertKey(publicKeyDataBlob, null); await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, globalKeyPair.pubKey, null); const finalRes = await cipher.doFinal(put) result = this.encodeToString(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: this.decodeToUint8Array(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 = this.decodeToUint8Array(message); } else { bytes = message; } const finalRes = await cipher.doFinal({ data: bytes }) result = this.uint8ArrayToString(finalRes.data); } catch (err) { console.error(err.code) } return result; }