将密码加密从java转换为php

发布于 2024-09-04 02:06:15 字数 829 浏览 5 评论 0原文

我正在尝试创建现有 JSP 程序的 PHP 版本,但是我陷入了密码加密部分。

你能告诉我如何转换这个吗?我知道它尝试获取 md5() 但之后我就没有得到它。我迷失在 Stringbuffer 和 for() 部分。

你能帮我一下吗?

 public static String encryptPassword( String password ) 
 {
     String encrypted = "";
     try
     {
        MessageDigest digest = MessageDigest.getInstance( "MD5" ); 
        byte[] passwordBytes = password.getBytes( ); 

        digest.reset( );
        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 ] ) );
        }
        encrypted = hexString.toString();
     }
     catch( Exception e ) { }
     return encrypted; 
 }

I'm trying to create a PHP version of an existing JSP program, however I'm stuck at the password encryption part.

Could you please tell me how to convert this one? I know it tries to get the md5() but after that, I don't get it. I get lost in the Stringbuffer and for() parts.

Can you help me out?

 public static String encryptPassword( String password ) 
 {
     String encrypted = "";
     try
     {
        MessageDigest digest = MessageDigest.getInstance( "MD5" ); 
        byte[] passwordBytes = password.getBytes( ); 

        digest.reset( );
        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 ] ) );
        }
        encrypted = hexString.toString();
     }
     catch( Exception e ) { }
     return encrypted; 
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

乖不如嘢 2024-09-11 02:06:15

伊拉克利斯应该是对的。默认情况下,md5() 为您提供十六进制编码的输出字符串。您只能像在 Java 中那样通过为可选的 $raw_output 参数传入 TRUE 来获取未编码的字节。

长度范围为 29 到 32

那么你的 Java 代码有一个错误。 MD5 哈希值始终为 128 位(32 个十六进制数字)。就是这样:

hexString.append( Integer.toHexString(0xFF & message[ i ] ) );

这将为 16 以下的所有字节生成 1 而不是 01。您存储的是一个损坏的哈希值,您无法从中恢复原始 MD5 值。如果您绝对必须保留这些损坏的数据,则必须在 PHP 中重现该错误:

function makeBrokenMD5($s) {
    $hash= md5($s, TRUE);
    $bytes= preg_split('//', $hash, -1, PREG_SPLIT_NO_EMPTY);
    $broken= '';
    foreach ($bytes as $byte)
        $broken.= dechex(ord($byte));
    return $broken;
}

Iraklis should be right. md5() gives you a hex-encoded output string by default. You only get the unencoded bytes like in Java by passing in TRUE for the optional $raw_output argument.

the lengths range from 29 to 32

Then your Java code has a bug. MD5 hashes are always 128 bits (32 hex digits). Here it is:

hexString.append( Integer.toHexString(0xFF & message[ i ] ) );

this will generate 1 instead of 01 for all bytes below 16. What you have stored is a mangled hash, from which you cannot recover the original MD5 value. If you absolutely must keep this broken data, you will have to reproduce the bug in PHP:

function makeBrokenMD5($s) {
    $hash= md5($s, TRUE);
    $bytes= preg_split('//', $hash, -1, PREG_SPLIT_NO_EMPTY);
    $broken= '';
    foreach ($bytes as $byte)
        $broken.= dechex(ord($byte));
    return $broken;
}
哆啦不做梦 2024-09-11 02:06:15

它将 MD5 哈希值转换为字符最低有效字节的字符串十六进制数字。在 Java 中,所有字符都是 2 个字节。

实际上,这仅意味着 ASCII 值。

It converts the MD5 hash to a string hexadecimal numbers of the least significan byte of the character. In Java all chars are 2 bytes.

In practice this means just the ASCII value.

暗恋未遂 2024-09-11 02:06:15
<?php
$password = "MyPass";
$hash = md5($password);
?>

更新:
两个版本之间存在一些差异。要解决此问题,请参阅 @bobince 答案。这是测试代码:

Java

package tests;

import java.security.MessageDigest;

/**
 * Created by IntelliJ IDEA.
 * User: Iraklis
 * Date: 2 Ιουν 2010
 * Time: 2:15:03 μμ
 * To change this template use File | Settings | File Templates.
 */
