Base64之java自定义实现

来源:互联网 发布:新网互联域名转移 编辑:程序博客网 时间:2024/06/12 01:11
BASE64是一种编码方式,可以把二进制数据编码为字符形式的数据,是一种可逆的编码方式。

base64的原理就不多说了,网上资料太多,日常所用的话jdk本身或者commons-codec都提供了实现,可以直接调用。

但本着知其然并知其所以然的态度,便尝试着用java来实现了一下base64的编码解码。


package test;import java.io.UnsupportedEncodingException;import java.math.BigInteger;/** * base64 工具类 */public class Base64Utils {  private static String base64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";  private static String add = "=";  /**   * base64编码   */  public static String encode(String str, String charsetName) {    StringBuilder base64Str = new StringBuilder();    byte[] bytesStr;    try {      bytesStr = str.getBytes(charsetName);    } catch (UnsupportedEncodingException e) {      throw new RuntimeException(e);    }    // 编码为二进制字符串    String bytesBinary = binary(bytesStr, 2);    // 24位为一组,不够后面补0,计算出需要补充的个数    int addCount = 0;    while (bytesBinary.length() % 24 != 0) {      bytesBinary += "0";      addCount++;    }    for (int i = 0; i <= bytesBinary.length() - 6; i += 6) {      // 二进制转十进制      int index = Integer.parseInt(bytesBinary.substring(i, i + 6), 2);      // 如果是有补位的6个0组成,则转换为'='      if (index == 0 && i >= bytesBinary.length() - addCount) {        base64Str.append(add);      } else {        base64Str.append(base64Table.charAt(index));      }    }    return base64Str.toString();  }  /**   * base64解码   */  public static String decode(String base64str, String charsetName) {    String base64Binarys = "";    for (int i = 0; i < base64str.length(); i++) {      char s = base64str.charAt(i);      if (s != '=') {        // 十进制转二进制        String binary = Integer.toBinaryString(base64Table.indexOf(s));        // 不够六位进行补位        while (binary.length() != 6) {          binary = "0" + binary;        }        base64Binarys += binary;      }    }    // 长度应该是8的倍数,去除后面多余的0    base64Binarys = base64Binarys.substring(0, base64Binarys.length() - base64Binarys.length() % 8);    byte[] bytesStr = new byte[base64Binarys.length() / 8];    for (int bytesIndex = 0; bytesIndex < base64Binarys.length() / 8; bytesIndex++) {      // 八位截取一次,转化为一个字节      bytesStr[bytesIndex] = (byte) Integer.parseInt(base64Binarys.substring(bytesIndex * 8, bytesIndex * 8 + 8), 2);    }    try {      return new String(bytesStr, charsetName);    } catch (UnsupportedEncodingException e) {      throw new RuntimeException(e);    }  }  /**   * 字节数组转自定义进制字符串   */  public static String binary(byte[] bytes, int radix) {    // 转化为二进制字符串   1代表正数,如果第一个字节以0开头,转化后会省略,所以要重新补位    String strBytes = new BigInteger(1, bytes).toString(radix);    while (strBytes.length() % 8 != 0) {      strBytes = "0" + strBytes;    }    return strBytes;  }}



0 0
原创粉丝点击