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
275浏览 • 1回复 待解决
HarmonyOS AES/ECB/PKCS7Padding 加解密
26浏览 • 1回复 待解决
HarmonyOS 加解密方式 AES/ECB/PKCS5Padding
43浏览 • 1回复 待解决
HarmonyOS 关于RSA加解密问题
40浏览 • 1回复 待解决
使用32字节加解密后报错
1712浏览 • 1回复 待解决
HarmonyOS RSA签名相关
40浏览 • 1回复 待解决
HarmonyOS RSA解密
16浏览 • 1回复 待解决
RSA签名后,后端失败
213浏览 • 1回复 待解决
HarmonyOS 接口参数的RSA签名+
25浏览 • 1回复 待解决
HarmonyOS RSA如何用公解密
666浏览 • 0回复 待解决
HarmonyOS RSA私钥加密公解密
48浏览 • 1回复 待解决
HarmonyOS RSA加密的base64转换
20浏览 • 1回复 待解决
HarmonyOS 加密导入RSA1024对Demo
26浏览 • 1回复 待解决
RSA导入外部密钥实现加解密
821浏览 • 1回复 待解决
HarmonyOSRSA加解密相关吗
461浏览 • 0回复 待解决
HarmonyOS 支持rsa使用公解密
499浏览 • 1回复 待解决
HarmonyOS 是否有rsa加解密的例子?
45浏览 • 1回复 待解决
HarmonyOS Native RSA 加解密实现咨询
35浏览 • 1回复 待解决