dimaslanjaka /
universal-framework
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Crypto; |
||
| 4 | |||
| 5 | const SALT = 'salt'; //salt |
||
| 6 | const IV = '1111111111111111'; //pass salt minimum length 12 chars or it'll be show warning messages |
||
| 7 | const ITERATIONS = 999; //iterations |
||
| 8 | |||
| 9 | class crypt |
||
| 10 | { |
||
| 11 | public static $iv = '1111111111111111'; |
||
| 12 | public static $iteration = '999'; |
||
| 13 | public static $salt = 'salt'; |
||
| 14 | public static $opensslrawdata = 1; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Encrypt. |
||
| 18 | * |
||
| 19 | * @see https://web-manajemen.blogspot.com/2019/07/phpjs-cryptojs-encrypt-decrypt.html |
||
| 20 | * |
||
| 21 | * @param string $passphrase |
||
| 22 | * @param string $plainText |
||
| 23 | * |
||
| 24 | * @return string |
||
| 25 | */ |
||
| 26 | public static function EN($plainText, $passphrase = 'dimaslanjaka') |
||
| 27 | { |
||
| 28 | $key = \hash_pbkdf2('sha256', $passphrase, SALT, ITERATIONS, 64); |
||
| 29 | $encryptedData = \openssl_encrypt($plainText, 'AES-256-CBC', \hex2bin($key), OPENSSL_RAW_DATA, IV); |
||
| 30 | |||
| 31 | return \base64_encode($encryptedData); |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Decrypt. |
||
| 36 | * |
||
| 37 | * @see https://web-manajemen.blogspot.com/2019/07/phpjs-cryptojs-encrypt-decrypt.html |
||
| 38 | * |
||
| 39 | * @param string $passphrase |
||
| 40 | * @param string $plaintext |
||
| 41 | * |
||
| 42 | * @return string |
||
| 43 | */ |
||
| 44 | public static function DE($plaintext, $passphrase = 'dimaslanjaka') |
||
| 45 | { |
||
| 46 | $encryptedText = \base64_decode($plaintext); |
||
| 47 | $key = \hash_pbkdf2('sha256', $passphrase, self::$salt, self::$iteration, 64); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 48 | $decryptedText = \openssl_decrypt($encryptedText, 'AES-256-CBC', \hex2bin($key), OPENSSL_RAW_DATA, self::$iv); |
||
| 49 | |||
| 50 | return $decryptedText; |
||
| 51 | } |
||
| 52 | |||
| 53 | public function encrypt($passphrase, $plainText) |
||
| 54 | { |
||
| 55 | $key = \hash_pbkdf2('sha256', $passphrase, SALT, ITERATIONS, 64); |
||
| 56 | $encryptedData = \openssl_encrypt($plainText, 'AES-256-CBC', \hex2bin($key), OPENSSL_RAW_DATA, IV); |
||
| 57 | |||
| 58 | return \base64_encode($encryptedData); |
||
| 59 | } |
||
| 60 | |||
| 61 | public function decrypt($passphrase, $plaintext) |
||
| 62 | { |
||
| 63 | $encryptedText = \base64_decode($plaintext); |
||
| 64 | $key = \hash_pbkdf2('sha256', $passphrase, SALT, ITERATIONS, 64); |
||
| 65 | $decryptedText = \openssl_decrypt($encryptedText, 'AES-256-CBC', \hex2bin($key), OPENSSL_RAW_DATA, IV); |
||
| 66 | |||
| 67 | return $decryptedText; |
||
| 68 | } |
||
| 69 | } |
||
| 70 |