MD5实现加密 java
程序员文章站
2024-03-20 13:21:46
...
package util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import sun.misc.BASE64Encoder;
public class MD5Util {
public static void test1(){
try {
MessageDigest md =
MessageDigest.getInstance("md5");
String str = "ihateyouasdfasdfasdf";
byte[] buf = md.digest(str.getBytes());
BASE64Encoder encoder =
new BASE64Encoder();
String str2 = encoder.encode(buf);
System.out.println("str2:" + str2 + " " + str2.length());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
public static String getMD5Str(String origStr) throws Exception{
MessageDigest md =
MessageDigest.getInstance("md5");
byte[] buf = md.digest(origStr.getBytes());
BASE64Encoder encoder =
new BASE64Encoder();
String str2 = encoder.encode(buf);
return str2;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
test1();
}
}
上一篇: python购物车简单实现编写