Why is the MD5 encryption in Java and PHP different?

Source: Internet
Author: User
Why is the MD5 encryption in Java and PHP different? Php code:
echo md5(chr(142));

Java code:
Import java. math. bigInteger; import java. security. messageDigest; import java. security. noSuchAlgorithmException; public class utils {public static void main (String [] args) {char ss = (char) 142; // replace the md5 value after 56 with the md5 value of php. The result is the same as that of System. out. println (md5 (ss + "");} public static String md5 (String plainText) {byte [] secretBytes = null; try {secretBytes = MessageDigest. getInstance ("md5 "). digest (plainText. getBytes ());} Catch (NoSuchAlgorithmException e) {throw new RuntimeException ("no md5 algorithm! ");} String md5code = new BigInteger (1, secretBytes ). toString (16); for (int I = 0; I <32-md5code. length (); I ++) {md5code = "0" + md5code ;}return md5code ;}}


After testing, the md5 values of the char type of 142 are different, and the md5 values of the char type of 56 are the same. how can this problem be solved (all are UTF-8 encoded)


Reply to discussion (solution)

PlainText. getBytes ("GBK ");

PlainText. getBytes ("GBK ");


This method does not work. I tried it.

I don't know why
For (int I = 0; I <32-md5code. length (); I ++ ){
Md5code = "0" + md5code;
}
And the final value is always changing.

Public static String getMd5 (byte [] buffer) throws NoSuchAlgorithmException {String s = null; char hexDigist [] = {'0', '1', '2', '3 ', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D ', 'E', 'F'}; MessageDigest md = MessageDigest. getInstance ("MD5"); md. update (buffer); byte [] datas = md. digest (); // A 16-byte long integer char [] str = new char [2*16]; int k = 0; for (int I = 0; I <16; I ++) {byte B = datas [I]; str [k ++] = hexDigist [B >>> 4 & 0xf]; // 4-bit high str [k ++] = hexDigist [B & 0xf]; // 4-bit low} s = new String (str); return s ;}


In java, MD5 returns a length integer of 128 characters, that is, 16 bytes. one byte is mapped to 2 characters, so it is 32 characters, the md5 protocol is not the same, but PHP is implemented in C language at the underlying layer.
Here we will test:
Java:
 public static void main(String[] args) {    try {      System.out.println(getMd5("123".getBytes()));    } catch (NoSuchAlgorithmException e) {      // TODO Auto-generated catch block      e.printStackTrace();    }  }

Output:
202cb962ac59075b964b07152d234b70
PHP code:
echo md5("123");

Output:
202cb962ac59075b964b07152d234b70

Public static String getMd5 (byte [] buffer) throws NoSuchAlgorithmException {String s = null; char hexDigist [] = {'0', '1', '2', '3 ', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D ', 'E', 'F'}; MessageDigest md = MessageDigest. getInstance ("MD5"); md. update (buffer); byte [] datas = md. digest (); // A 16-byte long integer char [] str = new char [2*16]; int k = 0; for (int I = 0; I <16; I ++) {byte B = datas [I]; str [k ++] = hexDigist [B >>> 4 & 0xf]; // 4-bit high str [k ++] = hexDigist [B & 0xf]; // 4-bit low} s = new String (str); return s ;}


In java, MD5 returns a length integer of 128 characters, that is, 16 bytes. one byte is mapped to 2 characters, so it is 32 characters, the md5 protocol is not the same, but PHP is implemented in C language at the underlying layer.
Here we will test:
Java:
 public static void main(String[] args) {    try {      System.out.println(getMd5("123".getBytes()));    } catch (NoSuchAlgorithmException e) {      // TODO Auto-generated catch block      e.printStackTrace();    }  }

Output:
202cb962ac59075b964b07152d234b70
PHP code:
echo md5("123");

Output:
202cb962ac59075b964b07152d234b70




I can't help but paste all the php and java code. The php results are correct. how can I change the java code?

 Result: ". $ pt ."
". $ Vc ."
Password: ". $ passwd ."

"; Function jspassword ($ p, $ pt, $ vc, $ md5 = true) {echo $ p. ":". $ pt. ":". $ vc; if ($ md5) {$ p = strtoupper (md5 ($ p);} // echo"
". $ P; exit; $ len = strlen ($ p); $ temp = null; // echo"
Md5Password: ". $ p ."
"; For ($ I = 0; $ I <$ len; $ I = $ I + 2) {// echo"
I: ". $ I; $ temp. = '\ X'. substr ($ p, $ I, 2);} // echo"
". $ Temp ."
"; // Echo"


--- >>> ". Md5 (hex2asc ($ temp ). hex2asc ($ pt); // $ str = hex2asc ($ temp ). hex2asc ($ pt); // echo"
Internal :". $ str. "--> ". md5 (hex2asc ($ temp )). "--> ". md5 (hex2asc ($ pt); return strtoupper (md5 (strtoupper (md5 (hex2asc ($ temp ). hex2asc ($ pt ))). $ vc);}/*** hexadecimal conversion character ** @ access private * @ param string $ str * @ return string */function hex2asc ($ str) {// echo "before processing :". $ str."
"; // Print_r (explode ('\ X', $ str); $ str = join ('', explode (' \ X', $ str )); // echo"
After processing :". $ str; $ len = strlen ($ str); $ data = null; for ($ I = 0; $ I <$ len; $ I ++ = 2) {// echo"
: ". Substr ($ str, $ I, 2); echo"
". Hexdec (substr ($ str, $ I, 2 )). ":::". chr (hexdec (substr ($ str, $ I, 2 ))). "-> ". md5 (chr (hexdec (substr ($ str, $ I, 2); $ data. = chr (hexdec (substr ($ str, $ I, 2);} echo"
". Md5 ($ data )."
"; Return $ data ;}?>



Import java. math. bigInteger; import java. security. messageDigest; import java. security. noSuchAlgorithmException; public class utils {public static void main (String [] args) {utils u = new utils (); String up = u. jspassword ("123456", "\ x00 \ x00 \ x00 \ x00 \ x16 \ x9d \ x56 \ x75 ","! PRY ", true); System. out. println ("-----------------------------"); System. out. println (up);} public String jspassword (String password, String pt, String vc, boolean md5) {if (md5) {password = utils. md5 (password ). toUpperCase ();} int len = password. length (); String temp = ""; for (int I = 0; I <len; I = I + 2) {temp + = "\ x" + password. substring (I, I + 2);} return (utils. md5 (utils. md5 (utils. hex2asc (temp) + utils. hex2asc (pt )). toUpperCase () + vc )). toUpperCase ();} public static String hex2asc (String str) {String [] s = str. trim (). split ("\\\\ x"); // System. out. println (s. length); StringBuffer sb = new StringBuffer (); for (String sItem: s) {// System. out. println (sItem); sb. append (sItem);} int len = sb. toString (). length (); // String data = null; StringBuffer sb1 = new StringBuffer (); for (int I = 0; I
 
  
"+ Utils. md5 (String. valueOf (ss); sb1.append (String. valueOf (ss);} return sb1.toString ();} public static String md5 (String plainText) {byte [] secretBytes = null; try {secretBytes = MessageDigest. getInstance ("md5 "). digest (plainText. getBytes ();} catch (NoSuchAlgorithmException e) {throw new RuntimeException ("no md5 algorithm! ");} String md5code = new BigInteger (1, secretBytes ). toString (16); for (int I = 0; I <32-md5code. length (); I ++) {md5code = "0" + md5code ;}return md5code ;}}
 

I don't know why
For (int I = 0; I <32-md5code. length (); I ++ ){
Md5code = "0" + md5code;
}
And the final value is always changing.




Public static String getMd5 (byte [] buffer) throws NoSuchAlgorithmException {String s = null; char hexDigist [] = {'0', '1', '2', '3 ', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D ', 'E', 'F'}; MessageDigest md = MessageDigest. getInstance ("MD5"); md. update (buffer); byte [] datas = md. digest (); // A 16-byte long integer char [] str = new char [2*16]; int k = 0; for (int I = 0; I <16; I ++) {byte B = datas [I]; str [k ++] = hexDigist [B >>> 4 & 0xf]; // 4-bit high str [k ++] = hexDigist [B & 0xf]; // 4-bit low} s = new String (str); return s ;}


In java, MD5 returns a length integer of 128 characters, that is, 16 bytes. one byte is mapped to 2 characters, so it is 32 characters, the md5 protocol is not the same, but PHP is implemented in C language at the underlying layer.
Here we will test:
Java:
 public static void main(String[] args) {    try {      System.out.println(getMd5("123".getBytes()));    } catch (NoSuchAlgorithmException e) {      // TODO Auto-generated catch block      e.printStackTrace();    }  }

Output:
202cb962ac59075b964b07152d234b70
PHP code:
echo md5("123");

Output:
202cb962ac59075b964b07152d234b70




I will post all php and java code.

 Result: ". $ pt ."
". $ Vc ."
Password: ". $ passwd ."

"; Function jspassword ($ p, $ pt, $ vc, $ md5 = true) {echo $ p. ":". $ pt. ":". $ vc; if ($ md5) {$ p = strtoupper (md5 ($ p);} // echo"
". $ P; exit; $ len = strlen ($ p); $ temp = null; // echo"
Md5Password: ". $ p ."
"; For ($ I = 0; $ I <$ len; $ I = $ I + 2) {// echo"
I: ". $ I; $ temp. = '\ X'. substr ($ p, $ I, 2);} // echo"
". $ Temp ."
"; // Echo"


--- >>> ". Md5 (hex2asc ($ temp ). hex2asc ($ pt); // $ str = hex2asc ($ temp ). hex2asc ($ pt); // echo"
Internal :". $ str. "--> ". md5 (hex2asc ($ temp )). "--> ". md5 (hex2asc ($ pt); return strtoupper (md5 (strtoupper (md5 (hex2asc ($ temp ). hex2asc ($ pt ))). $ vc);}/*** hexadecimal conversion character ** @ access private * @ param string $ str * @ return string */function hex2asc ($ str) {// echo "before processing :". $ str."
"; // Print_r (explode ('\ X', $ str); $ str = join ('', explode (' \ X', $ str )); // echo"
After processing :". $ str; $ len = strlen ($ str); $ data = null; for ($ I = 0; $ I <$ len; $ I ++ = 2) {// echo"
: ". Substr ($ str, $ I, 2); echo"
". Hexdec (substr ($ str, $ I, 2 )). ":::". chr (hexdec (substr ($ str, $ I, 2 ))). "-> ". md5 (chr (hexdec (substr ($ str, $ I, 2); $ data. = chr (hexdec (substr ($ str, $ I, 2);} echo"
". Md5 ($ data )."
"; Return $ data ;}?>



Import java. math. bigInteger; import java. security. messageDigest; import java. security. noSuchAlgorithmException; public class utils {public static void main (String [] args) {utils u = new utils (); String up = u. jspassword ("123456", "\ x00 \ x00 \ x00 \ x00 \ x16 \ x9d \ x56 \ x75 ","! PRY ", true); System. out. println ("-----------------------------"); System. out. println (up);} public String jspassword (String password, String pt, String vc, boolean md5) {if (md5) {password = utils. md5 (password ). toUpperCase ();} int len = password. length (); String temp = ""; for (int I = 0; I <len; I = I + 2) {temp + = "\ x" + password. substring (I, I + 2);} return (utils. md5 (utils. md5 (utils. hex2asc (temp) + utils. hex2asc (pt )). toUpperCase () + vc )). toUpperCase ();} public static String hex2asc (String str) {String [] s = str. trim (). split ("\\\\ x"); // System. out. println (s. length); StringBuffer sb = new StringBuffer (); for (String sItem: s) {// System. out. println (sItem); sb. append (sItem);} int len = sb. toString (). length (); // String data = null; StringBuffer sb1 = new StringBuffer (); for (int I = 0; I
 
  
"+ Utils. md5 (String. valueOf (ss); sb1.append (String. valueOf (ss);} return sb1.toString ();} public static String md5 (String plainText) {byte [] secretBytes = null; try {secretBytes = MessageDigest. getInstance ("md5 "). digest (plainText. getBytes ();} catch (NoSuchAlgorithmException e) {throw new RuntimeException ("no md5 algorithm! ");} String md5code = new BigInteger (1, secretBytes ). toString (16); for (int I = 0; I <32-md5code. length (); I ++) {md5code = "0" + md5code ;}return md5code ;}}
 

Function jspassword ($ p, $ pt, $ vc, $ md5 = true)
{
Echo $ p. ":". $ pt. ":". $ vc;
If ($ md5)
{
$ P = strtoupper (md5 ($ p ));
}
// Echo"
". $ P; exit;
$ Len = strlen ($ p );
$ Temp = null;
// Echo"
Md5Password: ". $ p ."
";
For ($ I = 0; $ I <$ len; $ I = $ I + 2)
{
// Echo"
I: ". $ I;
$ Temp. = '\ X'. substr ($ p, $ I, 2 );
}
// Echo"
". $ Temp ."
";
// Echo"


--- >>> ". Md5 (hex2asc ($ temp). hex2asc ($ pt ));
// $ Str = hex2asc ($ temp). hex2asc ($ pt );
// Echo"
Internal: ". $ str." --> ". md5 (hex2asc ($ temp)." --> ". md5 (hex2asc ($ pt ));
Return strtoupper (md5 (strtoupper (md5 (hex2asc ($ temp). hex2asc ($ pt). $ vc ));
}

/**
* Hexadecimal conversion character
*
* @ Access private
* @ Param string $ str
* @ Return string
*/
Function hex2asc ($ str)
{
// Echo "before processing:". $ str ."
";
// Print_r (explode ('\ X', $ str ));
$ Str = join ('', explode ('\ X', $ str ));
// Echo"
After processing: ". $ str;
$ Len = strlen ($ str );
$ Data = null;
For ($ I = 0; $ I <$ len; $ I + = 2)
{
// Echo"
: ". Substr ($ str, $ I, 2 );
Echo"
". Hexdec (substr ($ str, $ I, 2 )). ":::". chr (hexdec (substr ($ str, $ I, 2 ))). "-> ". md5 (chr (hexdec (substr ($ str, $ I, 2 ))));
$ Data. = chr (hexdec (substr ($ str, $ I, 2 )));
}
Echo"
". Md5 ($ data )."
";
Return $ data;
}
?>


[/Code]


Import java. math. bigInteger; import java. security. messageDigest; import java. security. noSuchAlgorithmException; public class utils {public static void main (String [] args) {utils u = new utils (); String up = u. jspassword ("123456", "\ x00 \ x00 \ x00 \ x00 \ x16 \ x9d \ x56 \ x75 ","! PRY ", true); System. out. println ("-----------------------------"); System. out. println (up);} public String jspassword (String password, String pt, String vc, boolean md5) {if (md5) {password = utils. md5 (password ). toUpperCase ();} int len = password. length (); String temp = ""; for (int I = 0; I <len; I = I + 2) {temp + = "\ x" + password. substring (I, I + 2);} return (utils. md5 (utils. md5 (utils. hex2asc (temp) + utils. hex2asc (pt )). toUpperCase () + vc )). toUpperCase ();} public static String hex2asc (String str) {String [] s = str. trim (). split ("\\\\ x"); // System. out. println (s. length); StringBuffer sb = new StringBuffer (); for (String sItem: s) {// System. out. println (sItem); sb. append (sItem);} int len = sb. toString (). length (); // String data = null; StringBuffer sb1 = new StringBuffer (); for (int I = 0; I
 
  
"+ Utils. md5 (String. valueOf (ss); sb1.append (String. valueOf (ss);} return sb1.toString ();} public static String md5 (String plainText) {byte [] secretBytes = null; try {secretBytes = MessageDigest. getInstance ("md5 "). digest (plainText. getBytes ();} catch (NoSuchAlgorithmException e) {throw new RuntimeException ("no md5 algorithm! ");} String md5code = new BigInteger (1, secretBytes ). toString (16); for (int I = 0; I <32-md5code. length (); I ++) {md5code = "0" + md5code ;}return md5code ;}}
 



I don't know why
For (int I = 0; I <32-md5code. length (); I ++ ){
Md5code = "0" + md5code;
}
And the final value is always changing.




Public static String getMd5 (byte [] buffer) throws NoSuchAlgorithmException {String s = null; char hexDigist [] = {'0', '1', '2', '3 ', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D ', 'E', 'F'}; MessageDigest md = MessageDigest. getInstance ("MD5"); md. update (buffer); byte [] datas = md. digest (); // A 16-byte long integer char [] str = new char [2*16]; int k = 0; for (int I = 0; I <16; I ++) {byte B = datas [I]; str [k ++] = hexDigist [B >>> 4 & 0xf]; // 4-bit high str [k ++] = hexDigist [B & 0xf]; // 4-bit low} s = new String (str); return s ;}


In java, MD5 returns a length integer of 128 characters, that is, 16 bytes. one byte is mapped to 2 characters, so it is 32 characters, the md5 protocol is not the same, but PHP is implemented in C language at the underlying layer.
Here we will test:
Java:
 public static void main(String[] args) {    try {      System.out.println(getMd5("123".getBytes()));    } catch (NoSuchAlgorithmException e) {      // TODO Auto-generated catch block      e.printStackTrace();    }  }

Output:
202cb962ac59075b964b07152d234b70
PHP code:
echo md5("123");

Output:
202cb962ac59075b964b07152d234b70




I will post all php and java code.

 Result: ". $ pt ."
". $ Vc ."
Password: ". $ passwd ."

"; Function jspassword ($ p, $ pt, $ vc, $ md5 = true) {echo $ p. ":". $ pt. ":". $ vc; if ($ md5) {$ p = strtoupper (md5 ($ p);} // echo"
". $ P; exit; $ len = strlen ($ p); $ temp = null; // echo"
Md5Password: ". $ p ."
"; For ($ I = 0; $ I <$ len; $ I = $ I + 2) {// echo"
I: ". $ I; $ temp. = '\ X'. substr ($ p, $ I, 2);} // echo"
". $ Temp ."
"; // Echo"


--- >>> ". Md5 (hex2asc ($ temp ). hex2asc ($ pt); // $ str = hex2asc ($ temp ). hex2asc ($ pt); // echo"
Internal :". $ str. "--> ". md5 (hex2asc ($ temp )). "--> ". md5 (hex2asc ($ pt); return strtoupper (md5 (strtoupper (md5 (hex2asc ($ temp ). hex2asc ($ pt ))). $ vc);}/*** hexadecimal conversion character ** @ access private * @ param string $ str * @ return string */function hex2asc ($ str) {// echo "before processing :". $ str."
"; // Print_r (explode ('\ X', $ str); $ str = join ('', explode (' \ X', $ str )); // echo"
After processing :". $ str; $ len = strlen ($ str); $ data = null; for ($ I = 0; $ I <$ len; $ I ++ = 2) {// echo"
: ". Substr ($ str, $ I, 2); echo"
". Hexdec (substr ($ str, $ I, 2 )). ":::". chr (hexdec (substr ($ str, $ I, 2 ))). "-> ". md5 (chr (hexdec (substr ($ str, $ I, 2); $ data. = chr (hexdec (substr ($ str, $ I, 2);} echo"
". Md5 ($ data )."
"; Return $ data ;}?>



Import java. math. bigInteger; import java. security. messageDigest; import java. security. noSuchAlgorithmException; public class utils {public static void main (String [] args) {utils u = new utils (); String up = u. jspassword ("123456", "\ x00 \ x00 \ x00 \ x00 \ x16 \ x9d \ x56 \ x75 ","! PRY ", true); System. out. println ("-----------------------------"); System. out. println (up);} public String jspassword (String password, String pt, String vc, boolean md5) {if (md5) {password = utils. md5 (password ). toUpperCase ();} int len = password. length (); String temp = ""; for (int I = 0; I <len; I = I + 2) {temp + = "\ x" + password. substring (I, I + 2);} return (utils. md5 (utils. md5 (utils. hex2asc (temp) + utils. hex2asc (pt )). toUpperCase () + vc )). toUpperCase ();} public static String hex2asc (String str) {String [] s = str. trim (). split ("\\\\ x"); // System. out. println (s. length); StringBuffer sb = new StringBuffer (); for (String sItem: s) {// System. out. println (sItem); sb. append (sItem);} int len = sb. toString (). length (); // String data = null; StringBuffer sb1 = new StringBuffer (); for (int I = 0; I
 
  
"+ Utils. md5 (String. valueOf (ss); sb1.append (String. valueOf (ss);} return sb1.toString ();} public static String md5 (String plainText) {byte [] secretBytes = null; try {secretBytes = MessageDigest. getInstance ("md5 "). digest (plainText. getBytes ();} catch (NoSuchAlgorithmException e) {throw new RuntimeException ("no md5 algorithm! ");} String md5code = new BigInteger (1, secretBytes ). toString (16); for (int I = 0; I <32-md5code. length (); I ++) {md5code = "0" + md5code ;}return md5code ;}}
 



The byte array passed in during java testing. you need to call string. the getBytes () method. I can test it. I also learned java in the past two days. if it is code, I don't know how to solve it.

Wrong encoding. try again.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.