java base64编码、解码的三种方式

1.用法介绍

方式一:DatatypeConverter

  说明:使用jdk自带的DatatypeConverter.java类实现,但是jdk版本必须>=1.6。

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

  编码

/**
 * base64编码之方法一
 * @explain DatatypeConverter.java实现
 * @param str
 *            待编码字符串
 * @return 编码字符串
 */
public static String encode(String str) {
    // base64字符串
    String base64Str = "" ;
    try {
        // String-->byte[]
        byte [] data = str.getBytes("utf-8" );
        // 编码
        base64Str = DatatypeConverter.printBase64Binary(data);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return base64Str;
}

  解码

/**
 * base64解码之方法一
 * @explain DatatypeConverter.java实现
 * @param base64Str
 *            待解码字符串
 * @return 解码字符串
 */
public static String decode(String base64Str) {
    // 解码后的字符串
    String str = "" ;
    // 解码
    byte [] base64Data = DatatypeConverter.parseBase64Binary(base64Str);
    try {
        // byte[]-->String
        str = new String(base64Data, "utf-8" );
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return str;
}

方式二:Base64

  说明:使用jdk自带的Base64.java类实现,但是jdk版本必须>=1.8。

      import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.nio.charset.StandardCharsets;

  编码

/**
 * base64编码之方法二
 * @explain Base64.java实现
 * @param str
 *            待编码字符串
 * @return 编码字符串
 */
public static String encode2(String str) {
    // String-->byte[]
    byte [] data = str.getBytes(StandardCharsets.UTF_8);
    // 编码(base64字符串)
    String base64Str = Base64.getEncoder().encodeToString(data);
    return base64Str;
}

  解码

/**
 * base64解码之方法二
 * @explain Base64.java实现
 * @param base64Str
 *            待解码字符串
 * @return 解码字符串
 */
public static String decode2(String base64Str) {
    // 解码
    byte [] base64Data = Base64.getDecoder().decode(base64Str);
    // byte[]-->String(解码后的字符串)
    String str = new String(base64Data, StandardCharsets.UTF_8);
    return str;
}

方式三:Base64

  所需jar包:commons-codec.jar

<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.15</version>
</dependency>
      import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.binary.Base64;
import java.nio.charset.StandardCharsets;

  编码

/**
 * base64编码之方法三
 * @explain Base64.java实现
 * @param str
 *            待编码字符串
 * @return 编码字符串
 */
public static String encode3(String str) {
    // String-->byte[]
    byte [] data = str.getBytes(StandardCharsets.UTF_8);
    // 编码(base64字符串)
    String base64Str =Base64.encodeBase64String(data).replaceAll("\r\n" , "");
    return base64Str;
}

  解码

/**
 * base64解码之方法三
 * @explain Base64.java实现
 * @param base64Str
 *            待解码字符串
 * @return 解码字符串
 */
public static String decode3(String base64Str) {
    // 解码
    byte [] base64Data = Base64.decodeBase64(base64Str);
    // byte[]-->String(解码后的字符串)
    String str = new String(base64Data, StandardCharsets.UTF_8);
    return str;
}

  2022年9月23日10:21:00

/*
 * base64编码(不补位)
 * @explain: 不进行多余的=填充
 * @date: 2020年07月13日 0013 10:39
 * @param: str
 * @return: java.lang.String base码
 */
@NotNull
public static String encodeURLSafe(String str) {
    if (StringUtils.isEmpty(str)) return "";

    log.debug("base64待编码字符串:\n" + str);
    // 字符串转base64
    byte[] byteArray = str.getBytes(StandardCharsets.UTF_8);
    // 编码
    String result = encodeURLSafe(byteArray);
    log.debug("base64编码结果:\n" + result);
    return result;
}

/*
 * 二进制转base64
 * @description: 3位一组,当最后一组不足3位时,不用=号填充
 * @date: 2022/3/1 21:00
 * @param: bytes
 * @return: java.lang.String
 */
@NotNull
public static String encodeURLSafe(byte[] bytes) {
    if (ByteUtils.isEmpty(bytes)) return "";
    return Base64.encodeBase64URLSafeString(bytes);
}

 20200824

方式四:

  所需jar包:tomcat-coyote.jar

  编码与解码方式与方式三一样,这里不再赘述。

20200827

方式五:

  所需jar包:spring-core.jar

  用法大同小异,不再赘述。

2022年9月22日18:21:42

方式五,不能将解决自动补位的问题

String str = "Marydon";
System.out.println(org.springframework.util.Base64Utils.encodeToUrlSafeString(str.getBytes(StandardCharsets.UTF_8)));

 

另外,需要说明的是关于base64编码与解码的方式不只有这几种方式, 一般情况下,只有jar包中集成的base64相关的class类,就可以直接导包引用。

2.测试

   public static void main(String[] args) {
    String str =    "http://www.cnblogs.com/Marydon20170307" ;
    String base64 = Base64Utils.encode(str);   // aHR0cDovL3d3dy5jbmJsb2dzLmNvbS9NYXJ5ZG9uMjAxNzAzMDc=
    System.out.println(base64);
    String str2 = Base64Utils.decode(base64);
    System.out.println(str.equals(str2));   // true
}

3.效率比较

  快-->慢:方式二 > 方式一 > 方式三

  因此,如果项目用的是jdk1.8,最佳选择是方式二;jdk1.6,最佳选择是方式一。

20200824

4.只知其一不知其二

  用了这么久的base64,但是,在前段时间的接口对接过程中,却因为它栽了跟头,有必要进行一次总结回顾。

  base64,我们通常的用途有两种:第一种,图片转码;第二种,作为数据加密的方式、步骤之一。

  encodeBase64和encodeBase64URLSafe

  一般情况下,我们只用到了第一个,没有用到第二个,绝大部分人用的也是第一个,网站在线解析也是如此,这就造成了我们只知其一不知其二。

  二者区别:

  encodeBase64会对字符串3位一组自动补全,因而最后可能会出现 == 或者 =(缺一个补一个=,缺两个补两个=);

  encodeBase64URLSafe则是按照字符串实际位数进行加密,不补全,不会出现 == 或者 =

  举个例子:

  待编码字符串:Marydon,编码后分别是:TWFyeWRvbg、TWFyeWRvbg==。

  上面的区别介绍是从网上找到的,但并没有说全,再看另外两个例子

待编码字符串:
{                "RegionCode" :                "410000" ,                "IvcAgcCode" :                "507403" ,                "Items" :[{                "ItemCode" :                "913001001" ,                "ItemName" :                "诊察费" ,                "IvcType" :                "1" ,                "Stands" :[{                "UpLimit" :                0 ,                "StandCode" :                "001" ,                "LowerLimit" :                0 ,                "Unit" :                "元" ,                "StandName" :                "诊察费" }]},{                "ItemCode" :                "913001002" ,                "ItemName" :                "检查费" ,                "IvcType" :                "1" ,                "Stands" :[{                "UpLimit" :                0 ,                "StandCode" :                "001" ,                "LowerLimit" :                0 ,                "Unit" :                "元" ,                "StandName" :                "检查费" }]},{                "ItemCode" :                "913001003" ,                "ItemName" :                "化验费" ,                "IvcType" :                "1" ,                "Stands" :[{                "UpLimit" :                0 ,                "StandCode" :                "001" ,                "LowerLimit" :                0 ,                "Unit" :                "元" ,                "StandName" :                "化验费" }]},{                "ItemCode" :                "913001004" ,                "ItemName" :                "治疗费" ,                "IvcType" :                "1" ,                "Stands" :[{                "UpLimit" :                0 ,                "StandCode" :                "001" ,                "LowerLimit" :                0 ,                "Unit" :                "元" ,                "StandName" :                "治疗费" }]},{                "ItemCode" :                "913001005" ,                "ItemName" :                "手术费" ,                "IvcType" :                "1" ,                "Stands" :[{                "UpLimit" :                0 ,                "StandCode" :                "001" ,                "LowerLimit" :                0 ,                "Unit" :                "元" ,                "StandName" :                "手术费" }]},{                "ItemCode" :                "913001006" ,                "ItemName" :                "卫生材料费" ,                "IvcType" :                "1" ,                "Stands" :[{                "UpLimit" :                0 ,                "StandCode" :                "001" ,                "LowerLimit" :                0 ,                "Unit" :                "元" ,                "StandName" :                "卫生材料费" }]},{                "ItemCode" :                "913001007" ,                "ItemName" :                "西药费" ,                "IvcType" :                "1" ,                "Stands" :[{                "UpLimit" :                0 ,                "StandCode" :                "001" ,                "LowerLimit" :                0 ,                "Unit" :                "元" ,                "StandName" :                "西药费" }]},{                "ItemCode" :                "913001008" ,                "ItemName" :                "中药饮片" ,                "IvcType" :                "1" ,                "Stands" :[{                "UpLimit" :                0 ,                "StandCode" :                "001" ,                "LowerLimit" :                0 ,                "Unit" :                "元" ,                "StandName" :                "中药饮片" }]},{                "ItemCode" :                "913001009" ,                "ItemName" :                "中成药费" ,                "IvcType" :                "1" ,                "Stands" :[{                "UpLimit" :                0 ,                "StandCode" :                "001" ,                "LowerLimit" :                0 ,                "Unit" :                "元" ,                "StandName" :                "中成药费" }]},{                "ItemCode" :                "913001010" ,                "ItemName" :                "挂号费" ,                "IvcType" :                "1" ,                "Stands" :[{                "UpLimit" :                0 ,                "StandCode" :                "001" ,                "LowerLimit" :                0 ,                "Unit" :                "元" ,                "StandName" :                "挂号费" }]},{                "ItemCode" :                "913001011" ,                "ItemName" :                "一般诊疗费" ,                "IvcType" :                "1" ,                "Stands" :[{                "UpLimit" :                0 ,                "StandCode" :                "001" ,                "LowerLimit" :                0 ,                "Unit" :                "元" ,                "StandName" :                "一般诊疗费" }]},{                "ItemCode" :                "913001012" ,                "ItemName" :                "其他门诊费" ,                "IvcType" :                "1" ,                "Stands" :[{                "UpLimit" :                0 ,                "StandCode" :                "001" ,                "LowerLimit" :                0 ,                "Unit" :                "元" ,                "StandName" :                "其他门诊费" }]},{                "ItemCode" :                "913002001" ,                "ItemName" :                "床位费" ,                "IvcType" :                "2" ,                "Stands" :[{                "UpLimit" :                0 ,                "StandCode" :                "001" ,                "LowerLimit" :                0 ,                "Unit" :                "元" ,                "StandName" :                "床位费" }]},{                "ItemCode" :                "913002002" ,                "ItemName" :                "诊察费" ,                "IvcType" :                "2" ,                "Stands" :[{                "UpLimit" :                0 ,                "StandCode" :                "001" ,                "LowerLimit" :                0 ,                "Unit" :                "元" ,                "StandName" :                "诊察费" }]},{                "ItemCode" :                "913002003" ,                "ItemName" :                "检查费" ,                "IvcType" :                "2" ,                "Stands" :[{                "UpLimit" :                0 ,                "StandCode" :                "001" ,                "LowerLimit" :                0 ,                "Unit" :                "元" ,                "StandName" :                "检查费" }]},{                "ItemCode" :                "913002004" ,                "ItemName" :                "化验费" ,                "IvcType" :                "2" ,                "Stands" :[{                "UpLimit" :                0 ,                "StandCode" :                "001" ,                "LowerLimit" :                0 ,                "Unit" :                "元" ,                "StandName" :                "化验费" }]},{                "ItemCode" :                "913002005" ,                "ItemName" :                "治疗费" ,                "IvcType" :                "2" ,                "Stands" :[{                "UpLimit" :                0 ,                "StandCode" :                "001" ,                "LowerLimit" :                0 ,                "Unit" :                "元" ,                "StandName" :                "治疗费" }]},{                "ItemCode" :                "913002006" ,                "ItemName" :                "手术费" ,                "IvcType" :                "2" ,                "Stands" :[{                "UpLimit" :                0 ,                "StandCode" :                "001" ,                "LowerLimit" :                0 ,                "Unit" :                "元" ,                "StandName" :                "手术费" }]},{                "ItemCode" :                "913002007" ,                "ItemName" :                "护理费" ,                "IvcType" :                "2" ,                "Stands" :[{                "UpLimit" :                0 ,                "StandCode" :                "001" ,                "LowerLimit" :                0 ,                "Unit" :                "元" ,                "StandName" :                "护理费" }]},{                "ItemCode" :                "913002008" ,                "ItemName" :                "卫生材料费" ,                "IvcType" :                "2" ,                "Stands" :[{                "UpLimit" :                0 ,                "StandCode" :                "001" ,                "LowerLimit" :                0 ,                "Unit" :                "元" ,                "StandName" :                "卫生材料费" }]},{                "ItemCode" :                "913002009" ,                "ItemName" :                "西药费" ,                "IvcType" :                "2" ,                "Stands" :[{                "UpLimit" :                0 ,                "StandCode" :                "001" ,                "LowerLimit" :                0 ,                "Unit" :                "元" ,                "StandName" :                "西药费" }]},{                "ItemCode" :                "913002010" ,                "ItemName" :                "中药饮片" ,                "IvcType" :                "2" ,                "Stands" :[{                "UpLimit" :                0 ,                "StandCode" :                "001" ,                "LowerLimit" :                0 ,                "Unit" :                "元" ,                "StandName" :                "中药饮片" }]},{                "ItemCode" :                "913002011" ,                "ItemName" :                "中成药费" ,                "IvcType" :                "2" ,                "Stands" :[{                "UpLimit" :                0 ,                "StandCode" :                "001" ,                "LowerLimit" :                0 ,                "Unit" :                "元" ,                "StandName" :                "中成药费" }]},{                "ItemCode" :                "913002012" ,                "ItemName" :                "一般诊疗费" ,                "IvcType" :                "2" ,                "Stands" :[{                "UpLimit" :                0 ,                "StandCode" :                "001" ,                "LowerLimit" :                0 ,                "Unit" :                "元" ,                "StandName" :                "一般诊疗费" }]},{                "ItemCode" :                "913002013" ,                "ItemName" :                "其他住院费用" ,                "IvcType" :                "2" ,                "Stands" :[{                "UpLimit" :                0 ,                "StandCode" :                "001" ,                "LowerLimit" :                0 ,                "Unit" :                "元" ,                "StandName" :                "其他住院费用" }]}]}
encodeBase64编码结果:
eyJSZWdpb25Db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CwiU3RhbmRDb2RlIjoiMDAxIiwiTG93ZXJMaW1pdCI6MCwiVW5pdCI6IuWFgyIsIlN0YW5kTmFtZSI6IuaMguWPt-i0uSJ9XX0seyJJdGVtQ29kZSI6IjkxMzAwMTAxMSIsIkl0ZW1OYW1lIjoi5LiA6Iis6K-K55aX6LS5IiwiSXZjVHlwZSI6IjEiLCJTdGFuZHMiOlt7IlVwTGltaXQiOjAsIlN0YW5kQ29kZSI6IjAwMSIsIkxvd2VyTGltaXQiOjAsIlVuaXQiOiLlhYMiLCJTdGFuZE5hbWUiOiLkuIDoiKzor4rnlpfotLkifV19XX0
base64待编码字符串:
{                "Type" :                1 ,                "PayerPartyType" :                "1" ,                "RegionCode" :                "410000" ,                "SerialNumber" :                "0000007519" ,                "IvcAgcCode" :                "506402" ,                "MainExt" :{                "OtherPayAmount" :                "0" ,                "MedicalDate" :                "20190117" ,                "FundPayAmount" :                "0" ,                "MedicalType" :                "门诊" ,                "MedicalInsuranceType" :                "新农合" ,                "AccountPayAmount" :                "0" ,                "Gender" :                "男" ,                "OwnPayAmount" :                "15.00" ,                "SelfpaymentAmount" :                "0" ,                "MedicalInsuranceID" :                "104381991001" ,                "OrgType" :                "综合医院" ,                "PatientNumber" :                "1000100201" ,                "SelfpaymentCost" :                "0" },                "PayerPartyName" :                "马露" ,                "RepCreater" :                "李四" ,                "Items" :[{                "ItemCode" :                "9940301020" ,                "ItemRemark" :[],                "ItemQuantity" :                "1" ,                "Amount" :                "10.00" ,                "ItemName" :                "检查费" ,                "ItemStd" :                "10.00" ,                "ItemUnit" :[],                "AuxItem" :[{                "AuxItemQuantity" :                "1" ,                "AuxItemAmount" :                "10.0000" ,                "AuxItemStd" :                "10.0000" ,                "AuxItemRelatedCode" :                "9940301020" ,                "AuxItemUnit" :                "人次" ,                "AuxItemRemark" :                "无自付" ,                "AuxItemRelatedName" :                "检查费" ,                "AuxItemName" :                "角膜曲率检查" }]},{                "ItemCode" :                "9940301070" ,                "ItemRemark" :[],                "ItemQuantity" :                "1" ,                "Amount" :                "5.00" ,                "ItemName" :                "西药费" ,                "ItemStd" :                "5.00" ,                "ItemUnit" :[],                "AuxItem" :[{                "AuxItemQuantity" :                "1" ,                "AuxItemAmount" :                "5.0000" ,                "AuxItemStd" :                "5.0000" ,                "AuxItemRelatedCode" :                "9940301070" ,                "AuxItemUnit" :                "支" ,                "AuxItemRemark" :                "无自付" ,                "AuxItemRelatedName" :                "西药费" ,                "AuxItemName" :                "复方托吡卡胺滴眼液" }]}],                "TotalAmount" :                "15.00" ,                "PayerPartyCode" :[]}
base64编码结果:
eyJUeXBlIjoxLCJQYXllclBhcnR5VHlwZSI6IjEiLCJSZWdpb25Db2RlIjoiNDEwMDAwIiwiU2VyaWFsTnVtYmVyIjoiMDAwMDAwNzUxOSIsIkl2Y0FnY0NvZGUiOiI1MDY0MDIiLCJNYWluRXh0Ijp7Ik90aGVyUGF5QW1vdW50IjoiMCIsIk1lZGljYWxEYXRlIjoiMjAxOTAxMTciLCJGdW5kUGF5QW1vdW50IjoiMCIsIk1lZGljYWxUeXBlIjoi6Zeo6K-KIiwiTWVkaWNhbEluc3VyYW5jZVR5cGUiOiLmlrDlhpzlkIgiLCJBY2NvdW50UGF5QW1vdW50IjoiMCIsIkdlbmRlciI6IueUtyIsIk93blBheUFtb3VudCI6IjE1LjAwIiwiU2VsZnBheW1lbnRBbW91bnQiOiIwIiwiTWVkaWNhbEluc3VyYW5jZUlEIjoiMTA0MzgxOTkxMDAxIiwiT3JnVHlwZSI6Iue7vOWQiOWMu-mZoiIsIlBhdGllbnROdW1iZXIiOiIxMDAwMTAwMjAxIiwiU2VsZnBheW1lbnRDb3N0IjoiMCJ9LCJQYXllclBhcnR5TmFtZSI6IumprOmcsiIsIlJlcENyZWF0ZXIiOiLmnY7lm5siLCJJdGVtcyI6W3siSXRlbUNvZGUiOiI5OTQwMzAxMDIwIiwiSXRlbVJlbWFyayI6W10sIkl0ZW1RdWFudGl0eSI6IjEiLCJBbW91bnQiOi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nOabsueOh+ajgOafpSJ9XX0seyJJdGVtQ29kZSI6Ijk5NDAzMDEwNzAiLCJJdGVtUmVtYXJrIjpbXSwiSXRlbVF1YW50aXR5IjoiMSIsIkFtb3VudCI6IjUuMDAiLCJJdGVtTmFtZSI6Iuilv+iNr+i0uSIsIkl0ZW1TdGQiOiI1LjAwIiwiSXRlbVVuaXQiOltdLCJBdXhJdGVtIjpbeyJBdXhJdGVtUXVhbnRpdHkiOiIxIiwiQXV4SXRlbUFtb3VudCI6IjUuMDAwMCIsIkF1eEl0ZW1TdGQiOiI1LjAwMDAiLCJBdXhJdGVtUmVsYXRlZENvZGUiOiI5OTQwMzAxMDcwIiwiQXV4SXRlbVVuaXQiOiLmlK8iLCJBdXhJdGVtUmVtYXJrIjoi5peg6Ieq5LuYIiwiQXV4SXRlbVJlbGF0ZWROYW1lIjoi6KW/6I2v6LS5IiwiQXV4SXRlbU5hbWUiOiLlpI3mlrnmiZjlkKHljaHog7rmu7TnnLzmtrIifV19XSwiVG90YWxBbW91bnQiOiIxNS4wMCIsIlBheWVyUGFydHlDb2RlIjpbXX0=  

  总的来说,二者区别有:

  base64,编码可能会生成+,/,=,三种可能同时存在,也可能都不存在;

  urlsafe,编码可能会生成-,最多有且只有这一种符号。  

  以后,谁再跟你谈标准的base64格式,你就可以锤他啦。

  在上面介绍的5种方式中,方式三、方式四都支持base64URLSafe()方法。

2022年9月22日18:14:26

Base64加密补位规则

  所谓Base64,就是说选出64个字符----小写字母a-z、大写字母A-Z、数字0-9、符号"+"、"/"(再加上作为垫字的"=",实际上是65个字符)----作为一个基本字符集。然后,其他所有符号都转换成这个字符集中的字符。

  转换方式可以分为四步:

  第一步,将每三个字节作为一组,一共是24个二进制位。
  第二步,将这24个二进制位分为四组,每个组有6个二进制位。
  第三步,在每组前面加两个00,扩展成32个二进制位,即四个字节。
  第四步,根据下表,得到扩展后的每个字节的对应符号,这就是Base64的编码值。

  Base64将三个字节转化成四个字节,因此Base64编码后的文本,会比原文本大出三分之一左右。

  此时如果字节数不足三,则这样处理:

  a)二个字节的情况:将这二个字节的一共16个二进制位,按照上面的规则,转成三组,最后一组除了前面加两个0以外,后面也要加两个0。这样得到一个三位的Base64编码,再在末尾补上一个"="号。
  比如,“Ma"这个字符串是两个字节,可以转化成三组00010011、00010110、00010000以后,对应Base64值分别为T、W、E,再补上一个”=“号,因此"Ma"的Base64编码就是TWE=。
  b)一个字节的情况:将这一个字节的8个二进制位,按照上面的规则转成二组,最后一组除了前面加二个0以外,后面再加4个0。这样得到一个二位的Base64编码,再在末尾补上两个”="号。
  比如,“M"这个字母是一个字节,可以转化为二组00010011、00010000,对应的Base64值分别为T、Q,再补上二个”="号,因此"M"的Base64编码就是TQ==。

 

写在最后

  哪位大佬如若发现文章存在纰漏之处或需要补充更多内容,欢迎留言!!!

 相关推荐:

posted @ 2018-07-24 11:31  Marydon  阅读(23070)  评论(0编辑  收藏  举报