HarmonyOS RSA/ECB/PKCS1 秘钥长度为2048位加解密,验签

Java RSA加解密以及验签

HarmonyOS
2024-08-13 15:02:54
浏览
收藏 1
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple

可以尝试下如下代码:

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-V5/crypto-rsa-asym-encrypt-decrypt-by-segment-0000001821000085-V5

分享
微博
QQ
微信
回复
2024-08-13 21:45:01
相关问题
签名算法不支持RSA|PKCS8|SHA1
30浏览 • 1回复 待解决
使用32字节加解密后报错
1535浏览 • 1回复 待解决
RSA签名后,后端失败
19浏览 • 1回复 待解决
HarmonyOS RSA如何用公解密
412浏览 • 0回复 待解决
HarmonyOS 支持rsa使用公解密
328浏览 • 1回复 待解决
HarmonyOSRSA加解密相关吗
295浏览 • 0回复 待解决
RSA导入外部密钥实现加解密
669浏览 • 1回复 待解决
HarmonyOS SM2加SM3摘要算法使用
221浏览 • 1回复 待解决
HarmonyOS RSA解密问题
317浏览 • 1回复 待解决
加解密HmacSha1 、HmacSha256、aes参考Demo
1922浏览 • 1回复 待解决
实现一次非对称RSA非对称加解密
922浏览 • 1回复 待解决
4.1.0(11) fluterr 无法通过
812浏览 • 1回复 待解决
HarmonyOS HmacSHA1算法实现
516浏览 • 1回复 待解决