2 回复
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { buffer, util } from '@kit.ArkTS';
@Entry
@Component
struct TDes3Crypto {
@State message: string = '点击开始';
build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(() => {
DES3Crypto()
})
}
.width('100%')
}
.height('100%')
}
}
// 加密消息
function encryptMessage(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) {
let cipher = cryptoFramework.createCipher('3DES192|CBC|PKCS5');
let iv = genIvParamsSpec();
cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, iv);
let encryptData = cipher.doFinalSync(plainText);
return encryptData;
}
// 解密消息
function decryptMessage(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) {
let decoder = cryptoFramework.createCipher('3DES192|CBC|PKCS5');
let iv = genIvParamsSpec();
decoder.initSync(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, iv);
let decryptData = decoder.doFinalSync(cipherText);
return decryptData;
}
function genSymKeyByData(symKeyData: Uint8Array) {
let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData };
let symGenerator = cryptoFramework.createSymKeyGenerator('3DES192');
let symKey = symGenerator.convertKeySync(symKeyBlob);
console.info('convertKey success');
return symKey;
}
function genIvParamsSpec() {
let iv = new Int8Array([-45, -21, 108, 107, 31, 7, 62, 12, 51, -39, 87, 89, -18, 48, 3, 23]/*buffer.from('01234567', 'utf-8').buffer*/);
console.error("AAAAAAIV"+iv)
let dataIv = new Uint8Array(iv);
let ivBlob: cryptoFramework.DataBlob = { data: dataIv };
let ivParamsSpec: cryptoFramework.IvParamsSpec = {
algName: "IvParamsSpec",
iv: ivBlob
};
return ivParamsSpec;
}
function DES3Crypto() {
let base64 = new util.Base64Helper()
let key = "DDEFFeEFe.,;Efffsadefewq";
let u8a = new Uint8Array(buffer.from(key, 'utf-8').buffer)
let symKey = genSymKeyByData(u8a);
let message = "123456";
let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) };
let encryptText = encryptMessage(symKey, plainText);
let base64en = base64.encodeToStringSync(encryptText.data)
console.info('decrypt ok '+ base64en) ;
let s = ""
console.error("aaaaasssss"+base64.decodeSync(s))
let encryptoMessage = base64.decodeSync(s);
let encryptoMes: cryptoFramework.DataBlob = { data: new Uint8Array(encryptoMessage) };
let decryptText = decryptMessage(symKey, encryptText );
if (plainText.data.toString() === decryptText.data.toString()) {
console.info('decrypt ok');
console.info('decrypt plainText: ' + buffer.from(decryptText.data).toString('utf-8'));
} else {
console.error('decrypt failed');
}
}
更多关于HarmonyOS 鸿蒙Next DES解密的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS(鸿蒙)系统中进行DES(Data Encryption Standard,数据加密标准)解密操作时,通常涉及使用系统提供的加密库或API来实现。以下是针对HarmonyOS环境进行DES解密的基本步骤和示例代码片段:
基本步骤
- 准备密钥和密文:确保你有正确的DES密钥和需要解密的密文数据。
- 配置解密参数:设置解密所需的参数,如模式(ECB、CBC等)和填充方式(PKCS5Padding等)。
- 调用解密API:使用HarmonyOS提供的加密库中的解密函数,传入密钥、密文和参数进行解密。
- 处理解密结果:获取解密后的明文数据,并进行必要的处理或验证。
示例代码片段(伪代码)
// 假设已包含必要的加密库头文件
#include <encryption_library.h>
// 密钥和密文(示例)
byte key[8] = {/* DES密钥 */};
byte ciphertext[/* 密文长度 */];
// 解密参数
DecryptParam param;
param.mode = DES_CBC_MODE;
param.padding = PKCS5_PADDING;
// 执行解密
byte plaintext[/* 解密后明文的最大长度 */];
int plaintext_len = DecryptDES(key, ciphertext, plaintext, param);
// 处理解密后的明文
// ...
注意:上述代码为伪代码,实际实现需根据HarmonyOS提供的加密API进行调整。如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html