HarmonyOS RSA加密中,公私钥问题

关于RSA加密有一个问题,咱们之前项目中用到的公私钥是字符串类型的,怎么转化为HarmonyOS 版本使用的格式keyPair.pubKey和keyPair.priKey格式?

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

可以参考以下demo:

import { buffer, util } from '@kit.ArkTS';
import { cryptoFramework } from '@kit.CryptoArchitectureKit';

async function main() {
  // 私钥
  const privatekey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMTW+419MhK4O1IjKxpG4JCouhIpgplQjYHPQ5VzA......Dxiz23k21Zs="
  // 公钥
  const pubkey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDE1vuNfTISuDtSIysaRuCQqLoSKYKZUI2Bz0OVcwM9tPGHDqkw452I38k9MUxDbnz2+8pUgT3wUkcdReybP+mA......"

  // 先对公钥进行base64解码,获取公私钥Uint8Array对象
  let base64 = new util.Base64Helper();
  let pkData = base64.decodeSync(pubkey );
  let skData = base64.decodeSync(privatekey )

  // 生成公私钥对
  let keyPair = await genKeyPairByData(pkData, skData);

  let pubKey = keyPair.pubKey;
  let priKey = keyPair.priKey;
  let message = 'This is a test';

  // 把字符串按utf-8解码为Uint8Array
  let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from('AGDMYBAYQWAYVRXY', 'utf-8').buffer) };
  let encryptText = await encryptMessagePromise(pubKey, plainText);
  let decryptText = await decryptMessagePromise(priKey, encryptText);
  if (plainText.data.toString() === decryptText.data.toString()) {
    console.info('decrypt ok');
    // 把Uint8Array按utf-8编码为字符串
    let messageDecrypted = buffer.from(decryptText.data).toString('utf-8');
    console.info('decrypted result string:' + messageDecrypted);
  } else {
    console.error('decrypt failed');
  }

}

// 加密消息
async function encryptMessagePromise(publicKey: cryptoFramework.PubKey, plainText: cryptoFramework.DataBlob) {
  let cipher = cryptoFramework.createCipher('RSA1024|PKCS1');
  await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, publicKey, null);
  let encryptData = await cipher.doFinal(plainText);
  return encryptData;
}
// 解密消息
async function decryptMessagePromise(privateKey: cryptoFramework.PriKey, cipherText: cryptoFramework.DataBlob) {
  let decoder = cryptoFramework.createCipher('RSA1024|PKCS1');
  await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, privateKey, null);
  let decryptData = await decoder.doFinal(cipherText);
  return decryptData;
}
// 生成RSA密钥对
async function genKeyPairByData(pubKeyData: Uint8Array, priKeyData: Uint8Array) {
  let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKeyData };
  let priKeyBlob: cryptoFramework.DataBlob = { data: priKeyData };
  let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024');
  let keyPair = await rsaGenerator.convertKey(pubKeyBlob, priKeyBlob);
  console.info('convertKey success');
  return keyPair;
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS RSA私钥加密公钥解密
34浏览 • 1回复 待解决
RSA加密,使用自有私钥解密报错
268浏览 • 1回复 待解决
HarmonyOS 私钥加密公钥解密问题
78浏览 • 1回复 待解决
HarmonyOS RSA加密失败问题
26浏览 • 1回复 待解决
Payment Kit的签名公私钥怎么获取?
1446浏览 • 1回复 待解决
HarmonyOS RSA加密方式
669浏览 • 1回复 待解决
HarmonyOS rsa加密报错
40浏览 • 1回复 待解决
HarmonyOS RSA分段加密乱码
628浏览 • 1回复 待解决
HarmonyOS 实现RSA加密功能
621浏览 • 1回复 待解决
HarmonyOS RSA加密算法实现
36浏览 • 1回复 待解决
RSA NoPadding模式加密失败
1268浏览 • 1回复 待解决
HarmonyOS 请问ArkTS如何实现RSA加密
522浏览 • 1回复 待解决