CryptoJS and key/IV length
我对AES密钥和IV长度有疑问。
首先,例如,如果我使用 drugs s>
但是,在
1 2 3 4 5 6 7 8 | 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 }); |
其中key是32个字节,IV是16。CryptoJS需要解析它,在
但这实际上并没有发生。 当我将更大的密钥甚至IV传递给CryptoJS.AES.encrypt()时,它会产生不同的输出。 所以我的问题是,为什么? 在这种情况下,CryptoJS库和OpenSSL有什么区别?
看起来我知道了。
如果您倾向于在使用CryptoJS时传递自定义
以Base64密钥和iv(长度= 22)为例,CryptoJS将其加密为AES-256:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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" |
对于AES-256,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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 |
同样,从我的看到,只有这种用例的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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. |