MD5 . SHA1 加密
程序员文章站
2022-03-14 23:17:16
...
public static String str;
public static String convertMD5(String plainText) {
String result = "";
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plainText.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
str = buf.toString();
result = buf.toString();
//System.out.println("result32: " + buf.toString());// 32位的加密
//System.out.println("result16: " + buf.toString().substring(8, 24));// 16位的加密
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
public static String convertSHA1(String plainText) {
String result = "";
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(plainText.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
str = buf.toString();
result = buf.toString(); //System.out.println("result32: " + buf.toString());// 32位的加密
//System.out.println("result16: " + buf.toString().substring(8, 24));// 16位的加密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return result;
}
public static void main(String agrs[]) {
System.out.println(ToHash.convertMD5("http://www.juapk.com"));
System.out.println(ToHash.convertSHA1("http://www.juapk.com"));
}
上一篇: js二分查找算法介绍和代码分享
下一篇: 使用tkinter实现三子棋游戏