Java implementation of MD5 encryption method summary
- 2020-04-01 04:19:14
- OfStack
A code:
public class MD5 {
public static String stringMD5(String pw) {
try {
//Get an MD5 converter (if you want SHA1 instead of "SHA1")
MessageDigest messageDigest =MessageDigest.getInstance("MD5");
//The input string is converted into a byte array
byte[] inputByteArray = pw.getBytes();
//InputByteArray is an array of bytes converted from an input string
messageDigest.update(inputByteArray);
//Converts and returns the result, also a byte array, containing 16 elements
byte[] resultByteArray = messageDigest.digest();
//Returns an array of characters converted to a string
return byteArrayToHex(resultByteArray);
} catch (NoSuchAlgorithmException e) {
return null;
}
}
public static String byteArrayToHex(byte[] byteArray) {
//First, an array of characters is initialized to hold each hexadecimal character
char[] hexDigits = {'0','1','2','3','4','5','6','7','8','9', 'A','B','C','D','E','F' };
//A byte is a binary of eight bits, or two hexadecimal characters (2 to the eighth is equal to 16 to the second).
char[] resultCharArray =new char[byteArray.length * 2];
//Traversing the byte array, by bit operation (bit operation is efficient), converts the character into the character array to go
int index = 0;
for (byte b : byteArray) {
resultCharArray[index++] = hexDigits[b>>> 4 & 0xf];
resultCharArray[index++] = hexDigits[b& 0xf];
}
//The array of characters is returned as a string
return new String(resultCharArray);
}
}
Method 2:
package other;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5 {
//Global array
private final static String[] strDigits = { "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
public MD5() {
}
//Returns a number and a string
private static String byteToArrayString(byte bByte) {
int iRet = bByte;
// System.out.println("iRet="+iRet);
if (iRet < 0) {
iRet += 256;
}
int iD1 = iRet / 16;
int iD2 = iRet % 16;
return strDigits[iD1] + strDigits[iD2];
}
//Return form only for Numbers
private static String byteToNum(byte bByte) {
int iRet = bByte;
System.out.println("iRet1=" + iRet);
if (iRet < 0) {
iRet += 256;
}
return String.valueOf(iRet);
}
//Converts the byte array to a hexadecimal string
private static String byteToString(byte[] bByte) {
StringBuffer sBuffer = new StringBuffer();
for (int i = 0; i < bByte.length; i++) {
sBuffer.append(byteToArrayString(bByte[i]));
}
return sBuffer.toString();
}
public static String GetMD5Code(String strObj) {
String resultString = null;
try {
resultString = new String(strObj);
MessageDigest md = MessageDigest.getInstance("MD5");
//The function md.digest() returns a byte array of hash results
resultString = byteToString(md.digest(strObj.getBytes()));
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
}
return resultString;
}
public static void main(String[] args) {
MD5 getMD5 = new MD5();
System.out.println(getMD5.GetMD5Code("000000"));
}
}