如何在java中实现php的crypt_md5

发布于 2024-11-08 18:00:25 字数 679 浏览 3 评论 0原文

我有一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自在安然 2024-11-15 18:00:25

如果 md5 适合您,您可以尝试以下代码:

    String pass = "1234";
    MessageDigest crypt = null;

    try {
        crypt = java.security.MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        System.out.println("MD5 not supported");
        return; // depends on your method
    }

    byte[] digested = crypt.digest(pass.getBytes());
    String crypt_password = new String();

    // Converts bytes to string
    for (byte b : digested) 
        crypt_password += Integer.toHexString(0xFF & b);

    System.out.println(crypt_password);

另外,您可以将“MD5”更改为“SHA1”,并且应该也可以工作。

If md5 works for you, you can try the following code:

    String pass = "1234";
    MessageDigest crypt = null;

    try {
        crypt = java.security.MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        System.out.println("MD5 not supported");
        return; // depends on your method
    }

    byte[] digested = crypt.digest(pass.getBytes());
    String crypt_password = new String();

    // Converts bytes to string
    for (byte b : digested) 
        crypt_password += Integer.toHexString(0xFF & b);

    System.out.println(crypt_password);

Also, you can change "MD5" to "SHA1" and should work too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文