// md5 实现类 MD5.java
package com.company;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5 {
public static String StringToMD5(String Plaintext) throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(Plaintext.getBytes());
byte[] res = md5.digest();
String md5code = new BigInteger(1, res).toString(16);
for (int i = 0; i < 32 - md5code.length(); i++) {
md5code = "0" + md5code;
}
return md5code;
}
}
// MD5Use.java
package com.company;
import java.security.NoSuchAlgorithmException;
import static java.lang.System.out;
public class MD5Use {
public static void main(String[] args) throws NoSuchAlgorithmException {
MD5 md5 = new MD5();
out.println( md5.StringToMD5("a12345678"));
}
}
原文:https://www.cnblogs.com/ruhai/p/14364207.html