brix / crypto-js

JavaScript library of crypto standards.
Other
15.82k stars 2.38k forks source link

i want to know how aes create key and iv from salt and passphrase #502

Open fancy45daddy opened 1 month ago

fancy45daddy commented 1 month ago

from the doc var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase"); ​ encrypted.key

"74eb593087a982e2a6f5dded54ecd96d1fd0f3d44a58728cdcd40c55227522223 "; ​ encrypted.iv "7781157e2629b094f0e3dd48c4d786115"; ​ encrypted.salt "7a25f9132ec6a8b34"; ​ encrypted.ciphertext "73e54154a15d1beeb509d9e12f1e462a0"; ​ encrypted "U2FsdGVkX1+iX5Ey7GqLND5UFUoV0b7rUJ2eEvHkYqA=";

i have no idea how to get key and iv, then i ask chatgpt, it give me some code

// Define passphrase and salt const passphrase = 'your-passphrase'; const salt = CryptoJS.enc.Hex.parse('your-salt-in-hex'); // Salt should be in a suitable format

// Derive key and IV using PBKDF2 const keySize = 256 / 32; // Key size in words const ivSize = 128 / 32; // IV size in words const iterations = 1000; // Number of iterations

// Derive key const key = CryptoJS.PBKDF2(passphrase, salt, { keySize: keySize + ivSize, // Total size for key and IV iterations: iterations });

// Extract key and IV const derivedKey = CryptoJS.lib.WordArray.create(key.words.slice(0, keySize)); // Extract key const iv = CryptoJS.lib.WordArray.create(key.words.slice(keySize)); // Extract IV

but when i input the passphrase and salt in chatgpt code. it does not create the same key as that in encrypted.key and encrypted.iv. plèase help me to understand how to get key and iv

nikhiltekwani09 commented 3 weeks ago

@fancy45daddy were you able to figure this out I also want the same information to write the code in native module

grandmastergainz commented 3 weeks ago

How come you’re doing that ?Sent from my iPhoneOn Sep 24, 2024, at 3:51 AM, nikhiltekwani09 @.***> wrote: @fancy45daddy were you able to figure this out I also want the same information to write the code in native module

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you are subscribed to this thread.Message ID: @.***>

nikhiltekwani09 commented 3 weeks ago

I m not sure right now, till I have the complete information which @fancy45daddy is requesting

Aobanana-chan commented 2 weeks ago

I am trying to decrypt encrypted content using a backend in a different language, and I solved this problem today. When you enter the Secret Passphrase, crypto-js consistently uses AES-256-CBC for encryption. The first 16 bytes of the encrypted content are related to the Salt. The first 8 bytes are a verification flag, which is fixed as 'Salted__', and the next 8 bytes are the actual Salt. Then, crypto-js simulates the OpenSSL EVP_bytesToKey function (evpkdf) and performs an MD5 hash on the Key and Salt. For the specific implementation, please refer to the compute function in evpkdf.js. This calculates a 48-byte data output, where the first 32 bytes are the key and the last 16 bytes are the IV.