作者:有你世界就很美_484 | 来源:互联网 | 2023-06-29 08:15
由于@ sami-hult,刚刚学习了如何加密二进制文件-我发现我也无法解密!我收到协议缓冲区消息并退出感兴趣的领域。纯文本是一个32字节的值。我可以验证它在远端被加密为32字节的有效负载,并且与发送时完全一样。
在这一点上,我正在努力找出下一部分。当我尝试将值缠绕为字符串时,我似乎做错了(再次)。
console.log('this.keyPairClient.public = ' + this.keyPairClient.public);
var iv_hexString = this.buf2hex(this.deviceRandom);
var key_hexString = this.buf2hex(this.shared_key);
var key = CryptoJS.enc.Hex.parse(key_hexString);
var iv = CryptoJS.enc.Hex.parse(iv_hexString);
console.log('iv = ' + iv);
console.log('key = ' + key);
const arrayToString = arr => arr.reduce((str,code) => str + String.fromCharCode(code),'');
const encrypted = new Uint8Array(this.device_Verfify_Data);
console.log('encrypted = ' + this.buf2hex( encrypted )); // correct! 32 bytes as sent
console.log('encrypted string length = ' + encrypted.length );
const encryptedString = arrayToString(encrypted);
// Decrypt.
const decryptedString = CryptoJS.AES.decrypt(encryptedString,key,{
mode: CryptoJS.mode.CTR,iv: iv,padding: CryptoJS.pad.NoPadding
}).toString(CryptoJS.enc.Latin1);
// const decryptedArray8 = Uint8Array.from(Buffer.from(decryptedString,'latin1'));
console.log('decrypted string length = ' + decryptedString.length );
console.log('decryptedString = ' + decryptedString);
const decryptedArray8 = this.stringToBytes(decryptedString);
console.log('decryptedArray8 = ' + decryptedArray8);
console.log(Array.apply([],decryptedArray8).join(","));
console.log('decryptedArray8 = ' + this.buf2hex( decryptedArray8 ));
buf2hex(buffer) { // buffer is an ArrayBuffer
return Array.prototype.map.call(new Uint8Array(buffer),x => ('00' + x.toString(16)).slice(-2)).join('');
}
日志如下:
[ng] [console.log]: "this.keyPairClient.public = 194,5,240,229,198,49,173,141,47,89,8,93,132,155,193,249,174,125,124,152,142,56,85,6,42,105,143,78,65"
[ng] [console.log]: "iv = ab3b3463be9f799c64ce9d556c6d4863"
[ng] [console.log]: "key = 7a8d39e4ec63d2a111c368e8b0b1681924e5881a4bb7c35766df39b87bd085f7"
[ng] [console.log]: "encrypted = f2cfc754c8329691b54d046c54b27f077fd42663171aeb3935f747de70918d36"
[ng] [console.log]: "encrypted string length = 32"
[ng] [console.log]: "decrypted string length = 24"
[ng] [console.log]: "decryptedString = ÿ7>úÇlú«P\u0007:@\u000eaMtqÀù;ÃÓy"
[ng] [console.log]: "decryptedArray8 = [object ArrayBuffer]"
[ng] [console.log]: ""
[ng] [console.log]: "decryptedArray8 = 93ff373efac76cfaab50073a400e614d7471c0f93bc3d379"
加密的= f2cfc ...与发送的二进制值匹配:
D (15209) security1: Device verify data:
D (15209) security1: f2 cf c7 54 c8 32 96 91 b5 4d 04 6c 54 b2 7f 07
D (15219) security1: 7f d4 26 63 17 1a eb 39 35 f7 47 de 70 91 8d 36
但是很明显,由于只有24个字节出现,所以下一部分会出错。它与所期望的值(this.keyPairClient.public)完全不同。噢亲爱的!有什么想法吗?