Home > Backend Development > PHP Tutorial > java实现AES算法(已有运行的PHP版本)

java实现AES算法(已有运行的PHP版本)

WBOY
Release: 2016-06-06 20:35:09
Original
936 people have browsed it

最近有一个需求,需要将php写的加密算法用java实现,php的代码已经有了,但是用java改写,尝试了好多久也没有成功。有知道的同学,希望不吝赐教,谢谢。

<code><?php function simple_encrypt($text)
{
    $key = "anything";
    $iv = 'MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=';
    $key = hash('sha256', $key, true);
    $iv = base64_decode($vi);
    return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, $iv)));
}

function simple_decrypt($text)
{
    $key = "anything";
    $iv = 'MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=';
    $key = hash('sha256', $key, true);
    $iv = base64_decode($vi);
    return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($text), MCRYPT_MODE_CBC, $iv));
}

$en = simple_encrypt($str);
$de = simple_decrypt($en);
var_dump($en, $de);

执行结果:
string(44) "OgmKJ5gu4rRYB47pVV5N1GyTW5+aKNjQnx2TFYjNdUI="
string(19) "1234567890987654321"
</code></code>
Copy after login
Copy after login

回复内容:

最近有一个需求,需要将php写的加密算法用java实现,php的代码已经有了,但是用java改写,尝试了好多久也没有成功。有知道的同学,希望不吝赐教,谢谢。

<code><?php function simple_encrypt($text)
{
    $key = "anything";
    $iv = 'MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=';
    $key = hash('sha256', $key, true);
    $iv = base64_decode($vi);
    return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, $iv)));
}

function simple_decrypt($text)
{
    $key = "anything";
    $iv = 'MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=';
    $key = hash('sha256', $key, true);
    $iv = base64_decode($vi);
    return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($text), MCRYPT_MODE_CBC, $iv));
}

$en = simple_encrypt($str);
$de = simple_decrypt($en);
var_dump($en, $de);

执行结果:
string(44) "OgmKJ5gu4rRYB47pVV5N1GyTW5+aKNjQnx2TFYjNdUI="
string(19) "1234567890987654321"
</code></code>
Copy after login
Copy after login

为何不百度

http://www.cnblogs.com/freeliver54/archive/2011/10/08/2202136.html

使用mcrypt_encrypt方法出现500的错误

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template