CryptoJS encrypt decrypt support PHP , PHP 7.x. See the Pen PHP CryptoJS Encrypt Decrypt by dimas
CryptoJS encrypt decrypt support PHP 5, PHP 7.x.
See the Pen PHP CryptoJS Encrypt Decrypt by dimas lanjaka (@dimaslanjaka) on CodePen.
Code PHP and details variable
[JS]
<?php
/**
* @package https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js
*/
var salt = 'salt'; //salt
var iv = '1111111111111111'; //pass salt minimum length 12 chars
var iterations = '999'; //iterations
/**
* Get key
* @param string passphrase
* @param string salt
*/
function getKey(passphrase, salt) {
var key = CryptoJS.PBKDF2(passphrase, salt, {
hasher: CryptoJS.algo.SHA256,
keySize: 64 / 8,
iterations: iterations
});
return key;
}
/**
* Encrypt function
* @param string passphrase
* @param string plainText
*/
function userJSEncrypt(passphrase, plainText) {
var key = getKey(passphrase, salt);
var encrypted = CryptoJS.AES.encrypt(plainText, key, {
iv: CryptoJS.enc.Utf8.parse(iv)
});
return encrypted.ciphertext.toString(CryptoJS.enc.Base64);
}
/**
* Decrypt function
* @param string passphrase
* @param string encryptedText
*/
function userJSDecrypt(passphrase, encryptedText) {
var key = getKey(passphrase, salt);
var decrypted = CryptoJS.AES.decrypt(encryptedText, key, {
iv: CryptoJS.enc.Utf8.parse(iv)
});
return decrypted.toString(CryptoJS.enc.Utf8);
}
[PHP 5.6++]
<?php
const SALT = "salt"; //salt
const IV = "1111111111111111"; //pass salt minimum length 12 chars or it'll be show warning messages
const ITERATIONS = 999; //iterations
function userPHPEncrypt($passphrase, $plainText)
{
$key = \hash_pbkdf2("sha256", $passphrase, SALT, ITERATIONS, 64);
$encryptedData = \openssl_encrypt(
$plainText,
"AES-256-CBC",
\hex2bin($key),
OPENSSL_RAW_DATA,
IV
);
return \base64_encode($encryptedData);
}
function userPHPDecrypt($passphrase, $encryptedTextBase64)
{
$encryptedText = \base64_decode($encryptedTextBase64);
$key = \hash_pbkdf2("sha256", $passphrase, SALT, ITERATIONS, 64);
$decryptedText = \openssl_decrypt(
$encryptedText,
"AES-256-CBC",
\hex2bin($key),
OPENSSL_RAW_DATA,
IV
);
return $decryptedText;
}
Fix Problems
Reference: Fix openssl encrypt decrypt [PHP]