Base64算法不一致可能会导致的坑

简述
笔者工作中常用的Base64算法的实现有三种方式,第一种是sun公司提供的Base64算法,第二种是bouncycastle提供的加密算法(以下简称BC包),第三种是apache的codec包(以下简称codec包)不推荐使用第一种,这些api在未来的jdk发行版本中是可能被删除的。
Base64
背景:两个公司直接互相通信,双方约定加密方式,涉及数字签名、数字证书等,约定非敏感信息使用Base64算法编码。我方公司选用bouncycastle包的base64算法,对方公司选择了codec包的base64加密算法。

   /**
     * 我方公司加密方法
     * @param str
     * @return
     * @throws Exception
     */
    private static String bouncycastleBase64Test(String str) throws Exception {
        byte[] bcBase64 = org.bouncycastle.util.encoders.Base64.encode(str.getBytes());
        String code = new String(bcBase64);
        System.out.println("BC包默认:    " + code);
        return code;
    }

    /**
    * 对方公司解密方法
     * @param str
     * @return
     * @throws Exception
     */
    private static String codecBase64Test(String str) throws Exception {
        byte[] bcBase64 = org.apache.commons.codec.binary.Base64.encodeBase64(str.getBytes(), true);
        String code = new String(bcBase64);
        System.out.println("codec包默认:" + code);
        return code;
    }

   public static void main(String[] args) throws Exception {
        String str = "加密字符串";
        String s1 = bouncycastleBase64Test(str);
        String s2 = codecBase64Test(str);
        System.out.println(s1.equals(s2));
    }

运行结果:Base64算法不一致可能会导致的坑
同样是base54加密算法,得出的结果竟然不一致
字符串完全一样,通过debug可以发现codec下面有一个换行符!
Base64算法不一致可能会导致的坑

这是因为codec提供了RFC2045标准的Base64实现,每76个字符加一个换行符,如果当前行不足76个字符,也要在最后加上加换行符!上面代码codec的Base64实现方法encodeBase64(str.getBytes(), true),第二个boolean类型的参数的意思是:是否使用RFC2045标准的Base64实现,也就是说如果为true,最后是有换行符的,如果为false,最后就没有换行符!

Base64编码
背景:我方公司和对方公司同样使用同一个算法,比如bouncycastle的Base64编码,但是双方编码是不一致的,同样会导致加密得出的编码不一致。

public static void main(String[] args) throws Exception {
    String str = "加密字符串111";
    bouncycastleBase64WithUtf8Test(str);
    bouncycastleBase64WithGBKTest(str);
}
private static void bouncycastleBase64WithUtf8Test(String str) throws Exception {
    byte[] bcBase64 = org.bouncycastle.util.encoders.Base64.encode(str.getBytes("UTF-8"));
    String code = new String(bcBase64);
    System.out.println("BC包utf8编码:" + code);
}
private static void bouncycastleBase64WithGBKTest(String str) throws Exception {
    byte[] bcBase64 = org.bouncycastle.util.encoders.Base64.encode(str.getBytes("GBK"));
    String code = new String(bcBase64);
    System.out.println("BC包GBK编码:" + code);
}

输出结果:

 BC包utf8编码:5Yqg5a+G5a2X56ym5LiyMTEx
 BC包GBK编码:vNPD3NfWt/u0rjExMQ==

同样,使用不同字符编码的加密解密也是有问题的,如下面代码所示:

private static void bouncycastleBase6Test(String str) throws Exception {
    byte[] encodeBase64 = org.bouncycastle.util.encoders.Base64.encode(str.getBytes("UTF-8"));
    String code = new String(encodeBase64);
    System.out.println("BC包utf8编码:" + code);
    byte[] decodeBase64 = org.bouncycastle.util.encoders.Base64.decode(str.getBytes("GBK"));
    System.out.println("BC包GBK解码:" + decodeBase64);
}

使用utf-8编码,使用gbk解码,在base64解密的时候报错了

BC包utf8编码:5Yqg5a+G5a2X56ym5LiyMTEx
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -68
at org.bouncycastle.util.encoders.Base64Encoder.decode(Unknown Source)
at org.bouncycastle.util.encoders.Base64.decode(Unknown Source)
at com.jiupai.wallstreet.biz.Test.bouncycastleBase64WithUtf8Test(Test.java:93)
at com.jiupai.wallstreet.biz.Test.main(Test.java:26)

URLBase64
Base64中的“/”和“=”在url中使用是可能出现问题的,所以BC包和codec包采取了不同的应对方式,BC包使用了.来作为填充符号,而codec包直接放弃了填充符号。
如下代码:

  private static void test3(String str) throws UnsupportedEncodingException {
    String str = "加密字符……%&*1";
    System.out.println(str);
    byte[] bytes = org.bouncycastle.util.encoders.UrlBase64.encode(str.getBytes("UTF-8"));
    byte[] s1 = org.apache.commons.codec.binary.Base64.encodeBase64URLSafe(str.getBytes("UTF-8"));
    System.out.println("bouncycastle URLbase64加密后:"+new String(bytes));
    System.out.println("codec URLbase64加密后       :"+new String(s1));
    byte[] bytes4 = org.bouncycastle.util.encoders.UrlBase64.decode(s1);
    byte[] bytes3 = org.apache.commons.codec.binary.Base64.decodeBase64(bytes);
    System.out.println("使用codec URLbase64加密,使用BC URLbase64加密解密:" + new String(bytes4));
    System.out.println("使用BC URLbase64加密,使用codec URLbase64加密解密:" + new String(bytes3));
}

结果:

加密字符……%&*1
bouncycastle URLbase64加密后:5Yqg5a-G5a2X56ym4oCm4oCmJSYqMQ..
codec URLbase64加密后       :5Yqg5a-G5a2X56ym4oCm4oCmJSYqMQ
使用codec URLbase64加密,使用BC URLbase64加密解密:加密字符……%&*b�
使用BC URLbase64加密,使用codec URLbase64加密解密:加密字符……%&*1

可以看到BC包使用了“.”作为填充符号,而codec包却没有使用填充符号,这时使用codec的URLBase64编码,使用BC URLbase64加密解码是可能出问题的,而使用BC包的URLbase64编码,使用codec URLbase64解码没有出问题。

总结
1、使用BC包Base64加密,使用codec包的RFC2045标准的Base64方法解密,会导致解密不一致。
2、即使是同一种Base64加密算法,不同字符编码会导致生成的密文不一致。
3、即使是同一种Base64加密算法,使用不同字符编码加密解密会出问题。
4、对于URLBase64编码,BC包和codec包的实现不同,对于生成的密文,BC包的密文最后可能会有填充符号“.”,而codec舍弃了填充符号,会导致不一致,有趣的是:使用codec包的URLBase64编码,使用BC包的URLbase64解码可能出问题;使用BC包的URLbase64编码,使用codec URLbase64解码不会出问题。