HarmonyOS 公钥的格式相关

如何将下面的字符串,先经过base64解密,解密完的二进制转16进制

MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQClTsX4wJRr7xW7EKVUtSUg/K1frM2UGUOUya6MQCXwl+hHXxyqnf03lPKOdF2tTKARGoFVuy1EDLHca47rrnxt4SgtupzWy6W3sAda3Em5akn2onPkkUmPynnQD7ORW7N77frZ+cMX3eKhSx5njfiftD2CWy6bYD57fXY8NXSCSQIDAQAB

最后变成如下格式(不是变成下面一样的字符串,而是变成一样的格式,由数字+字母abcdef组成)

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

HarmonyOS 端支持原生rsa加密 并且可以直接接受base64的公钥和私钥进行加解密 请参考一下代码demo实例

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

@Entry
@Component
struct RSACrypto {
  @State message: string = '点击开始';

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            rsa1024Crypto()
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

// 加密消息
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;
}
async function rsa1024Crypto() {
  let pkData = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCjSASCSzDPoXFq/6V5kXhhdH...........o2jQvnx8tRX0mof0Sk1GqgqSUHniXOJewu6KHDt8wZTnY8ncB3HJmPUa8Pct01MOmRZI3a31Rh6P8mZPI1iwZD/lxGkkyVo2MQDp/M4AwIy7n+QkGkfQIDAQAB";
  let skData = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKNIBIJLMM+hcWr/pXmReGF0f6AtpqWaFn4U0lkLHxGv3imj3djzkhzDCjaNC+fHy1FfSah/RKTUaqCpJQeeJc4l7C7oocO3zBlOdjydwHccmY9Rrw9y3TUw6ZFkjdrfVGHo/yZk8jWLBkP+XEaSTJWjYxAOn8zgDAjLuf5CQaR9AgMBAAECgYABLlIqBxUxSz+gwHyX5n9yZP9PT0U3SWgEPW5QCo6M+DKpJnBCU3CpGJgIUPjXElDcI85Kk7ERaB/lTZPg/DnVwuaDoWO3utNkA9J5y1xIrJ+m32Op1WPQjYNTq8CDyVO0wqOsgXVMsbJaEezlNZed2gQ5CQQKLiIDuMznzla+g..........LT4NpVbEXtlpmaqV9wEaEY+QBjdkfMHaI0CQQDDEq4X2VvjCqjUiKeSTNXywxl48LLHC3A5pkl9vRYlt+ec7SLydseShP834DCBZIjRvqHs5UdXzvFOEQYaAIexAkBPc3FfFdpBN3dJctE/w/s8itpPhBILduEAUEaVTRV8FRKtfLfCSKC02UQD5Rx6UJp+frLNFaVERlil36H6JskRAkEAkVE4GZIVTmQhcvo+AtF0S/0k26BLPdX6iyeh9aZHel+ujYtmDkOH1lF3InPDDpELD1y4mZYPeI0z21j5N6OPcQJAdxnnEtw05TaX2DxbVhxPvEcwgpU/CpsrIC1eNe17Th68jAX4xVxTxSAUftECw8rBQ5C9vxGPbuPyvfk6MKkZ8A==";

  let base64 = new util.Base64Helper();
  let base64skData = base64.decodeSync(skData );
  let base64pkData = base64.decodeSync(pkData )

  let keyPair = await genKeyPairByData(base64pkData, base64skData);
  let pubKey = keyPair.pubKey;
  let priKey = keyPair.priKey;

  let message = " This is a long plainTest!";
  // 把字符串按utf-8解码为Uint8Array
  let plainText: cryptoFramework.DataBlob = { data: new Uint8Array( buffer.from(message, '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');
  }
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS RSA解密
16浏览 • 1回复 待解决
HarmonyOS 证书问题
29浏览 • 1回复 待解决
HarmonyOS RSA如何用解密
666浏览 • 0回复 待解决
HarmonyOS 私钥加密解密问题
78浏览 • 1回复 待解决
HarmonyOS RSA私钥加密解密
44浏览 • 1回复 待解决
HarmonyOS 支持rsa使用解密吗
499浏览 • 1回复 待解决
HarmonyOS 关于RSA加解密问题
34浏览 • 1回复 待解决
HarmonyOS SM2PEM读取接口
23浏览 • 1回复 待解决
rsa 解密初始化报错
309浏览 • 1回复 待解决
如何使用RSA加密明文数据
496浏览 • 1回复 待解决
如何实现RSAPK加密一段文字
638浏览 • 1回复 待解决
HarmonyOS 国密SM2转换失败
32浏览 • 1回复 待解决