HarmonyOS 鸿蒙Next 3des加密中文字符串结果不一致,英文数字正确
HarmonyOS 鸿蒙Next 3des加密中文字符串结果不一致,英文数字正确
使用如下方式进行字符串加密。英文结果与服务器后台和安卓前台一致,字符串中有中文时,结果不一致
let cipherAlgName = ‘3DES192|CBC|PKCS5’;
let symKeyGenerator = cryptoFramework.createSymKeyGenerator(‘3DES192’)
let cipherAlgName = ‘3DES192|CBC|PKCS5’;
let symKeyGenerator = cryptoFramework.createSymKeyGenerator(‘3DES192’)
2 回复
import { cryptoFramework } from '[@kit](/user/kit).CryptoArchitectureKit';
import { buffer, util } from '[@kit](/user/kit).ArkTS';
[@Entry](/user/Entry)
[@Component](/user/Component)
struct TDes3Crypto {
[@State](/user/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('xxx');
let symKey = symGenerator.convertKeySync(symKeyBlob);
console.info('convertKey success');
return symKey;
}
function genIvParamsSpec() {
let iv = new Uint8Array(buffer.from('xxx', 'utf-8').buffer);
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 = "xxx";
let u8a = new Uint8Array(buffer.from(key, 'utf-8').buffer)
let symKey = genSymKeyByData(u8a);
let str: string = "12345去问人体qwert";
let message = "123456";
let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(str, 'utf-8').buffer) };
let encryptText = encryptMessage(symKey, plainText);
let base64en = base64.encodeToStringSync(encryptText.data)
console.info('decrypt ok '+ base64en) ;
let decryptText = decryptMessage(symKey, encryptText);
console.info('decrypt ok');
console.info('decrypt plainText: ' + buffer.from(decryptText.data).toString('utf-8'));
}
也可以使用在线加解密验证 【 https://the-x.cn/cryptography/TripleDes.aspx 】
作为IT专家,针对HarmonyOS 鸿蒙Next 3DES加密中文字符串结果不一致的问题,这里给出专业解答:
HarmonyOS鸿蒙Next开发中,3DES加解密是一项重要的安全功能。如果在3DES加密中遇到中文字符串结果不一致,而英文和数字加密结果正确的情况,这通常是由于编码问题导致的。中文字符在加密前需要被正确地转换成字节流,否则在加密过程中可能会出现错误,导致解密后的结果与原始中文字符串不一致。
要解决这个问题,可以确保在加密前将中文字符串转换为UTF-8编码的字节流。HarmonyOS提供了相应的API来处理这种转换。例如,可以使用buffer.from(str, 'utf-8').buffer
将字符串转换为字节流,然后再进行3DES加密。
此外,还需要确保加密和解密过程中使用的密钥、算法参数(如分组模式和填充模式)等完全一致,否则也会导致加密结果不一致。
如果上述方法仍然无法解决问题,建议检查加密库的实现和API调用是否正确,或者尝试使用其他加密库进行加密和解密操作。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html