Create an MD5 class first in the project directory
You can directly copy the following code
//import your package name here
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Utils {
public static String md5Password(String password){
StringBuffer sb = new StringBuffer();
// 得到一个信息摘要器
try {
MessageDigest digest = MessageDigest.getInstance("md5");
byte[] result = digest.digest(password.getBytes());
// 把每一个byte做一个与运算 0xff
for (byte b : result) {
// 与运算
int number = b & 0xff;
String str = Integer.toHexString(number);
if (str.length() == 1) {
sb.append("0");
}
sb.append(str);
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return sb.toString();
}
}
Then, taking the CreateNewAccount class as an example, you can obtain the password entered by the user and convert it into a string, and then directly pass it into MD5Utils.md5Password( ). At this time, the password converted by this class method is the ciphertext after MD5 encryption. Isn't it very simple~