Issue with EvpKDF implementation in JavaScript CryptoJS

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);  


New contributor

user25361095 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật