java自带的MD5\Sha1\Base64加密和Bse64解密

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,./";

System.out.println("原始字符串为:"+str);


//MD5加密
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,./

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值