如何在java中实现php的crypt_md5
我有一个简单的 PHP 应用程序,它使用以下代码对密码进行哈希处理并将其存储在数据库中。
<?php
$user_name = "admin";
$password = "1234";
$salt = substr($user_name, 0, 2);
$salt = '$1$' . $salt . '$'; //$salt = $1$ad$
$crypt_password = crypt($password, $salt);
echo $crypt_password;
?>
这段代码,产生以下密码存储在数据库中: $1$ad$BH3wnQs1wym28vdzP8zyh1
我试图用Java制作完全相同的代码,但由于我是Java新手,我遇到了很多困难。我在这里检查了 http:// /www.java2s.com/Open-Source/Java-Document/Groupware/LibreSource/md5/MD5Crypt.java.htm#cryptStringString 似乎它是我所需要的,但我没能让它发挥作用。任何帮助将不胜感激。先感谢您。
I have a simple application in PHP which uses the following code to hash the password and store it in a db.
<?php
$user_name = "admin";
$password = "1234";
$salt = substr($user_name, 0, 2);
$salt = '$1
this code, produces the following password to store in the db: $1$ad$BH3wnQs1wym28vdzP8zyh1
I am trying to make exactly the same code with Java, but as I am new to Java, I have a lot of difficulties. I checked over here http://www.java2s.com/Open-Source/Java-Document/Groupware/LibreSource/md5/MD5Crypt.java.htm#cryptStringString and it seems that it is what I need, but I didn't manage to make it work. Any help would be appreciated. Thank you in advance.
. $salt . '
this code, produces the following password to store in the db: $1$ad$BH3wnQs1wym28vdzP8zyh1
I am trying to make exactly the same code with Java, but as I am new to Java, I have a lot of difficulties. I checked over here http://www.java2s.com/Open-Source/Java-Document/Groupware/LibreSource/md5/MD5Crypt.java.htm#cryptStringString and it seems that it is what I need, but I didn't manage to make it work. Any help would be appreciated. Thank you in advance.
; //$salt = $1$ad$
$crypt_password = crypt($password, $salt);
echo $crypt_password;
?>
this code, produces the following password to store in the db: $1$ad$BH3wnQs1wym28vdzP8zyh1
I am trying to make exactly the same code with Java, but as I am new to Java, I have a lot of difficulties. I checked over here http://www.java2s.com/Open-Source/Java-Document/Groupware/LibreSource/md5/MD5Crypt.java.htm#cryptStringString and it seems that it is what I need, but I didn't manage to make it work. Any help would be appreciated. Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 md5 适合您,您可以尝试以下代码:
另外,您可以将“MD5”更改为“SHA1”,并且应该也可以工作。
If md5 works for you, you can try the following code:
Also, you can change "MD5" to "SHA1" and should work too.