Javascript CryptoJS 和密钥/IV 长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29512858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CryptoJS and key/IV length
提问by Damaged Organic
I have question about AES key and IV length.
我对 AES 密钥和 IV 长度有疑问。
First of all, if, for example, I'm using drugsOpenSSLextension and openssl_encrypt()method, I can clearly see that key for 256-bit AESshould be 32bytes, and IV throws warning if it's different than 16bytes. I can understand that, and everything is fine.
首先,例如,如果我使用的是药物OpenSSL扩展名和openssl_encrypt()方法,我可以清楚地看到256 位 AES 的密钥应该是32字节,如果它与16字节不同,IV 会抛出警告。我能理解,一切都很好。
However, in CryptoJSlibrary the key and IV length is frustrating. This is some example:
但是,在CryptoJS库中,密钥和 IV 长度令人沮丧。这是一些例子:
var text = "test",
key = "us5N0PxHAWuIgb0/Qc2sh5OdWBbXGady",
iv = "zAvR2NI87bBx746n";
key = CryptoJS.enc.Base64.parse(key);
iv = CryptoJS.enc.Base64.parse(iv);
crypted = CryptoJS.AES.encrypt(text, key, { iv: iv });
where key is 32bytes, IV is 16. CryptoJS requires to parse it, and after CryptoJS.enc.Base64.parse()I get 48 and 24 bytes accordingly. I expect that those values will get truncated to required 256-bit AESlength, and further expansion to n bytes will be irrelevant, and so resulting ciphertext will be the same.
其中 key 是32字节, IV 是16。CryptoJS 需要解析它,在CryptoJS.enc.Base64.parse()我得到 48 和 24 字节之后。我希望这些值将被截断为所需的256 位 AES长度,并且进一步扩展到 n 字节将无关紧要,因此生成的密文将是相同的。
But that's not actually happening. When I pass to CryptoJS.AES.encrypt() larger size key and evenIV, it's producing different output. So my question is, why? What is the difference between CryptoJSlibrary and OpenSSLin this case?
但这实际上并没有发生。当我传递给 CryptoJS.AES.encrypt() 更大尺寸的密钥甚至IV 时,它会产生不同的输出。所以我的问题是,为什么?在这种情况下,CryptoJS库和OpenSSL 有什么区别?
回答by Damaged Organic
Looks like I've got it.
看起来我已经得到了。
If you tend to pass custom keyand IVin using CryptoJS, make sure that (assuming that CryptoJS.enc.Base64.parse()gives HEXstring, which is used in CryptoJS.AES.encrypt()).
如果您倾向于通过 customkey并IV使用CryptoJS,请确保(假设CryptoJS.enc.Base64.parse()给出了在 中使用的十六进制字符串CryptoJS.AES.encrypt())。
Taking this example, with Base64key and iv (length=22), which CryptoJSencrypts as AES-256:
以这个例子为例,使用Base64密钥和 iv(长度=22),CryptoJS 将其加密为AES-256:
var message = "some_secret_message";
var key = "6Le0DgMTAAAAANokdEEial"; //length=22
var iv = "mHGFxENnZLbienLyANoi.e"; //length=22
key = CryptoJS.enc.Base64.parse(key);
//key is now e8b7b40e031300000000da247441226a, length=32
iv = CryptoJS.enc.Base64.parse(iv);
//iv is now 987185c4436764b6e27a72f2fffffffd, length=32
var cipherData = CryptoJS.AES.encrypt(message, key, { iv: iv });
var data = CryptoJS.AES.decrypt(cipherData, key, { iv: iv });
//data contains "some_secret_message"
Length of the keyis 32 bytes for AES-256. (16 bytes if you want to get AES-128. If more, CryptoJS will switch to higher key length). In other case on decrypt you will get an empty message. Example:
AES-256 的长度key为 32 字节。(如果你想获得AES-12816 个字节。如果更多,CryptoJS 将切换到更高的密钥长度)。在解密的其他情况下,您将收到一条空消息。例子:
var message = "some_secret_message";
var key = "6Le0DgMTAAAAANokdEEial1"; //length=23
var iv = "mHGFxENnZLbienLyANoi.e"; //length=22
key = CryptoJS.enc.Base64.parse(key); // length = 17 bytes
//key is now e8b7b40e031300000000da247441226a5d, length=34 (hex encoded)
iv = CryptoJS.enc.Base64.parse(iv); // length = 16 bytes
//iv is now 987185c4436764b6e27a72f2fffffffd, length=32 (hex encoded)
var cipherData = CryptoJS.AES.encrypt(message, key, { iv: iv });
var data = CryptoJS.AES.decrypt(cipherData, key, { iv: iv });
//data contains "" - an empty string
Also, from what I can see, only x % 8 == 0bytes of such use case gives valid result.
此外,据我所知,只有x % 8 == 0此类用例的字节才能给出有效结果。
Length of IVshould be 22 bytes (when Base64 encoded), and while transforming with CryptoJS.enc.Base64.parse()you will get 16 bytes (32 hex encoded), which is max for AES-256block size. Everything more than that will get truncated.
的长度IV应为 22 字节(Base64 编码时),而与CryptoJS.enc.Base64.parse()您一起转换时将获得 16 字节(32 位十六进制编码),这是AES-256块大小的最大值。除此之外的所有内容都将被截断。
var message = "some_secret_message";
var key = "6Le0DgMTAAAAANokdEEial"; //length=22
var iv = "mHGFxENnZLbienLyANoi.e"; //length=22
key = CryptoJS.enc.Base64.parse(key); // length=16 bytes
//key is now e8b7b40e031300000000da247441226a5d, length=32 (hex encoded)
iv = CryptoJS.enc.Base64.parse(iv); // length=16 bytes
//iv is now 987185c4436764b6e27a72f2fffffffd, length=32 (hex encoded)
var cipherData = CryptoJS.AES.encrypt(message, key, { iv: iv });
var key = "6Le0DgMTAAAAANokdEEial"; //length=22
var iv = "mHGFxENnZLbienLyANoi.e123"; //length=25
key = CryptoJS.enc.Base64.parse(key); // length = 16 bytes
//key is now e8b7b40e031300000000da247441226a5d, length=32 (hex encoded)
iv = CryptoJS.enc.Base64.parse(iv); // length = 18 bytes
//iv is now 987185c4436764b6e27a72f2fffffffded76, length=36 (hex encoded)
var data = CryptoJS.AES.decrypt(cipherData, key, { iv: iv }); //data contains "some_secret_message", so additional "123" in IV is irrelevant.

