Convert password encryption from java to php

I am trying to create a PHP version of an existing JSP program, however I am stuck with passwords encryption.

Could you tell me how to convert this? I know that he is trying to get md5 (), but after that I do not understand. I get lost in Stringbuffer and for () parts.

You can help me?

 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; 
 }
-1
source share
4 answers

Iraklis must be right. md5()by default displays a string with hexadecimal encoding. You only get unwritten bytes, as in Java, passing TRUEfor an optional argument $raw_output.

lengths from 29 to 32

Java . MD5 128 (32 ). :

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

1 01 16. - , 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;
}
+3

MD5 . Java 2 .

ASCII.

+1
<?php
$password = "MyPass";
$hash = md5($password);
?>

UPDATE: . , . @bobince answer.Here :

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
+1

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);
?>

Hope this helps

Edit: if the java variant returns 31 characters, appends a "0" before the string to match the php hash, which returns 32 characters.

0
source

Source: https://habr.com/ru/post/1774959/


All Articles