For the past few days, I’ve been trying to understand how this code fragment in JavaScript with the CryptoJS library works. I want to port it to Python. I’ve written an AES algorithm that replicates the JavaScript error, which occurs because AES accepts keys larger than 256 bits, but the number of rounds changes: for 256 bits / 32 + 6 = 14 rounds, for 512 bits / 32 + 6 = 22 rounds, and so on. I’ve also replicated the EvpKDF algorithm in Python using MD5.
Here’s what I don’t understand:
I run the sha512 hashing function with 1000 iterations, getting a variable ‘i’ of size 512 bits.
function encrypt(secret, passwrd) {
for (var i = CryptoJS.SHA512(passwrd), n = 0; n < 1000; n++) i = CryptoJS.SHA512(i);
console.log("1000 SHA-512:n", i.toString());
Then in the code, there are parameters: AES key size is 32 * 4 = 128 bytes (1024 bits), and the number of iterations for the EvpKDF algorithm is 1000. As I understand it, the EvpKDF algorithm is executed inside CryptoJS.AES.encrypt.
CryptoJS.algo.AES.keySize = 32;
CryptoJS.algo.EvpKDF.cfg.iterations = 1000;
So, the EvpKDF algorithm with the value i.toString() undergoes 1000 iterations and is expanded to 128 bytes, which AES uses as the secret code for encrypting the ‘secret’ value.
var iv = CryptoJS.SHA256(passwrd).toString().substring(0, 32);
var ciphertext = CryptoJS.AES.encrypt(secret, i.toString(), {
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
iv: CryptoJS.enc.Utf8.parse(iv)
});
return ciphertext;
}
I’ve extracted the operation of the EvpKDF function from internal execution in AES and passed the value to AES with a new variable, intending to replicate this in Python, but the results don’t match. Please help me understand what happens with the password i.toString(). Perhaps it’s padded with zero bytes before undergoing 1000 iterations of EvpKDF.
This is the original code I want to replicate in Python.
function hex2a(t) {
for (var e = t.toString(), i = "", n = 0; n < e.length && "00" !== e.substr(n, 2); n += 2) i += String.fromCharCode(parseInt(e.substr(n, 2), 16));
return i;
}
function decoder(secret, passwrd) {
for (var i = CryptoJS.SHA512(passwrd), n = 0; n < 1000; n++) i = CryptoJS.SHA512(i);
CryptoJS.algo.AES.keySize = 32;
CryptoJS.algo.EvpKDF.cfg.iterations = 1000;
var r = CryptoJS.AES.decrypt(secret, i.toString());
return (out = hex2a(r)), out;
}
function encrypt(secret, passwrd) {
for (var i = CryptoJS.SHA512(passwrd), n = 0; n < 1000; n++) i = CryptoJS.SHA512(i);
console.log("11512 SHA-512:n", i.toString());
CryptoJS.algo.AES.keySize = 32;
CryptoJS.algo.EvpKDF.cfg.iterations = 1000;
var iv = CryptoJS.SHA256(passwrd).toString().substring(0, 32);
console.log("IV:n", iv);
var ciphertext = CryptoJS.AES.encrypt(secret, i.toString(), {
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
iv: CryptoJS.enc.Utf8.parse(iv)
});
return ciphertext;
}
var ciphertext = encrypt('1231231321231231kjfsgbfjdskgbjkfd', 'a111111111111111111111111111111b');
var decryptedData = decoder(ciphertext, 'a111111111111111111111111111111b');
console.log("Ciphertext:n", ciphertext.toString());
console.log("Plaintext:n", decryptedData);
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script>
And this code separates EvpKDF, with encryption moved out; decryption remains unchanged for verification.
function hex2a(t) {
for (var e = t.toString(), i = "", n = 0; n < e.length && "00" !== e.substr(n, 2); n += 2) i += String.fromCharCode(parseInt(e.substr(n, 2), 16));
return i;
}
function decoder(secret, passwrd) {
for (var i = CryptoJS.SHA512(passwrd), n = 0; n < 1000; n++) i = CryptoJS.SHA512(i);
CryptoJS.algo.AES.keySize = 32;
CryptoJS.algo.EvpKDF.cfg.iterations = 1000;
var r = CryptoJS.AES.decrypt(secret, i.toString());
return (out = hex2a(r)), out;
}
function encrypt(secret, passwrd) {
for (var i = CryptoJS.SHA512(passwrd), n = 0; n < 1000; n++) i = CryptoJS.SHA512(i);
console.log("11512 SHA-512:n", i.toString());
CryptoJS.algo.EvpKDF.cfg.keySize = 32;
var keyEvpKDF = CryptoJS.EvpKDF(i, {iterations: 1000});
var wer = keyEvpKDF.toString();
console.log("EVPKDF:n", wer);
var iv = CryptoJS.SHA256(passwrd).toString().substring(0, 32);
var ciphertext = CryptoJS.AES.encrypt(secret, wer.toString(), {
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
iv: CryptoJS.enc.Utf8.parse(iv)
});
return ciphertext;
}
var ciphertext = encrypt('1231231321231231kjfsgbfjdskgbjkfd', 'a111111111111111111111111111111b');
var decryptedData = decoder(ciphertext, 'a111111111111111111111111111111b');
console.log("Ciphertext:n", ciphertext.toString());
console.log("Plaintext:n", decryptedData);
user25361095 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.