HarmonyOS 鸿蒙Next RSA如何使用现有的公私钥进行分段加解密

发布于 1周前 作者 h691938207 来自 鸿蒙OS

HarmonyOS 鸿蒙Next RSA如何使用现有的公私钥进行分段加解密

RSA分段加密这边是通过生成器随机生成的密钥,进行加解密,有没有使用现有的公私钥(字符串)进行RSA加解密的demo
 

2 回复

RSA分段加密制定公私钥参考以下代码demo实例

import { cryptoFramework } from '[@kit](/user/kit).CryptoArchitectureKit';

import { buffer, util } from '[@kit](/user/kit).ArkTS';

import promptAction from '[@ohos](/user/ohos).promptAction'

[@Entry](/user/Entry)

[@Component](/user/Component)

struct Crypto {

 [@State](/user/State) message: string = '点击开始';

 [@State](/user/State) outMessage: string = ''

 build() {

   Column() {

     Text(this.message)

       .fontSize(50)

       .fontWeight(FontWeight.Bold)

       .onClick(() => {

         rsaEncryptLongMessage()

       })

     Text(this.outMessage)

       .fontSize(14)

       .fontWeight(FontWeight.Normal)

       .width('80%')

   }

   .height('100%')

   .width('100%')

   .justifyContent(FlexAlign.Center)

   .alignItems(HorizontalAlign.Center)

 }

}

// 分段加密消息

function rsaEncryptBySegment(pubKey: cryptoFramework.PubKey, plainText: cryptoFramework.DataBlob) {

 let cipher = cryptoFramework.createCipher('RSA1024|PKCS1');

 cipher.initSync(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 = cipher.doFinalSync(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;

}

// 分段解密消息

function rsaDecryptBySegment(priKey: cryptoFramework.PriKey, cipherText: cryptoFramework.DataBlob) {

 let decoder = cryptoFramework.createCipher('RSA1024|PKCS1');

 decoder.initSync(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字节进行拆分解密,得到原文后进行拼接

   // 将原文按117字符进行拆分,循环调用doFinal进行加密,使用1024bit密钥时,每次加密生成128字节长度的密文

   let updateOutput = decoder.doFinalSync(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;

}

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

 let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKeyData };

 let priKeyBlob: cryptoFramework.DataBlob = { data: priKeyData };

 let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024|PRIMES_2');

 let keyPair = rsaGenerator.convertKeySync(pubKeyBlob, priKeyBlob);

 console.info('convertKey success');

 return keyPair;

}

function 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 =

   "xxx";

 let skData =

   "xxx" /*new Uint8Array()*/;



 let pubKeyBlob = /*new Uint8Array()*/ base64.decodeSync(pkData);

 let priKeyBlob = base64.decodeSync(skData);

 let keyPair = genKeyPairByData(pubKeyBlob, priKeyBlob);

 let plainText: cryptoFramework.DataBlob =

   { data: new Uint8Array(/*base64.decodeSync(message2)*/buffer.from(message, 'utf-8').buffer) };

 let encryptText = rsaEncryptBySegment(keyPair.pubKey, plainText);

 let decryptText = rsaDecryptBySegment(keyPair.priKey, encryptText);

 console.info('decrypt plainText: ' + buffer.from(decryptText.data).toString('utf-8'));

}

更多关于HarmonyOS 鸿蒙Next RSA如何使用现有的公私钥进行分段加解密的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next系统中,使用RSA进行分段加解密(即分段加密和分段解密)的操作通常涉及以下几个步骤:

  1. 分段准备:首先,确定要加密或解密的数据大小,将其分割成小于RSA密钥长度的数据块(通常减去填充长度)。

  2. 加密过程

    • 对每个数据块使用RSA公钥进行加密。
    • 确保每个数据块在加密前进行了适当的填充,以符合RSA加密的标准(如PKCS#1填充)。
  3. 解密过程

    • 对每个加密后的数据块使用RSA私钥进行解密。
    • 去除填充后,按顺序拼接解密后的数据块,恢复原始数据。
  4. 注意事项

    • 确保所有分段加密或解密使用的密钥对一致。
    • 分段处理需要记录或传递分段信息(如顺序、总数等),以确保解密时能正确重组。
  5. API调用

    • HarmonyOS提供了相应的API接口用于RSA加解密,具体使用可参考鸿蒙系统官方文档或示例代码。

示例代码(伪代码):

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部