public class Md5Test {
    public static String encryptPassword(String password) {
        String encrypted = "";
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            byte[] passwordBytes = password.getBytes();

            digest.reset();
            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]));
            }
            encrypted = hexString.toString();
        }
        catch (Exception e) {
        }
        return encrypted;
    }

    public static void main(String[] args) {
        System.out.println("Pass1 md5 = " + encryptPassword("Test123FORXTREMEpass"));
        System.out.println("Pass1 md5 = " + encryptPassword("Ijdsaoijds"));
        System.out.println("Pass1 md5 = " + encryptPassword("a"));
        System.out.println("Pass1 md5 = " + encryptPassword(" "));
    }

}


Output:
Pass1 md5 = dc3a7b42a97a3598105936ef22ad2c1
Pass1 md5 = df7ca542bdbf7c4b8776cb21c45e7eef
Pass1 md5 = cc175b9c0f1b6a831c399e269772661
Pass1 md5 = 7215ee9c7d9dc229d2921a40e899ec5f

PHP

<?php
echo "Pass1 md5 = ".md5("Test123FORXTREMEpass")."<BR>";
echo "Pass2 md5 = ".md5("Ijdsaoijds")."<BR>";
echo "Pass3 md5 = ".md5("a")."<BR>";
echo "Pass4 md5 = ".md5(" ")."<BR>";
?>

输出:

Pass1 md5 = dc3a7b42a97a35981059036ef22ad2c1
Pass2 md5 = df7ca542bdbf7c4b8776cb21c45e7eef
Pass3 md5 = 0cc175b9c0f1b6a831c399e269772661
Pass4 md5 = 7215ee9c7d9dc229d2921a40e899ec5f
<?php
$password = "MyPass";
$hash = md5($password);
?>

UPDATE:
There are some discrepancies between the two versions. To fix this see @bobince answer.Here is the test code:

Java

package tests;

import java.security.MessageDigest;

/**
 * Created by IntelliJ IDEA.
 * User: Iraklis
 * Date: 2 Ιουν 2010
 * Time: 2:15:03 μμ
 * To change this template use File | Settings | File Templates.
 */
public class Md5Test {
    public static String encryptPassword(String password) {
        String encrypted = "";
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            byte[] passwordBytes = password.getBytes();

            digest.reset();
            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]));
            }
            encrypted = hexString.toString();
        }
        catch (Exception e) {
        }
        return encrypted;
    }

    public static void main(String[] args) {
        System.out.println("Pass1 md5 = " + encryptPassword("Test123FORXTREMEpass"));
        System.out.println("Pass1 md5 = " + encryptPassword("Ijdsaoijds"));
        System.out.println("Pass1 md5 = " + encryptPassword("a"));
        System.out.println("Pass1 md5 = " + encryptPassword(" "));
    }

}


Output:
Pass1 md5 = dc3a7b42a97a3598105936ef22ad2c1
Pass1 md5 = df7ca542bdbf7c4b8776cb21c45e7eef
Pass1 md5 = cc175b9c0f1b6a831c399e269772661
Pass1 md5 = 7215ee9c7d9dc229d2921a40e899ec5f

PHP

<?php
echo "Pass1 md5 = ".md5("Test123FORXTREMEpass")."<BR>";
echo "Pass2 md5 = ".md5("Ijdsaoijds")."<BR>";
echo "Pass3 md5 = ".md5("a")."<BR>";
echo "Pass4 md5 = ".md5(" ")."<BR>";
?>

output:

Pass1 md5 = dc3a7b42a97a35981059036ef22ad2c1
Pass2 md5 = df7ca542bdbf7c4b8776cb21c45e7eef
Pass3 md5 = 0cc175b9c0f1b6a831c399e269772661
Pass4 md5 = 7215ee9c7d9dc229d2921a40e899ec5f
她如夕阳 2024-09-11 02:06:15

为了在 java 和 php 中获得相同的结果,我使用了这个。

Java(确保在“try”块内调用该方法):

public static String getHash(String pass) throws Exception
{
    MessageDigest md=MessageDigest.getInstance("MD5");
    md.update(pass.getBytes(),0,pass.length());
    return new BigInteger(1,md.digest()).toString(16);
}

PHP:

<?php
echo md5(pass);
?>

希望这有帮助

编辑:如果java变体返回31个字符,则在字符串前面添加一个“0”以匹配返回32个字符的php哈希。

To get the same results in both java and php I used this.

Java(make sure to call the method inside a "try" block):

public static String getHash(String pass) throws Exception
{
    MessageDigest md=MessageDigest.getInstance("MD5");
    md.update(pass.getBytes(),0,pass.length());
    return new BigInteger(1,md.digest()).toString(16);
}

PHP:

<?php
echo md5(pass);
?>

Hope this helps

Edit: If the java variant returns 31 characters, adds a "0" in front of the string to match the php hash which returns 32 characters.

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