码迷,mamicode.com
首页 > 编程语言 > 详细

算法系列(十一)BASE64算法实现和使用说明

时间:2016-07-01 11:57:31      阅读:401      评论:0      收藏:0      [点我收藏+]

标签:

完整的BASE64定义可见RFC 1421RFC 2045。编码后的数据比原始数据略长,为原来的4/3。在电子邮件中,根据RFC 822规定,每76个字符,还需要加上一个回车换行。可以估算编码后数据长度大约为原长的135.1%。

转换的时候,将三个byte的数据,先后放入一个24bit的缓冲区中,先来的byte占高位。数据不足3byte的话,于缓冲器中剩下的bit用0补足。然后,每次取出6(因为26=64)个bit,按照其值选择ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/中的字符作为编码后的输出。不断进行,直到全部输入数据转换完成。

当原数据长度不是3的整数倍时, 如果最后剩下一个输入数据,在编码结果后加2个“=”;如果最后剩下两个输入数据,编码结果后加1个“=”;如果没有剩下任何数据,就什么都不要加,这样才可以保证数据还原的正确性。

例子

举例来说,一段引用自托马斯·霍布斯利维坦》的文句:

Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.

经过Base64编码之后变成:

TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz
IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg
dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu
dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo
ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=
  • 编码“Man”
文本 M a n
ASCII编码 77 97 110
二进制位 0 1 0 0 1 1 0 1 0 1 1 0 0 0 0 1 0 1 1 0 1 1 1 0
索引 19 22 5 46
Base64编码 T W F u

在此例中,Base64算法将三个字符编码为4个字符

Base64索引表:

数值 字符   数值 字符   数值 字符   数值 字符
0 A 16 Q 32 g 48 w
1 B 17 R 33 h 49 x
2 C 18 S 34 i 50 y
3 D 19 T 35 j 51 z
4 E 20 U 36 k 52 0
5 F 21 V 37 l 53 1
6 G 22 W 38 m 54 2
7 H 23 X 39 n 55 3
8 I 24 Y 40 o 56 4
9 J 25 Z 41 p 57 5
10 K 26 a 42 q 58 6
11 L 27 b 43 r 59 7
12 M 28 c 44 s 60 8
13 N 29 d 45 t 61 9
14 O 30 e 46 u 62 +
15 P 31 f 47 v 63 /

如果要编码的字节数不能被3整除,最后会多出1个或2个字节,那么可以使用下面的方法进行处理:先使用0字节值在末尾补足,使其能够被3整除,然后再进行Base64的编码。在编码后的Base64文本后加上一个或两个‘=‘号,代表补足的字节数。也就是说,当最后剩余一个八位字节(一个byte)时,最后一个6位的Base64字节块有四位是0值,最后附加上两个等号;如果最后剩余两个八位字节(2个byte)时,最后一个6位的base字节块有两位是0值,最后附加一个等号。 参考下表:

文本(1 Byte) A

二进制位 0 1 0 0 0 0 0 1















二进制位(补0) 0 1 0 0 0 0 0 1 0 0 0 0











Base64编码 Q Q

文本(2 Byte) B C
二进制位 0 1 0 0 0 0 1 0 0 1 0 0 0 0 1 1

x x x x x x
二进制位(补0) 0 1 0 0 0 0 1 0 0 1 0 0 0 0 1 1 0 0 x x x x x x
Base64编码 Q k M


public class Base64
{
  /**
    * 将原始数据编码为base64编码
    */
    static public char[] encode(byte[] data)
    {
        char[] out = new char[((data.length + 2) / 3) * 4];

        for (int i = 0, index = 0; i < data.length; i += 3, index += 4)
        {
                boolean quad = false;
                boolean trip = false;
                int val = (0xFF & (int) data[i]);
                val <<= 8;
                if ((i + 1) < data.length)
                {
                        val |= (0xFF & (int) data[i + 1]);
                        trip = true;
                }
                val <<= 8;
                if ((i + 2) < data.length)
                {
                        val |= (0xFF & (int) data[i + 2]);
                        quad = true;
                }
                out[index + 3] = alphabet[(quad ? (val & 0x3F) : 64)];
                val >>= 6;
                out[index + 2] = alphabet[(trip ? (val & 0x3F) : 64)];
                val >>= 6;
                out[index + 1] = alphabet[val & 0x3F];
                val >>= 6;
                out[index + 0] = alphabet[val & 0x3F];
        }
        return out;
    }
    /**
    * 将base64编码的数据解码成原始数据
    */
    static public byte[] decode(char[] data)
    {
      int len = ((data.length + 3) / 4) * 3;
      if(data.length > 0 && data[data.length - 1] == '=')
        --len;
      if(data.length > 1 && data[data.length - 2] == '=')
        --len;
      byte[] out = new byte[len];
      int shift = 0;
      int accum = 0;
      int index = 0;
      for(int ix = 0; ix < data.length; ix++)
      {
        int value = codes[data[ix] & 0xFF];
        if(value >= 0)
        {
          accum <<= 6;
          shift += 6;
          accum |= value;
          if(shift >= 8)
          {
            shift -= 8;
            out[index++] = (byte)((accum >> shift) & 0xff);
          }
        }
      }
      if(index != out.length)
        throw new Error("miscalculated data length!");
      return out;
    }

    static private char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".toCharArray();

    static private byte[] codes = new byte[256];
    static {
            for (int i = 0; i < 256; i++)
                    codes[i] = -1;
            for (int i = 'A'; i <= 'Z'; i++)
                    codes[i] = (byte) (i - 'A');
            for (int i = 'a'; i <= 'z'; i++)
                    codes[i] = (byte) (26 + i - 'a');
            for (int i = '0'; i <= '9'; i++)
                    codes[i] = (byte) (52 + i - '0');
            codes['+'] = 62;
            codes['/'] = 63;
    }

    public static void main(String[] args) throws Exception
    {
      //加密成base64
      String strSrc = "林";
      String strOut = new String(Base64.encode(strSrc.getBytes()));
      System.out.println(strOut);

      String strOut2 = new String(Base64.decode(strOut.toCharArray()));
      System.out.println(strOut2);
    }
}


欢迎扫描二维码,关注公众账号
技术分享

算法系列(十一)BASE64算法实现和使用说明

标签:

原文地址:http://blog.csdn.net/robertcpp/article/details/51628647

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!