import org.apache.commons.codec.binary.Base64;

public class Base64Util {
    /**
     * BASE64解密
     * 
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] decryptBASE64(String key) throws Exception {
        Base64 base64 = new Base64();
        return base64.decode(key.getBytes());
    }

    /**
     * BASE64加密
     * 
     * @param key
     * @return
     * @throws Exception
     */
    public static String encryptBASE64(byte[] key) throws Exception {
        Base64 base64 = new Base64();
        return new String(base64.encode(key),"UTF-8");
    }
}

 

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.elasticsearch.common.Base64;

public class Base64UtilEx {
    public static String encode(String str) {
        try {
            str = Base64.encodeBytes(str.getBytes("UTF-8")).replace("+", "|");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return str;
    }

    public static String decode(String str) {
        str = str.replace("|", "+");
        try {
            str = new String(Base64.decode(str.getBytes("UTF-8")));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }
}

 

posted on 2017-07-31 10:53  怂人不倦  阅读(263)  评论(0)    收藏  举报