base64加密解密实现方法

/**

     * Base64加密算法

     * @param str

     * @return

     */

    public static String base64Encode(String str) throws Exception {

        String retStr = "";

        if(StringUtils.isBlank(str)) {

            return "";

        }

        try{

            //BASE64加密算法

            BASE64Encoder base64 = new BASE64Encoder();

            byte[] xmlStr = str.getBytes();

            retStr = base64.encode(xmlStr);

        }catch(Exception e){

            throw new RuntimeException("Base64编码 加密 失败!");

        }

        return retStr;

    }



    /**

     * Base64解码算法

     * @param str

     * @return

     */

    public static String base64Decode(String str) throws Exception{

        if(StringUtils.isBlank(str)) {

            return "";

        }

        byte[] bt = null;

        String retStr = "";

        try{

            sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();

            bt = decoder.decodeBuffer(str);

            retStr = new String(bt);

        }catch(Exception e){

            throw new RuntimeException("XML字符串Base64解码失败");

        }

        return retStr;

    }

 

你可能感兴趣的:(base64)