<?php
function url_base64_encode($string) {
$data = base64_encode($string);
$data = str_replace(array('+','/','='),array('-','_',''),$data);
return $data;
}
function url_base64_decode($string) {
$data = str_replace(array('-','_'),array('+','/'),$string);
$mod4 = strlen($data) % 4;
if ($mod4) {
$data .= substr('====', $mod4);
}
return base64_decode($data);
}
function encrypt($key, $str) {
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_ECB),MCRYPT_DEV_URANDOM);
$str_encrypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_128,$key,$str,MCRYPT_MODE_ECB,$iv);
return trim(url_base64_encode($str_encrypt));
}
function decrypt($key, $str_encrypt) {
$str_encrypt = trim(url_base64_decode($str_encrypt));
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_ECB),MCRYPT_DEV_URANDOM);
$str_decrypt = mcrypt_decrypt(MCRYPT_RIJNDAEL_128,$key,$str_encrypt,MCRYPT_MODE_ECB,$iv);
return trim($str_decrypt);
}
echo encrypt('key','http://www.baidu.com////aaaa/bbbb/ccd//ddd')."\n";
echo decrypt('key','zPsrxzCmqAAvlLwv4E7AlI346XTbaw1hAOoPZhtD3sKw__EIxmWpyHTEp4u2skGM')."\n";
?>
标签:php