import java.security.*;
public class MD5 {
public static String convert(String s) {
char hexChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f' };
try {
byte[] bytes = s.getBytes();
//如果使用SHA1算法加密 这个地方把MD5 改成 SHA-1即可
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(bytes);
bytes = md.digest();
int j = bytes.length;
char[] chars = new char[j * 2];
int k = 0;
for (int i = 0; i < bytes.length; i++) {
byte b = bytes[i];
chars[k++] = hexChars[b >>> 4 & 0xf];
chars[k++] = hexChars[b & 0xf];
}
return new String(chars);
} catch (Exception e) {
return null;
}
}
public static void main(String[] args) {
System.out.println(MD5.convert("0123456789"));
}
}
JAVA 生成MD5
最新推荐文章于 2022-09-29 15:02:46 发布