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(() => {
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 =
"MIGfMA0GCSqG****RZI3a31Rh6P8xxxkGkfQIDAQAB";
let skData =
"MIICdgIBADANBgkqhkiGxxx6MKkZ8A==" /*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'));
}
示例参考如下: