| @@ -1,55 +1,55 @@ | ||
| 1 | -<?php | |
| 2 | -/** | |
| 3 | - * Helper library for CryptoJS AES encryption/decryption | |
| 4 | - * Allow you to use AES encryption on client side and server side vice versa | |
| 5 | - * | |
| 6 | - * @author BrainFooLong (bfldev.com) | |
| 7 | - * @link https://github.com/brainfoolong/cryptojs-aes-php | |
| 8 | - */ | |
| 9 | -/** | |
| 10 | - * Decrypt data from a CryptoJS json encoding string | |
| 11 | - * | |
| 12 | - * @param mixed $passphrase | |
| 13 | - * @param mixed $jsonString | |
| 14 | - * @return mixed | |
| 15 | - */ | |
| 16 | -function cryptoJsAesDecrypt($passphrase, $jsonString){ | |
| 17 | - $jsondata = json_decode($jsonString, true); | |
| 18 | - try { | |
| 19 | - $salt = hex2bin($jsondata["s"]); | |
| 20 | - $iv = hex2bin($jsondata["iv"]); | |
| 21 | - } catch(Exception $e) { return null; } | |
| 22 | - $ct = base64_decode($jsondata["ct"]); | |
| 23 | - $concatedPassphrase = $passphrase.$salt; | |
| 24 | - $md5 = array(); | |
| 25 | - $md5[0] = md5($concatedPassphrase, true); | |
| 26 | - $result = $md5[0]; | |
| 27 | - for ($i = 1; $i < 3; $i++) { | |
| 28 | - $md5[$i] = md5($md5[$i - 1].$concatedPassphrase, true); | |
| 29 | - $result .= $md5[$i]; | |
| 30 | - } | |
| 31 | - $key = substr($result, 0, 32); | |
| 32 | - $data = openssl_decrypt($ct, 'aes-256-cbc', $key, true, $iv); | |
| 33 | - return json_decode($data, true); | |
| 34 | -} | |
| 35 | -/** | |
| 36 | - * Encrypt value to a cryptojs compatiable json encoding string | |
| 37 | - * | |
| 38 | - * @param mixed $passphrase | |
| 39 | - * @param mixed $value | |
| 40 | - * @return string | |
| 41 | - */ | |
| 42 | -function cryptoJsAesEncrypt($passphrase, $value){ | |
| 43 | - $salt = openssl_random_pseudo_bytes(8); | |
| 44 | - $salted = ''; | |
| 45 | - $dx = ''; | |
| 46 | - while (strlen($salted) < 48) { | |
| 47 | - $dx = md5($dx.$passphrase.$salt, true); | |
| 48 | - $salted .= $dx; | |
| 49 | - } | |
| 50 | - $key = substr($salted, 0, 32); | |
| 51 | - $iv = substr($salted, 32,16); | |
| 52 | - $encrypted_data = openssl_encrypt(json_encode($value), 'aes-256-cbc', $key, true, $iv); | |
| 53 | - $data = array("ct" => base64_encode($encrypted_data), "iv" => bin2hex($iv), "s" => bin2hex($salt)); | |
| 54 | - return json_encode($data); | |
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Helper library for CryptoJS AES encryption/decryption | |
| 4 | + * Allow you to use AES encryption on client side and server side vice versa | |
| 5 | + * | |
| 6 | + * @author BrainFooLong (bfldev.com) | |
| 7 | + * @link https://github.com/brainfoolong/cryptojs-aes-php | |
| 8 | + */ | |
| 9 | +/** | |
| 10 | + * Decrypt data from a CryptoJS json encoding string | |
| 11 | + * | |
| 12 | + * @param mixed $passphrase | |
| 13 | + * @param mixed $jsonString | |
| 14 | + * @return mixed | |
| 15 | + */ | |
| 16 | +function cryptoJsAesDecrypt($passphrase, $jsonString){ | |
| 17 | + $jsondata = json_decode($jsonString, true); | |
| 18 | + try { | |
| 19 | + $salt = hex2bin($jsondata["s"]); | |
| 20 | + $iv = hex2bin($jsondata["iv"]); | |
| 21 | + } catch(Exception $e) { return null; } | |
| 22 | + $ct = base64_decode($jsondata["ct"]); | |
| 23 | + $concatedPassphrase = $passphrase.$salt; | |
| 24 | + $md5 = array(); | |
| 25 | + $md5[0] = md5($concatedPassphrase, true); | |
| 26 | + $result = $md5[0]; | |
| 27 | + for ($i = 1; $i < 3; $i++) { | |
| 28 | + $md5[$i] = md5($md5[$i - 1].$concatedPassphrase, true); | |
| 29 | + $result .= $md5[$i]; | |
| 30 | + } | |
| 31 | + $key = substr($result, 0, 32); | |
| 32 | + $data = openssl_decrypt($ct, 'aes-256-cbc', $key, true, $iv); | |
| 33 | + return json_decode($data, true); | |
| 34 | +} | |
| 35 | +/** | |
| 36 | + * Encrypt value to a cryptojs compatiable json encoding string | |
| 37 | + * | |
| 38 | + * @param mixed $passphrase | |
| 39 | + * @param mixed $value | |
| 40 | + * @return string | |
| 41 | + */ | |
| 42 | +function cryptoJsAesEncrypt($passphrase, $value){ | |
| 43 | + $salt = openssl_random_pseudo_bytes(8); | |
| 44 | + $salted = ''; | |
| 45 | + $dx = ''; | |
| 46 | + while (strlen($salted) < 48) { | |
| 47 | + $dx = md5($dx.$passphrase.$salt, true); | |
| 48 | + $salted .= $dx; | |
| 49 | + } | |
| 50 | + $key = substr($salted, 0, 32); | |
| 51 | + $iv = substr($salted, 32,16); | |
| 52 | + $encrypted_data = openssl_encrypt(json_encode($value), 'aes-256-cbc', $key, true, $iv); | |
| 53 | + $data = array("ct" => base64_encode($encrypted_data), "iv" => bin2hex($iv), "s" => bin2hex($salt)); | |
| 54 | + return json_encode($data); | |
| 55 | 55 | } |