php版的AES算法(可与java兼容)
直接上代码吧
class CryptAES{ private $cipher = "rijndael-128"; private $mode = "cbc"; private $secret_key = "ef955cd2d062642c7b0988f20598d96f"; private $iv = "1010011918423013"; function CryptAES($iv,$key){ $this->iv = $iv; $this->secret_key = $key; } function encrypt($str){ $td = mcrypt_module_open($this->cipher, "", $this->mode, $this->iv); mcrypt_generic_init($td, $this->secret_key, $this->iv); $cyper_text = mcrypt_generic($td, $str); $r = bin2hex($cyper_text); mcrypt_generic_deinit($td); mcrypt_module_close($td); return $r; } function decrypt($str){ $td = mcrypt_module_open($this->cipher, "", $this->mode, $this->iv); mcrypt_generic_init($td, $this->secret_key, $this->iv); $decrypted_text = mdecrypt_generic($td, $this->hex2bin($str)); $r = $decrypted_text; mcrypt_generic_deinit($td); mcrypt_module_close($td); return $r; } private function hex2bin($hexdata) { $bindata=""; for ($i=0;$i<strlen($hexdata);$i+=2) { $bindata.=chr(hexdec(substr($hexdata,$i,2))); } return $bindata; } } $aes = new CryptAES("1010011518423013","af955cd2a0626b2c7f0988f20598d96f"); $s0 = "www.codigg.com"; echo "string:".$s0."\n"; $s1 = $aes->encrypt($s0); echo "encrypted:".$s1."\n"; $s2 = $aes->decrypt($s1); echo "decrypted:".$s2."\n"; |
原创文章如转载,请注明:转载自CODIGG [ http://www.codigg.com/ ]
本文链接地址:http://www.codigg.com/2010/01/php-aes-java-encrypt-decrypt/

Leave a Reply