Java Base64 介绍

发布时间: 更新时间: 总字数:813 阅读时间:2m 作者: IP上海 分享 网址

Base64编码的主要用途在于:将二进制编码后,就变成了文本形式的可见字符,可以用于xml传输以及存入数据库。

介绍

Base64编码的主要用途在于:将二进制编码后,就变成了文本形式的可见字符,可以用于xml传输以及存入数据库。英文的解析更清楚:

Base64 encoding schemes are commonly used when there is a need to encode binary data that needs to be stored and transferred over media that are designed to deal with textual data. This is to ensure that the data remain intact without modification during transport. Base64 is commonly used in a number of applications including email via MIME, and storing complex data in XML.

When the number of bytes to encode is not divisible by 3 (that is, if there are only one or two bytes of input for the last block), then the following action is performed: Add extra bytes with value zero so there are three bytes, and perform the conversion to base64. If there was only one significant input byte, only the first two base64 digits are picked, and if there were two significant input bytes, the first three base64 digits are picked. ‘=’ characters might be added to make the last block contain four base64 characters. As a result: When the last group contains one octet, the four least significant bits of the final 6-bit block are set to zero; and when the last group contains two octets, the two least significant bits of the final 6-bit block are set to zero.

The ‘==’ sequence indicates that the last group contained only 1 byte, and ‘=’ indicates that it contained 2 bytes. The example below illustrates how truncating the input of the whole of the above quote changes the output padding:

  1. 三个ascii值最后被转换成了4个可见字符,所以可以知道如果原字符串能被3整除,那么就ok,如果不能整除呢?就补0!两种情况:一种是原来是1个字符(在结果末尾跟上==),一种是原来是2个字符(在结果末尾跟上=)。

  2. 为什么原来是一个字符时,最后补了两个“==”?而原来是两个字符时,最后补了一个“=”?因为就是最后的结果要被4整除,也就是说经过base64编码后的结果肯定是4的倍数!但是长度是不定的!

Java 代码

package cn.xiexianbin.u;

public class Base64Demo {

    private final static String base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    public static String encode(String s) {

        // the result/encoded string, the padding string, and the pad count
        String r = "", p = "";
        int c = s.length() % 3;

        // add a right zero pad to make this string a multiple of 3 characters
        if (c > 0) {
            for (; c < 3; c++) {
                p += "=";
                s += "\0";
            }
        }

        // increment over the length of the string, three characters at a time
        for (c = 0; c < s.length(); c += 3) {

            // we add newlines after every 76 output characters, according to
            // the MIME specs
            if (c > 0 && (c / 3 * 4) % 76 == 0)
                r += "\r\n";

            // these three 8-bit (ASCII) characters become one 24-bit number
            int n = (s.charAt(c) << 16) + (s.charAt(c + 1) << 8) + (s.charAt(c + 2));

            // this 24-bit number gets separated into four 6-bit numbers
            int n1 = (n >> 18) & 63, n2 = (n >> 12) & 63, n3 = (n >> 6) & 63, n4 = n & 63;

            // those four 6-bit numbers are used as indices into the base64
            // character list
            r += "" + base64chars.charAt(n1) + base64chars.charAt(n2) + base64chars.charAt(n3) + base64chars.charAt(n4);
        }

        return r.substring(0, r.length() - p.length()) + p;
    }

    public static void main(String[] args) {
        String originStr = "ab";
        System.out.println(Base64Demo.encode(originStr));
    }

}

参考

  1. http://en.wikibooks.org/wiki/Algorithm_implementation/Miscellaneous/Base64
  2. http://en.wikipedia.org/wiki/Base64
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数