Current location - Quotes Website - Personality signature - About MD5 encryption results
About MD5 encryption results

You should have found the wrong JAVA MD5 encryption implementation code online, right? Using the wrong method to encrypt is the ciphertext you posted

//Wrong method; 0xFF & byteArray[i]).length() == 1 will be missing 0 digits, which is incorrect ,

// md5StrBuff.append(Integer.toHexString(0xff & byteArray[i]));

//The correct way

if (Integer. toHexString(0xFF & byteArray[i]).length() == 1) {

md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));< /p>

} else {

md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));

}

// end

Complete:

import java.security.MessageDigest;

public class MD5Util {

public static void main(String[ ] args) {

System.out.println(MD5("123"));

}

/**

* The result of encrypting the string into MD5

* @param source source string

* @return the encrypted string

*/

public static String MD5(String source) {

try {

// MD5 encryption of the password

byte byteEncrypt[] = null ;

byteEncrypt = source.getBytes("UTF8");

MessageDigest mdInstance = MessageDigest.getInstance("MD5");

mdInstance.update(byteEncrypt );

byte byteArray[] = mdInstance.digest();

// MD5 conversion encoding

StringBuffer md5StrBuff = new StringBuffer();

for (int i = 0; i < byteArray.length; i++) {

//Wrong way; 0xFF & byteArray[i]).length() == 1 Missing 0 digits is wrong

// md5StrBuff.append(Integer.toHexString(0xff & byteArray[i]));

//The correct way

if (Integer.toHexString(0xFF & byteArray[i]).length() == 1) {

md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i] ));

} else {

md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));

}

< p>//end

}

return md5StrBuff.toString();

} catch (Exception e) {

}< /p>

return null;

}

}