shishaoe 2012-02-10
<?php
function pkcs5_pad ($text, $blocksize)
{
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
function pkcs5_unpad($text)
{
$pad = ord($text{strlen($text)-1});
if ($pad > strlen($text)) return false;
if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false;
return substr($text, 0, -1 * $pad);
}
function encrypt_something($input)
{
$size = mcrypt_get_block_size('des', 'cbc');
$input = pkcs5_pad($input, $size);
$key = '7e81857e';
$td = mcrypt_module_open('des', '', 'cbc', '');
mcrypt_generic_init($td, $key, "\0\0\0\0\0\0\0\0");
$data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$data = base64_encode($data);
return $data;
}
function decrypt($str){
$key = '7e81857e';
$size = mcrypt_get_block_size('des', 'cbc');
$td=mcrypt_module_open('des', '', 'cbc', '');
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td,$key,"\0\0\0\0\0\0\0\0");
$decrypted_text = mdecrypt_generic($td,base64_decode($str));
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$plain_text = pkcs5_unpad($decrypted_text);
return $plain_text;
}
echo decrypt("8cvdp5aZ3mDXPOQtXuLDg6aMhWwuxPWzfw3NRbZYBYjdFKbxIhKVnxJNQndKAAG0C8GmV5qdQa/zCv8cDIe7yg==");
//echo encrypt_something("Hello World! ASDFGHJKL;'asdfghjkl;'!@#$%^&*()01234567890");
?>