HarmonyOS RSA加密的base64秘钥转换

let  _key =   "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtYPCnyI7KlbdwBCVTPK...........wzkf0xzT40qC3axlKCAFAdrdWfnYq2tHgM2N81bk7pZty4PytsEnTcYbsac5/miop74kInsmX9XiTwRah/tDNq/gV4ilThbJL3QEoBdmrQQxX5tx8O00IBFfeeXh00n2YROnwPxzsGnT8RtFLYeMv4XqQ5+eotEFItc20UmLEETLnkd8DcuVgMe+EF6wD8S7XU8wTRyNB9XDtUNNCpApUqPXK8L7u+MWUYwV4VnFoa13F/Er67x1/At7dt2eTbcnAaYnoXkm1B8X4P+Q8NFjgjwIDAQAB"

let base64Util = new util.Base64Helper()
let pubData = base64Util.decodeSync(_key)
let pair = cryptoFramework.createAsyKeyGenerator('RSA1024').convertKeySync({ data: pubData }, null)
if (pair) {
  let aesKeyEnc = await EncryptUtils.encryptMessagePromise(pair.pubKey, { data })
  let resultStr = base64Util.encodeToStringSync(aesKeyEnc.data)
}

export 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;
}

我这里有一个base64的公钥字符串,要用它来给数据加密,现在遇到的问题是 我加密出来的数据后台没法解密。整个过程看起来是非常合理的,调试过程有发现生成秘钥对的数组大小是294 ,你们的一些demo上面的rsa秘钥长度是256 。是不是转换有问题

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

尝试后台是否能解开 以下为使用公钥进行加密的base64格式的字符串 :

base64 encrypted result string:SbdMhoDQFP5GWk3U8nvOxTTCUEMgMKYa.......T7KZYXYc8FkWWL+nNbv1CuEn3pVgh8aYjKZNPw9C3xJvlK28o2wWp3w8YX9LIKz31shF4SZg7Kpa8czwIM85ub0WPNay+E3LIXMU+16PlMMf7umJ0od9sDWMTPjdOOzWb9+gbYfmY7gZdvZLnqEBBuM8duxLMnjsB3KeCmp4o+us3FG9T2SN4LoUse1xDjSV3dsSBjKyWBptDvfZQADnqolW0r9KTtu2Q54VtJ7M1oft8pUCL69xUG6x/qWjt2HuTv4Wp2nfeSP+0mGGA==

使用的RSA1024|PKCS1 模式 代码如下

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

@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 keyP = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtYPCnyI7KlbdwBCVTPK........oAGdNi7N6ySwzkf0xzT40qC3axlKCAFAdrdWfnYq2tHgM2N81bk7pZty4PytsEnTcYbsac5/miop74kInsmX9XiTwRah/tDNq/gV4ilThbJL3QEoBdmrQQxX5tx8O00IBFfeeXh00n2YROnwPxzsGnT8RtFLYeMv4XqQ5+eotEFItc20UmLEETLnkd8DcuVgMe+EF6wD8S7XU8wTRyNB9XDtUNNCpApUqPXK8L7u+MWUYwV4VnFoa13F/Er67x1/At7dt2eTbcnAaYnoXkm1B8X4P+Q8NFjgjwIDAQAB"
  let base64 = new util.Base64Helper();
  let base64pkData = base64.decodeSync(keyP )
  let base64skData = new Uint8Array /*base64.decodeSync(skData )*/;

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

  let message = " This is a long plainTest!"/*'This is a test'*/;
  // let messageBase64 = 'BN+lPyyHzI1N3V3zn6iEySK+J9U1AqYZJq7EsFaoZNoVOlNnZryFwioJvlnBuj8jXINxy25xLroh/j98d2GRbvE0NQBql0baJ6HKsNLbp1PiqXjfWBHlDByhijGS42TgI07bLDTph3mYuM7nXxzEqfKRiowLYBqX0Z2d2m6j4NM='
  // 把字符串按utf-8解码为Uint8Array
  let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(/*base64.decodeSync(messageBase64)*/buffer.from(message, 'utf-8').buffer) };
  let encryptText = await encryptMessagePromise(pubKey, plainText);
  // let messageEncrypted = buffer.from(encryptText.data).toString('utf-8');
  let base64EncryptoText = base64.encodeToStringSync(new Uint8Array(encryptText.data))
  console.error('base64 encrypted result string:' + base64EncryptoText);

  try{
    // let decryptText = await decryptMessagePromise(priKey, plainText);
  } catch (error){
    console.error(JSON.stringify(error) );
  }

  /*let baseDecrypto = base64.encodeToStringSync(decryptText.data)
  console.error('base64 decrypted result string:' + baseDecrypto);

  let messageDecrypted = buffer.from(decryptText.data).toString('utf-8');
  console.error('decrypted result string:' + messageDecrypted);*/
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS 加密导入RSA1024对Demo
26浏览 • 1回复 待解决
HarmonyOS 中文base64加密解密乱码
604浏览 • 1回复 待解决
HarmonyOS 如何做base64加密
29浏览 • 1回复 待解决
HarmonyOS 加解密base64转换
36浏览 • 1回复 待解决
如何base64转换成图片?
10342浏览 • 2回复 待解决
HarmonyOS 关于base64和aes加密相关问题
37浏览 • 1回复 待解决
如何实现PixelMap和base64相互转换
1877浏览 • 1回复 待解决
HarmonyOS 如何将base64数据转换为图片
553浏览 • 1回复 待解决
HarmonyOS base64编码API
214浏览 • 1回复 待解决
HarmonyOS base64编码问题
672浏览 • 1回复 待解决
HarmonyOS base64转image
84浏览 • 1回复 待解决
HarmonyOS 图片转base64
122浏览 • 1回复 待解决
HarmonyOS RSA私钥加密解密
48浏览 • 1回复 待解决
HarmonyOS base64解码报错
1064浏览 • 1回复 待解决
HarmonyOS base64编解码
43浏览 • 0回复 待解决
HarmonyOS 图片Base64编码
75浏览 • 1回复 待解决
HarmonyOS arraybuffer转base64乱码
185浏览 • 1回复 待解决
HarmonyOS Image加载base64失败
39浏览 • 1回复 待解决
HarmonyOS 图片进行base64编码报错
735浏览 • 1回复 待解决
HarmonyOS string怎么base64和decodeBase64
70浏览 • 1回复 待解决
PixelMap类型怎么转换Base64字符串
661浏览 • 1回复 待解决