Last active
May 16, 2022 15:08
-
-
Save joshhartman/1009456 to your computer and use it in GitHub Desktop.
Rijndael 256-bit Encryption Function (ECB)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Define an strong encryption key to use with MCRYPT | |
| // Note: The same encryption key used to encrypt the data must be used to decrypt the data correctly | |
| define('ENCRYPTION_KEY', 'ayf9y0fuY3WN27bha45c65g5pw8FEep7'); | |
| // Encrypt Function | |
| function mc_encrypt($encrypt, $mc_key){ | |
| $encrypt = serialize($encrypt); | |
| $passcrypt = trim(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $mc_key, trim($encrypt), | |
| MCRYPT_MODE_ECB)); | |
| $encoded = base64_encode($passcrypt); | |
| return $encoded; | |
| } | |
| // Decrypt Function | |
| function mc_decrypt($decrypt, $mc_key){ | |
| $decoded = base64_decode($decrypt); | |
| $decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $mc_key, trim($decoded), | |
| MCRYPT_MODE_ECB)); | |
| $decrypted = unserialize($decrypted); | |
| return $decrypted; | |
| } | |
| echo '<h1>Rijndael 256-bit Encryption Function</h1>'; | |
| $data = 'Super secret confidential string data.'; | |
| $encrypted_data = mc_encrypt($data, ENCRYPTION_KEY); | |
| echo '<h2>Example #1: String Data</h2>'; | |
| echo 'Data to be Encrypted: ' . $data . '<br/>'; | |
| echo 'Encrypted Data: ' . $encrypted_data . '<br/>'; | |
| echo 'Decrypted Data: ' . mc_decrypt($encrypted_data, ENCRYPTION_KEY) . '</br>'; | |
| $data = array(1, 5, 8, 9, 22, 10, 61); | |
| $encrypted_data = mc_encrypt($data, ENCRYPTION_KEY); | |
| echo '<h2>Example #2: Non-String Data</h2>'; | |
| echo 'Data to be Encrypted: <pre>'; | |
| print_r($data); | |
| echo '</pre><br/>'; | |
| echo 'Encrypted Data: ' . $encrypted_data . '<br/>'; | |
| echo 'Decrypted Data: <pre>'; | |
| print_r(mc_decrypt($encrypted_data, ENCRYPTION_KEY)); | |
| echo '</pre>'; | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment