Created
May 20, 2013 11:25
-
-
Save ademalp/5611703 to your computer and use it in GitHub Desktop.
This file contains 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 | |
function encrypt($plaintext,$plainkey){ | |
$key = pack('H*', "$plainkey"); | |
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); | |
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); | |
$plaintext_utf8 = utf8_encode($plaintext); | |
$ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, | |
$plaintext_utf8, MCRYPT_MODE_CBC, $iv); | |
$ciphertext = $iv . $ciphertext; | |
$ciphertext_base64 = base64_encode($ciphertext); | |
return $ciphertext_base64; | |
} | |
function decrypt($ciphertext_base64,$plainkey) | |
{ | |
$key = pack('H*', "$plainkey"); | |
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); | |
$ciphertext_dec = base64_decode($ciphertext_base64); | |
$iv_dec = substr($ciphertext_dec, 0, $iv_size); | |
$ciphertext_dec = substr($ciphertext_dec, $iv_size); | |
$plaintext_utf8_dec = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, | |
$ciphertext_dec, MCRYPT_MODE_CBC, $iv_dec); | |
return $plaintext_utf8_dec; | |
} |
This file contains 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 | |
include "func.mcrypt.php"; | |
$key = "12345"; | |
$text = "This string was AES-256 / CBC / ZeroBytePadding encrypted."; | |
$ciphertext = encrypt($text,$key); | |
$retext = decrypt($ciphertext,$key); | |
echo "$text\n$ciphertext\n$retext"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment