package com.tec.base.util; import java.io.UnsupportedEncodingException; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; /** * base64加密解密 * @author ZCZ * */ public class EncodeBase64 { /*** 加密 * encode by Base64 */ public static String encodeBase64(String value) throws Exception{ byte[] b = null; String s = null; try { b = value.getBytes("utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } if (b != null) { s = new BASE64Encoder().encode(b); } return s; } /*** 解码 * decode by Base64 */ public static String decodeBase64(String value) throws Exception{ byte[] b = null; String result = null; if (value != null) { BASE64Decoder decoder = new BASE64Decoder(); try { b = decoder.decodeBuffer(value); result = new String(b, "utf-8"); } catch (Exception e) { e.printStackTrace(); } } return result; } public static void main(String[] args) throws Exception { String str = "test1234"; System.out.println("加密之前原始数据:"+str); String base = EncodeBase64.encodeBase64(str); System.out.println("加密:"+base); String decode=EncodeBase64.decodeBase64(base); System.out.println("解密:"+decode); } }