import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
//需要导入 commons-codec-1.4.jar 包
public class Md5_Sha1_Base64 {
/**
* @param args
*/
public static void main(String[] args) {
String str="中文123abc,./";
String md5Str=DigestUtils.md5Hex(str);
System.out.println("MD5加密为:"+md5Str);
//SHA1加密
String sha1Str=DigestUtils.shaHex(str);
System.out.println("Sha1加密为:"+sha1Str);
//Base64加密
byte[] base64Byte=null;
String base64Str="";
try {
base64Byte = Base64.encodeBase64(str.getBytes("utf-8"), true);
base64Str=new String(base64Byte);
System.out.println("Base64加密为:"+base64Str);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Base64解密
byte[] base64ByteBack=null;
String base64StrBack="";
try {
base64ByteBack= Base64.decodeBase64(base64Str.getBytes("utf-8"));
base64StrBack=new String(base64ByteBack);
System.out.println("Base64解密为:"+base64StrBack);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
//需要导入 commons-codec-1.4.jar 包
public class Md5_Sha1_Base64 {
/**
* @param args
*/
public static void main(String[] args) {
String str="中文123abc,./";
System.out.println("原始字符串为:"+str);
String md5Str=DigestUtils.md5Hex(str);
System.out.println("MD5加密为:"+md5Str);
//SHA1加密
String sha1Str=DigestUtils.shaHex(str);
System.out.println("Sha1加密为:"+sha1Str);
//Base64加密
byte[] base64Byte=null;
String base64Str="";
try {
base64Byte = Base64.encodeBase64(str.getBytes("utf-8"), true);
base64Str=new String(base64Byte);
System.out.println("Base64加密为:"+base64Str);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Base64解密
byte[] base64ByteBack=null;
String base64StrBack="";
try {
base64ByteBack= Base64.decodeBase64(base64Str.getBytes("utf-8"));
base64StrBack=new String(base64ByteBack);
System.out.println("Base64解密为:"+base64StrBack);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行结果:
原始字符串为:中文123abc,./
MD5加密为:9ba58cffe5815ea9f365b86a3f473ad5
Sha1加密为:ec2f9a1f8f601dd5e6889d65204a7a998a0aa389
Base64加密为:5Lit5paHMTIzYWJjLC4v
Base64解密为:中文123abc,./