我正在将我的PHP代码迁移到Google App Engine - Java。
因此,我需要在Java中找到与PHP的crypt函数等效的函数,
因为我已经使用crypt在我的数据库中存储了所有已注册用户的密码。
Edit 1: 以下是我用于加密密码的PHP代码:
$password = "test123";
$pwd = crypt($password,$password);
echo $pwd;
输出结果如下(在Windows和基于HostMonster 的Linux服务器上):
temjCCsjBECmU
有人能提供相应的Java代码吗?
我已经尝试使用MessageDigest类进行各种排列组合,但无法正确实现。
Edit 2:
以下是我认为会起作用但实际上没有的示例代码:
try {
{
String password = "test123";
MessageDigest digest = MessageDigest.getInstance( "MD5" );
byte[] passwordBytes = password.getBytes( );
digest.reset( );
digest.update( passwordBytes );
digest.update( passwordBytes );
byte[] message = digest.digest( );
StringBuffer hexString = new StringBuffer();
for ( int i=0; i < message.length; i++)
{
hexString.append( Integer.toHexString(
0xFF & message[ i ] ) );
}
String encrypted = hexString.toString();
System.out.println(encrypted);
} } catch (NoSuchAlgorithmException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}