Base64 四种方式的编码和解码

base64与字符串String之间的相互转换

package com.wzq;

import javax.xml.bind.DatatypeConverter;
import java.io.UnsupportedEncodingException;
import java.util.Base64;

/**
 * @ClassName Base64Utils
 * @Description: TODO
 * @Author wzq
 * @CreateDate 2019/10/14
 * @UpdateDate 2019/10/16
 * @Version V1.0
 **/
public class Base64Utils {

    /**
     * 测试编码  OK
     */
    public static void main(String[] args) throws Throwable {
        System.out.println("*************************方式一*********************");
        String encode1 = Base64Encode("在线加密解密测试", "UTF-8");
        System.out.println("Encode1 " + encode1);
        String decode1 = Base64Decode(encode1, "UTF-8");
        System.out.println("decode1 " + decode1);
        System.out.println("*************************方式二*********************");
        String encode2 = encode2("在线加密解密测试", "UTF-8");
        System.out.println("encode2 "+encode2);
        String decode2 = decode2(encode2, "UTF-8");
        System.out.println("decode2 "+decode2);
        System.out.println("*************************方式三*********************");
        String encode3 = encode3("在线加密解密测试", "UTF-8");
        System.out.println("encode3 "+encode3);
        String decode3 = decode3(encode3, "UTF-8");
        System.out.println("decode3 "+decode3);
        System.out.println("*************************方式四*********************");
        String encode4 = encode4("在线加密解密测试", "UTF-8");
        System.out.println("encode4 "+encode4);
        String decode4 = decode3(encode4, "UTF-8");
        System.out.println("decode4 "+decode4);
    }

    /*****************************************方式一**********************************************/
    /***************使用jdk自带的DatatypeConverter.java类实现,但是jdk版本必须>=1.6。****************/

    /**
     * base64编码
     *
     * @param result
     * @param encodeType
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String Base64Encode(String result, String encodeType) throws Throwable {
        try {
            byte[] data = result.getBytes(encodeType);
            return DatatypeConverter.printBase64Binary(data);
        } catch (Exception e) {
            throw new Throwable("base64编码错误");
        }
    }


    /**
     * base64解码
     *
     * @param encodeType
     * @param result
     * @return
     */
    public static String Base64Decode(String result, String encodeType) throws Throwable {
        try {
            byte[] data = DatatypeConverter.parseBase64Binary(result);
            return new String(data, encodeType);
        } catch (Exception e) {
            throw new Throwable("base64解码");
        }
    }

    /*****************************************方式二**********************************************/
    /*******************使用jdk自带的Base64.java类实现,但是jdk版本必须>=1.8。***********************/

    /**
     * base64编码
     *
     * @param str
     * @param encodeType
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String encode2(String str, String encodeType) throws UnsupportedEncodingException {
        byte[] data = str.getBytes(encodeType);
        return Base64.getEncoder().encodeToString(data);
    }

    /**
     * base64解码
     *
     * @param result
     * @param encodeType
     * @return
     */
    public static String decode2(String result, String encodeType) throws UnsupportedEncodingException {
        byte[] data = result.getBytes(encodeType);
        byte[] decode = Base64.getDecoder().decode(data);
        return new String(decode,encodeType);
    }

    /*****************************************方式三**********************************************/
    /** 使用cn.hutool.core.codec.Base64类实现
     * <dependency>
     *     <groupId>cn.hutool</groupId>
     *     <artifactId>hutool-all</artifactId>
     *     <version>4.5.0</version>
     * </dependency>
     */

    /**
     * base64编码
     *
     * @param str
     * @param encodeType
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String encode3(String str, String encodeType) throws UnsupportedEncodingException {
        return cn.hutool.core.codec.Base64.encode(str,encodeType);
    }

    /**
     * base64解码
     *
     * @param result
     * @param encodeType
     * @return
     */
    public static String decode3(String result, String encodeType) throws UnsupportedEncodingException {
        return cn.hutool.core.codec.Base64.decodeStr(result,encodeType);
    }

    /*****************************************方式四**********************************************/
    /** 使用org.apache.commons.codec.binary.Base64类实现
     * <dependency>
     *     <groupId>commons-codec</groupId>
     *     <artifactId>commons-codec</artifactId>
     *     <version>1.11</version>
     * </dependency>
     */

    /**
     * base64编码
     *
     * @param str
     * @param encodeType
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String encode4(String str, String encodeType) throws UnsupportedEncodingException {
        return org.apache.commons.codec.binary.Base64.encodeBase64String(str.getBytes(encodeType));
    }

    /**
     * base64解码
     *
     * @param result
     * @param encodeType
     * @return
     */
    public static String decode4(String result, String encodeType) throws UnsupportedEncodingException {
        return new String(org.apache.commons.codec.binary.Base64.decodeBase64(result),encodeType);
    }

}

输出打印:

 

在线测试:成功

微信公众号,感谢关注。持续更新...

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值