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yw2yCay4aVENnFy/VHAG0Oglythcp/vtFO0iBGvNXlpGNH1JYbB6wEmdcQNRjoqZ7i1CrRir+hEoxNOTCSEC2j9EpFjytvrPZHfS7QUJ0kIiIm91osDM/IJkcaBEgJjeXVkAkx/JJGLWl2mX2Kx8iQPynaYVL/Ivz+rtJjRN5dggmxsQ4L9jQcNBkT779MnLCnrHd8o1iOmqBuOVFY4839Ymlpww3EEckterQgC/IX2UUguzr3o/DGZTMlw6jmVvB0bj5f+PM55u1Yn0SvHDixhiLXk0zbZOo7SUfOldtDAXKUakAtrWRvgqtZw3LdIe8+jQJc/n8T+8mSzcleDArbBUNLxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxF990vau15sSs3ctM3EdFaYGbGTSATAtWLTKCQQ69D9xqxoYdzVooQMqGPnfBNOR+vE=”,

使用该私钥无法解密数据,是什么原因

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

如果未在java后端 对长文本进行分段加密 会导致需要解密的密文过长 从而使HarmonyOS端无法解密成功。

以下为分段加解密HarmonyOS端的代码

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

@Entry
@Component
struct Crypto {
  @State message: string = '点击开始';
  @State outMessage: string = ''
  build() {
    Column() {
      Text(this.message)
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .onClick(() => {
          this.rsaEncryptLongMessage()
        })
      Text(this.outMessage)
        .fontSize(14)
        .fontWeight(FontWeight.Normal)
        .width('80%')
    }
    .height('100%')
    .width('100%')
    .justifyContent(FlexAlign.Center)
    .alignItems(HorizontalAlign.Center)

  }
  // 分段加密消息
  async rsaEncryptBySegment(pubKey: cryptoFramework.PubKey, plainText: cryptoFramework.DataBlob) {
    let cipher = cryptoFramework.createCipher('RSA1024|PKCS1');
    await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, pubKey, null);
    let plainTextSplitLen = 64;
    let cipherText = new Uint8Array();
    for (let i = 0; i < plainText.data.length; i += plainTextSplitLen) {
      let updateMessage = plainText.data.subarray(i, i + plainTextSplitLen);
      let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage };
      // 将原文按64字符进行拆分,循环调用doFinal进行加密,使用1024bit密钥时,每次加密生成128字节长度的密文
      let updateOutput = await cipher.doFinal(updateMessageBlob);
      let mergeText = new Uint8Array(cipherText.length + updateOutput.data.length);
      mergeText.set(cipherText);
      mergeText.set(updateOutput.data, cipherText.length);
      cipherText = mergeText;
    }
    let cipherBlob: cryptoFramework.DataBlob = { data: cipherText };
    return cipherBlob;
  }
  // 分段解密消息
  async rsaDecryptBySegment(priKey: cryptoFramework.PriKey, cipherText: cryptoFramework.DataBlob) {
    let decoder = cryptoFramework.createCipher('RSA1024|PKCS1');
    await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, priKey, null);
    let cipherTextSplitLen = 128; // RSA密钥每次加密生成的密文字节长度计算方式:密钥位数/8
    let decryptText = new Uint8Array();
    for (let i = 0; i < cipherText.data.length; i += cipherTextSplitLen) {
      let updateMessage = cipherText.data.subarray(i, i + cipherTextSplitLen);
      let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage };
      // 将密文按128字节进行拆分解密,得到原文后进行拼接
      let updateOutput = await decoder.doFinal(updateMessageBlob);
      let mergeText = new Uint8Array(decryptText.length + updateOutput.data.length);
      mergeText.set(decryptText);
      mergeText.set(updateOutput.data, decryptText.length);
      decryptText = mergeText;
    }
    let decryptBlob: cryptoFramework.DataBlob = { data: decryptText };
    return decryptBlob;
  }


  async genKeyPairByData(pubKeyData: Uint8Array, priKeyData: Uint8Array) {

  }

  async rsaEncryptLongMessage() {
    let message = "This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
      "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
      "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
      "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
      "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
      "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
      "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
      "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!";
    let base64 = new util.Base64Helper();

    let pkData = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCjSASCSzDPoXFq/6V5kXhhdH+gLaalmhZ+FNJZCx8Rr94xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxt8wZTnY8ncB3HJmPUa8Pct01MOmRZI3a31Rh6P8mZPI1iwZD/lxGkkyVo2MQDp/M4AwIy7n+QkGkfQIDAQAB";
    let skData = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKNIBIJLMM+hcWr/pXmReGF0f6AtpqWxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxJQeeJc4l7C7oocO3zBlOdjydwHccmY9Rrw9y3TUw6ZFkjdrfVGHo/yZk8jWLBkP+XEaSTJWjYxAOn8zgDAjLuf5CQaR9AgMBAAECgYABLlIqBxUxSz+gwHyX5n9yZP9PT0U3SWgEPW5QCo6M+DKpJnBCU3CpGJgIUPjXElDcI85Kk7ERaB/lTZPg/DnVwuaDoWO3utNkA9J5y1xIrJ+m32Op1WPQjYNTq8CDyVO0wqOsgXVMsbJaEezlNZed2gQ5CQQKLiIDuMznzla+gQJBANZHZMh+WX7RMBdeIHmWLRhg/nc9KVWYjSkD/d4S6HTBKASLdEDmSLT4NpVbEXtlpmaqV9wEaEY+QBjdkfMHaI0CQQDDEq4X2VvjCqjUiKeSTNXywxl48LLHC3A5pkl9vRYlt+ec7SLydseShP834DCBZIjRvqHs5UdXzvFOEQYaAIexAkBPc3FfFdpBN3dJctE/w/s8itpPhBILduEAUEaVTRV8FRKtfLfCSKC02UQD5Rx6UJp+frLNFaVERlil36H6JskRAkEAkVE4GZIVTmQhcvo+AtF0S/0k26BLPdX6iyeh9aZHel+ujYtmDkOH1lF3InPDDpELD1y4mZYPeI0z21j5N6OPcQJAdxnnEtw05TaX2DxbVhxPvEcwgpU/CpsrIC1eNe17Th68jAX4xVxTxSAUftECw8rBQ5C9vxGPbuPyvfk6MKkZ8A=="/*new Uint8Array()*/;

    let pubKeyBlob: cryptoFramework.DataBlob = { data: base64.decodeSync(pkData) };
    let priKeyBlob: cryptoFramework.DataBlob = { data: base64.decodeSync(skData) };
    let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024');
    let keyPair = await rsaGenerator.convertKey(pubKeyBlob, priKeyBlob);
    console.info('convertKey success');


    let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(/*base64.decodeSync(message2)*/buffer.from(message, 'utf-8').buffer) };
    let encryptText = await this.rsaEncryptBySegment(keyPair.pubKey, plainText);
    let decryptText = await this.rsaDecryptBySegment(keyPair.priKey, encryptText);
    if (plainText.data.toString() === decryptText.data.toString()) {
      this.outMessage = buffer.from(decryptText.data).toString('utf-8')
      promptAction.showToast({
        message: "成功",
        duration: 2000
      })
    } else {
      promptAction.showToast({
        message: "失败",
        duration: 2000
      })
    }
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS RSA解密问题
485浏览 • 1回复 待解决
HarmonyOS RSA公钥解密
16浏览 • 1回复 待解决
HarmonyOS RSA如何用公钥解密
666浏览 • 0回复 待解决
HarmonyOS RSA私钥加密公钥解密
34浏览 • 1回复 待解决
RSA导入外部密钥实现加解密
813浏览 • 1回复 待解决
HarmonyOS rsa解密速度过于缓慢
659浏览 • 1回复 待解决
HarmonyOSRSA解密相关吗
461浏览 • 0回复 待解决
HarmonyOS 支持rsa使用公钥解密
499浏览 • 1回复 待解决
HarmonyOS 是否有rsa解密的例子?
45浏览 • 1回复 待解决
HarmonyOS Native RSA解密实现咨询
31浏览 • 1回复 待解决
HarmonyOS 关于RSA公钥加解密问题
33浏览 • 1回复 待解决
RSA加密,使用自有私钥解密报错
268浏览 • 1回复 待解决
rsa 公钥解密初始化报错
309浏览 • 1回复 待解决
实现一次非对称RSA非对称加解密
1101浏览 • 1回复 待解决
如何使用RSA公钥加密明文数据
496浏览 • 1回复 待解决