我尝试用AES加密ArrayBuffer,因此将其转换为wordArray,然后转换为字符串:
private encrypt(file: ArrayBuffer, key: string): string {
const wordArray = CryptoJS.lib.WordArray.create(file);
const str = CryptoJS.enc.Hex.stringify(wordArray);
console.log(str); //6920616d206120737472696e67
return CryptoJS.AES.encrypt(str, key).toString();
}现在我想解密回一个ArrayButter,但是打印的字符串甚至不匹配:
private decrypt(file: string, key: string) {
const decrypted = CryptoJS.AES.decrypt(file, key);
console.log(decrypted.toString()); //3639323036313664323036313230373337343732363936653637
}我想我搞砸了一些步骤,但我不知道在哪里。
更新:我需要将字符串转换为utf以生成一个wordarray:
private decrypt(file: string, key: string) {
const decrypted = CryptoJS.AES.decrypt(file, key);
const str = decrypted.toString(CryptoJS.enc.Utf8);
const wordArray = CryptoJS.enc.Hex.parse(str);
}现在,我离再次将它转换为ArrayBuffer只差一步了
发布于 2022-09-16 03:21:49
function CryptJsWordArrayToUint8Array(wordArray) {
const l = wordArray.sigBytes;
const words = wordArray.words;
const result = new Uint8Array(l);
var i = 0 /*dst*/, j = 0 /*src*/;
while (true) {
// here i is a multiple of 4
if (i == l) {
break;
}
var w = words[j++];
result[i++] = (w & 0xff000000) >>> 24;
if (i == l) {
break;
}
result[i++] = (w & 0x00ff0000) >>> 16;
if (i == l) {
break;
}
result[i++] = (w & 0x0000ff00) >>> 8;
if (i == l) {
break;
}
result[i++] = (w & 0x000000ff);
}
return result;
}https://stackoverflow.com/questions/51127116
复制相似问题