你可以尝试下如下代码:// RSA加解密 const pubKey = 'MIGfxxx0GCSxxxxxxxxxxxxxxxxxxxxxxxxxx公钥参数'; const priKey = 'MIICxxxxBADANBxxxxxxxxxxxxxxxxx私钥参数' const base64 = new util.Base64Helper() const publicKeyDataBlob: cryptoFramework.DataBlob = { data: base64.decodeSync(pubKey) }; const priKeyDataBlob: cryptoFramework.DataBlob = { data: base64.decodeSync(priKey) } const asyKeyGenerator = cryptoFramework.createAsyKeyGenerator('RSA2048'); const keyGenPromise: cryptoFramework.KeyPair = await asyKeyGenerator.convertKey(publicKeyDataBlob, priKeyDataBlob); let cipher = cryptoFramework.createCipher('RSA|ECB|PKCS1'); //创建一个 Cipher (解密)对象 await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, keyGenPromise.pubKey, null); const plaintext = '我是明文' let put: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(plaintext).buffer) }; const finalRes = await cipher.doFinal(put) await cipher.init(cryptoFramework.CryptoMode.DECRYPT_MODE, keyGenPromise.priKey, null) const result = await cipher.doFinal(finalRes) console.log(TAG, 'success'); // RSA验签 const pubKey = 'MIGfMA0GCS公钥'; const priKey = 'MIICdQIBADA私钥' // 完整的明文被拆分为input1和input2 let input1: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("This is Sign test plan1", 'utf-8').buffer) }; let input2: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("This is Sign test plan2", 'utf-8').buffer) }; const base64 = new util.Base64Helper() const publicKeyDataBlob: cryptoFramework.DataBlob = { data: base64.decodeSync(pubKey) }; const priKeyDataBlob: cryptoFramework.DataBlob = { data: base64.decodeSync(priKey) } const asyKeyGenerator = cryptoFramework.createAsyKeyGenerator('RSA2048'); const keyGen: cryptoFramework.KeyPair = await asyKeyGenerator.convertKey(publicKeyDataBlob, priKeyDataBlob); let signAlg = "RSA2048|PKCS1|SHA256"; let signer = cryptoFramework.createSign(signAlg); await signer.init(keyGen.priKey); await signer.update(input1); // 如果明文较短,可以直接调用sign接口一次性传入 let signData = await signer.sign(input2); let verifyAlg = "RSA2048|PKCS1|SHA256"; let verifier = cryptoFramework.createVerify(verifyAlg); await verifier.init(keyGen.pubKey); await verifier.update(input1); // 如果明文较短,可以直接调用verify接口一次性传入 let res = await verifier.verify(input2, signData);分段加解密可以参考一下指南:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides...
你可以尝试下如下代码:
分段加解密可以参考一下指南:
https://developer.huawei.com/consumer/cn/doc/harmonyos-guides...