使用C#加密AES加密和使用JavaScript解密
我有一组用于在服务器上加密/解密的功能(C#),但是我想将解密件移植到JavaScript。这是我以前在C#中使用的C#解密函数,with a 示例加密的字符串,密码,密码和IV
我使用cryptojs
在JavaScript解密中遇到麻烦。回来的结果出乎意料地是一个空字符串。
var ciphertext = "0MuDwNoWBFjN/1anszbl0Cxkrwh9ahRwE3c61t7io2c=";
var key = "8beee7ac-42d1-4294-91b8-68cd032cf1e1";
var iv = "9bC_#$/-+%@Kli%1Az=-@qT";
var ciphertextWA = CryptoJS.enc.Hex.parse(ciphertext);
var keyWA = CryptoJS.enc.Utf8.parse(key);
var ivWA = CryptoJS.enc.Utf8.parse(iv);
var ciphertextCP = { ciphertext: ciphertextWA };
var decrypted = CryptoJS.AES.decrypt(
ciphertextCP,
keyWA,
{ iv: ivWA }
);
console.log(decrypted.toString(CryptoJS.enc.Utf8));
I've got a set of functions for encrypting/decrypting on the server (C#), however I'd like to port the decryption piece to Javascript. Here's the C# decryption functions I've used previously in C# with a sample encrypted string, password and IV
I'm having trouble with the JavaScript decryption piece using Cryptojs
. The result that comes back is unexpectedly an empty string.
var ciphertext = "0MuDwNoWBFjN/1anszbl0Cxkrwh9ahRwE3c61t7io2c=";
var key = "8beee7ac-42d1-4294-91b8-68cd032cf1e1";
var iv = "9bC_#$/-+%@Kli%1Az=-@qT";
var ciphertextWA = CryptoJS.enc.Hex.parse(ciphertext);
var keyWA = CryptoJS.enc.Utf8.parse(key);
var ivWA = CryptoJS.enc.Utf8.parse(iv);
var ciphertextCP = { ciphertext: ciphertextWA };
var decrypted = CryptoJS.AES.decrypt(
ciphertextCP,
keyWA,
{ iv: ivWA }
);
console.log(decrypted.toString(CryptoJS.enc.Utf8));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些错误是在IV和密钥的确定中。两者都是使用SHA512的密码得出的。在键的情况下,使用前32个字节(字节0-31),对于IV的情况,在第一个32个字节之后16个字节(字节32-47)。
固定的代码是:
编辑:
正如您正确猜测的那样,问题是由不同的编码引起的(C#代码中的Windows-1252和JavaScript代码中的UTF-8中的Windows-1252)。修复程序是用Windows-1252在JavaScript代码中解码。
cryptojs支持 latin1 (aka iso-88859-1) =“ https://en.wikipedia.org/wiki/windows-1252” rel =“ nofollow noreferrer”> windows-1252 (aka cp1252),s。 encoders 。两个编码之间的差异很小,范围从0x80到0x9f。就像UTF-8一样,都对应于0x00和0x7f之间的ASCII编码。由于原始的明文仅包含ASCII字符,因此没有问题。
但是,新的明文现在包含与UTF-8不兼容的字节序列(因此错误)或其字符与Latin1不同的字节序列(因此不匹配)。
由于CryptoJS不支持Windows-1252,因此必须使用JavaScript在板载均值或其他库进行解码。一种可能性是将cryptojs type
WordArray
转换为更通用的JavaScript类型uint8array
。然后,使用textdecoder(textdecoder() /a>支持大量编码的曲目,可以使用Windows-1252进行解码:
The bugs are in the determination of IV and key. Both are derived from passwords using SHA512. In the case of the key the first 32 bytes (bytes 0-31) are used, in the case of the IV the 16 bytes following the first 32 bytes (bytes 32-47).
The fixed code is:
Edit:
As you correctly guessed, the problem is caused by the different encodings (Windows-1252 in the C# code and UTF-8 in the JavaScript code). The fix is to decode in the JavaScript code with Windows-1252.
CryptoJS supports Latin1 (aka ISO-8859-1) but not Windows-1252 (aka Cp1252), s. encoders. The differences between the two encodings are minor, ranging from 0x80 to 0x9F. Just like UTF-8, both correspond to ASCII encoding in the range between 0x00 and 0x7F. Since the original plaintext contained only ASCII characters, there were no problems.
However, the new plaintext now contains byte sequences that are not compatible with UTF-8 (hence the error) or whose characters differ from Latin1 (hence the mismatches).
Since CryptoJS does not support Windows-1252, the decoding must be done with JavaScript on-board means or another library. One possibility is to convert the CryptoJS type
WordArray
to the more general JavaScript typeUint8Array
. Then, usingTextDecoder()
, which supports a large repertoire of encodings, decoding can be performed using Windows-1252: