java使用apache commons codec来进行Base64加密解密
2015-12-15 00:00
826 查看
摘要: java使用apache commons codec来进行Base64加密解密
java使用apache commons codec来进行Base64加密解密
如果字符串默认没有使用编码,则使用utf-8编码
需要加编码的则使用
在使用是要么throws 要么使用try catch.
java使用apache commons codec来进行Base64加密解密
import org.apache.commons.codec.binary.Base64; public class Base64Test1 { /** * 作者:*TODO 使用Base64加密算法加密字符串 * return */ public static String encodeStr(String plainText){ byte[] b=plainText.getBytes(); Base64 base64=new Base64(); b=base64.encode(b); String s=new String(b); return s; } /** * 作者:*TODO 使用Base64加密 * return */ public static String decodeStr(String encodeStr){ byte[] b=encodeStr.getBytes(); Base64 base64=new Base64(); b=base64.decode(b); String s=new String(b); return s; } public static void main(String[] args) { String str1 = "你好,我是中国重庆,这个要是进行加密的字符串。"; String enstr1 = encodeStr(str1); String str2 = decodeStr(enstr1); System.out.println("原字符串:" + str1 + " 长度:" + str1.length()); System.out.println("encode字符串:" + enstr1 + " 长度:" + enstr1.length()); System.out.println("decode字符串:" + str2 + " 长度:" + str2.length() ); } }
如果字符串默认没有使用编码,则使用utf-8编码
需要加编码的则使用
public static String encodeStr(String plainText) throws UnsupportedEncodingException{ // byte[] b=plainText.getBytes("gb2312"); byte[] b=plainText.getBytes("utf-8"); Base64 base64=new Base64(); b=base64.encode(b); String s=new String(b); return s; }
在使用是要么throws 要么使用try catch.
相关文章推荐
- How to use re-write module of Apache web server 2.2.3 on FC6 with new or refurbi
- ubuntu install apache and php
- apache中配置php支持模块模式、cgi模式和fastcgi模式
- linux安装apache2.4.9
- Linux-CentOS XAMPP Apache 防盗链模块mod_auth_token的安装配置
- Mac下配置PHP+Apache+phpMyAdmin+MySql远程链接
- Linux-CentOS XAMPP Apache 支持mp4与flv拖动播放的功能模块
- Apache Commons 工具集
- Linux-CentOS XAMPP Apache mod_xsendfile 为php提供更快的文件下载
- java.lang.NoSuchMethodError: org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar
- Windows下的PHP开发环境搭建——PHP线程安全与非线程安全、Apache版本选择,及详解五种运行模式。
- Apache防盗链
- 配置Apache服务器与PHP语言
- Apache配置静态缓存
- Apache Commons 工具集使用简介
- PHP介绍和Apache的Module、CGI与FastCGI
- Apache2.4+PHP5.6+Mysql5.6(web环境搭建)
- apache日志切割与不记录指定文件类型日志
- apache服务器设置
- apache服务器设置