packagecom.esint.ztb.util;importsun.misc.BASE64Decoder;importsun.misc.BASE64Encoder;importjava.io.UnsupportedEncodingException;//应对明文传输问题publicclassBase64{
// 加密publicstaticStringencode(String str){
byte[] b =null;String s =null;try{
b = str.getBytes("utf-8");}catch(UnsupportedEncodingException e){
e.printStackTrace();}if(b !=null){
s =newBASE64Encoder().encode(b);//据RFC 822规定,每76个字符,还需要加上一个回车换行去掉换行符
s = s.replaceAll("[\\s*\t\n\r]","");}return s;}// 解密publicstaticStringdecode(String s){
byte[] b =null;String result =null;if(s !=null){
BASE64Decoder decoder =newBASE64Decoder();try{
b = decoder.decodeBuffer(s);
result =newString(b,"utf-8");}catch(Exception e){
e.printStackTrace();}}return result;}//使用方法publicstaticvoidmain(String[] args){
String s ="你好,The Word!";//System.out.println(s.length());String enStr =encode(s);String deStr =decode(enStr);System.out.println("原始数据:"+s+"\n加密数据:"+enStr+"\n解密数据:"+deStr);}}
2、js
/*!
* jquery.base64.js 0.1 - https://github.com/yckart/jquery.base64.js
* Makes Base64 en & -decoding simpler as it is.
*
* Based upon: https://gist.github.com/Yaffle/1284012
*
* Copyright (c) 2012 Yannick Albert (http://yckart.com)
* Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php).
* 2013/02/10
**//**
* Created by SLICE_30_K on 2017/5/22.
*
* 支持一般Base64的编码和解码
* 支持符合RFC_4648标准中"URL and Filename Safe Alphabet"的URL安全Base64编解码
* 支持中文字符的编解码(Unicode编码)
*/;(function(root, factory){
if(typeof exports ==="object"){
// CommonJS
module.exports = exports =factory();}elseif(typeof define ==="function"&& define.amd){
// AMDdefine(factory);}else{
// Global (browser)
window.BASE64=factory();}}(this,function(){
varBASE64_MAPPING=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U',