最近有一个需求,需要将php写的加密算法用java实现,php的代码已经有了,但是用java改写,尝试了好多久也没有成功。有知道的同学,希望不吝赐教,谢谢。
php的代码如下:
function simple_encrypt($text)
{
$key = "anything";
$vi = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $vi)));
}
function simple_decrypt($text)
{
$key = "anything";
$vi = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($text), MCRYPT_MODE_ECB, $vi));
}
$str = '12345';
$en = simple_encrypt($str);
$de = simple_decrypt($en);
var_dump($en, $de);
?>
执行结果为:
string(44) "uEJfzXHxFVIlu2KGCtToiouT/EeMeat8CHvgkN5kzA4="
string(5) "12345"