Skip to content

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

@ghost

Description

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions