cryptoJs просто с нюансами, на основе соли генерирует IV + encrypt key)
<?php $plaintext = 'My secret message 1234'; $password = '3sc3RLrpd17'; $method = 'aes-256-cbc'; $encrypted = encrypt_aes_cryptoJS($method, $password, $plaintext); $decrypted = decrypt_aes_cryptoJS($method, $password, $encrypted); echo 'plaintext=' . $plaintext . "\n"; echo 'cipher=' . $method . "\n"; echo 'encrypted to: ' . $encrypted . "\n"; echo 'decrypted to: ' . $decrypted . "\n\n"; function encrypt_aes_cryptoJS($cipher, $password, $plaintext) { $iv_len = openssl_cipher_iv_length($cipher); $key_len = openssl_cipher_key_length($cipher); $cryptoJS_magic = "Salted__"; $salt = random_bytes(8); list('key'=>$key, 'iv'=>$iv) = evpBytesToKey($password, $salt, $key_len, $iv_len); $encrypted_raw = openssl_encrypt($plaintext, $cipher, $key, OPENSSL_RAW_DATA, $iv); $full = $cryptoJS_magic . $salt . $encrypted_raw; return base64_encode($full); }; function decrypt_aes_cryptoJS($cipher, $password, $payloadB64) { $cryptoJS_magic = "Salted__"; $data = base64_decode($payloadB64, true); if ($data === false) { throw new Exception("Invalid base64 payload"); } if (substr($data, 0, 8) !== $cryptoJS_magic) { throw new Exception("missing cryptoJS Salted__ prefix"); } $salt = substr($data, 8, 8); $keyLen = openssl_cipher_key_length($cipher); $ivLen = openssl_cipher_iv_length($cipher); list('key' => $key, 'iv' => $iv) = evpBytesToKey($password, $salt, $keyLen, $ivLen); $encrypted_raw = substr($data, 16); $decrypted = openssl_decrypt($encrypted_raw, $cipher, $key, OPENSSL_RAW_DATA, $iv); if ($decrypted === false) { throw new Exception("Decryption failed"); } return $decrypted; } function evpBytesToKey($password, $salt, $key_len = 32, $iv_len = 16) { $data = ''; $prev = ''; $required = $key_len + $iv_len; while (strlen($data) < $required) { // D_i = MD5( D_{i-1} || password || salt ) $prev = md5($prev . $password . $salt, true); $data .= $prev; } return [ 'key' => substr($data, 0, $key_len), 'iv' => substr($data, $key_len, $iv_len), ]; }8b5545d7-bf3d-448e-90d8-b5022a9a07c7-image.png



