在Java中等同于PHP的crypt函数 [英] Equivalent of PHP's crypt function in Java
问题描述
所以我需要一个相当于Java的PHP的crypt函数,
,因为我已经存储了所有的密码注册用户
在我的数据库中使用crypt。
编辑1 :
这是我的加密密码的PHP代码:
$ password =test123;
$ pwd = crypt($ password,$ password);
echo $ pwd;
输出为(On Windows 以及 HostMonser )上的基于Linux的服务器):
temjCCsjBECmU
有人可以给我等价的java代码吗?
我已经尝试过各种排列和组合与
MessageDigest类,但无法正确的..
编辑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(); (int i = 0; i< message.length; i ++)
{
hexString.append(Integer.toHexString(
0xFF& message [i])) ;
}
加密字符串= hexString.toString();
System.out.println(encrypted);
}} catch(NoSuchAlgorithmException e1){
// TODO自动生成的catch块
e1.printStackTrace();
}
你必须知道什么实现由于有几个,根据您的操作系统,使用了PHP crypt(MD5?SHA256?SHA512?): http://php.net/manual/fr/function.crypt.php
Java等效类是 MessageDigest 。当您创建此类的实例时,您将提供散列算法,例如:
MessageDigest md = MessageDigest.getInstance( MD5\" );
MessageDigest md2 = MessageDigest.getInstance(SHA-256);
MessageDigest md3 = MessageDigest.getInstance(SHA-512);
//等
byte [] encryptedPassword = md.digest(yourPassword.getBytes());
I am migrating my PHP code to Google App Engine - Java.
So I need an equivalent of PHP's crypt function in Java,
since I have stored all the passwords of registered users
using crypt in my DB.
Edit 1: Here is my php code for encrypting passwords :
$password = "test123";
$pwd = crypt($password,$password);
echo $pwd;
Output is (On Windows as well as a linux based server on HostMonser):
temjCCsjBECmU
Can someone give me equivalted java code?
I have tried various permutations & combinations with
MessageDigest class, but can't get it right..
Edit 2:
Here is sample code which I thought would work but did not:
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();
}
You have to know what implementation of PHP crypt has been used (MD5? SHA256? SHA512?) because there are several, depending on your OS : http://php.net/manual/fr/function.crypt.php
The Java equivalent class is MessageDigest. When you create an instance of this class, you provide the hash algorithm, for example :
MessageDigest md = MessageDigest.getInstance("MD5");
MessageDigest md2 = MessageDigest.getInstance("SHA-256");
MessageDigest md3 = MessageDigest.getInstance("SHA-512");
// etc.
byte[] encryptedPassword = md.digest("yourPassword".getBytes());
这篇关于在Java中等同于PHP的crypt函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!