BASE64加密算法的java实现

本文详细介绍了Base64编码解码的基本原理,包括编码和解码规则,并提供了Java实现示例。此外还展示了如何使用Apache Commons Codec库简化Base64操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自: https://blog.youkuaiyun.com/zyhlwzy/article/details/77964763

Base64加密算法编码规则

Base64编码的思想是采用64个基本的ASCII码字符对数据进行重新编码。他将需要编码的数据拆分成字节数组,以三个字节为一组。按顺序排列24位数据,再把这24位数据分成四组,即每组6位。再在每组的最高位前补两个0凑足一个字节。这样就把一个3字节为一组的数据重新编码成了4个字节。当所要编码的数据的字节数不是3的整数倍,也就是说在分组时最后一组不足3个字节。这时在最后一组填充1到2个0字节。并在最后编码完成后在结尾添加1到2个‘=’。

例如:将RON进行Base64编码:

首先取RON对应的ASCII码值:R(82)O(79)N(78);
再取二进制值:R(01010010)O(01001111)N(01001110);
然后把这三个字节的二进制码接起来(010100100100111101001110);
再以6位为单位分成4个数据块,并在最高位填充两个0后形成4个字节的编码后的值,(00010100)(00100100)(00111101)(00001110);
再把这四个字节转换为10进制数据得到:(20)(36)(61)(14),这里算出来的值实际上是数据在字符表中的索引。
最后根据BASE64给出的64个基本字符表,查出对应的ASCII码字符:(U)(k)(9)(O)。

注:BASE64字符表:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=

Base64加密算法解码规则

解码过程就是把4个字节再还原成3个字节再根据不同的数据形式把字节数组重新整理成数据。

例如:将Uk9O解码

首先取Uk9O在编码表中的索引:U(20)k(36)9(61)O(14);
再取二进制值:U(00010100)k(00100100)9(00111101)O(00001110);
然后将四个字节的二进制码高位两个0去掉然后拼接起来:(010100100100111101001110)
再以8位为单位分成3个数据块:(01010010)(01001111)(01001110);
再把3个字节转换为10进制数据得到:(82)(79)(78),得到的十进制数据即为字符的ASCII码。
根据ASCII码查出字符:RON,这里是ASCII码表中的实际字符,并非我们定义的由65个字符组成的编码表。

Base64编码解码java实现

package ron.blog.blog_common.custom;

/**

  • @Comment 自定义实现BASE64编码和解码

  • @Author Ron

  • @Date 2017年9月13日 上午9:53:39

  • @return
    */
    public class CustomBase64 {

    /**

    • 编码表
      */
      private static final byte[] encodingTable = {
      (byte) ‘A’, (byte) ‘B’, (byte) ‘C’, (byte) ‘D’, (byte) ‘E’, (byte) ‘F’, (byte) ‘G’, (byte) ‘H’, (byte) ‘I’, (byte) ‘J’,
      (byte) ‘K’, (byte) ‘L’, (byte) ‘M’, (byte) ‘N’, (byte) ‘O’, (byte) ‘P’, (byte) ‘Q’, (byte) ‘R’, (byte) ‘S’, (byte) ‘T’,
      (byte) ‘U’, (byte) ‘V’, (byte) ‘W’, (byte) ‘X’, (byte) ‘Y’, (byte) ‘Z’, (byte) ‘a’, (byte) ‘b’, (byte) ‘c’, (byte) ‘d’,
      (byte) ‘e’, (byte) ‘f’, (byte) ‘g’, (byte) ‘h’, (byte) ‘i’, (byte) ‘j’, (byte) ‘k’, (byte) ‘l’, (byte) ‘m’, (byte) ‘n’,
      (byte) ‘o’, (byte) ‘p’, (byte) ‘q’, (byte) ‘r’, (byte) ‘s’, (byte) ‘t’, (byte) ‘u’, (byte) ‘v’, (byte) ‘w’, (byte) ‘x’,
      (byte) ‘y’, (byte) ‘z’, (byte) ‘0’, (byte) ‘1’, (byte) ‘2’, (byte) ‘3’, (byte) ‘4’, (byte) ‘5’, (byte) ‘6’, (byte) ‘7’,
      (byte) ‘8’, (byte) ‘9’, (byte) ‘+’, (byte) ‘/’, (byte) ‘=’
      };

    /**

    • 解码表
      */
      private static final byte[] decodingTable;

    static {
    decodingTable = new byte[128];
    for (int i = 0; i < encodingTable.length; i++) {
    decodingTable[encodingTable[i]] = (byte) i;
    }
    }

    /**

    • @Comment BASE64编码
    • @Author Ron
    • @Date 2017年9月13日 上午9:56:02
    • @return
      /
      public static byte[] encode(byte[] data) {
      byte[] bytes;
      int modulus = data.length % 3;
      if (modulus == 0) {
      bytes = new byte[(4 * data.length) / 3];
      } else {
      bytes = new byte[4 * ((data.length / 3) + 1)];
      }
      int dataLength = (data.length - modulus);
      int a1;
      int a2;
      int a3;
      for (int i = 0, j = 0; i < dataLength; i += 3, j += 4) {
      a1 = data[i] & 0xff;
      a2 = data[i + 1] & 0xff;
      a3 = data[i + 2] & 0xff;
      bytes[j] = encodingTable[(a1 >>> 2) & 0x3f];
      bytes[j + 1] = encodingTable[((a1 << 4) | (a2 >>> 4)) & 0x3f];
      bytes[j + 2] = encodingTable[((a2 << 2) | (a3 >>> 6)) & 0x3f];
      bytes[j + 3] = encodingTable[a3 & 0x3f];
      }
      int b1;
      int b2;
      int b3;
      int d1;
      int d2;
      switch (modulus) {
      case 0: /
      nothing left to do */
      break;
      case 1:
      d1 = data[data.length - 1] & 0xff;
      b1 = (d1 >>> 2) & 0x3f;
      b2 = (d1 << 4) & 0x3f;
      bytes[bytes.length - 4] = encodingTable[b1];
      bytes[bytes.length - 3] = encodingTable[b2];
      bytes[bytes.length - 2] = (byte) ‘=’;
      bytes[bytes.length - 1] = (byte) ‘=’;
      break;
      case 2:
      d1 = data[data.length - 2] & 0xff;
      d2 = data[data.length - 1] & 0xff;
      b1 = (d1 >>> 2) & 0x3f;
      b2 = ((d1 << 4) | (d2 >>> 4)) & 0x3f;
      b3 = (d2 << 2) & 0x3f;
      bytes[bytes.length - 4] = encodingTable[b1];
      bytes[bytes.length - 3] = encodingTable[b2];
      bytes[bytes.length - 2] = encodingTable[b3];
      bytes[bytes.length - 1] = (byte) ‘=’;
      break;
      }
      return bytes;
      }

    public static byte[] decode(byte[] data) {
    byte[] bytes;
    byte b1;
    byte b2;
    byte b3;
    byte b4;
    data = discardNonBase64Bytes(data);
    if (data[data.length - 2] == ‘=’) {
    bytes = new byte[(((data.length / 4) - 1) * 3) + 1];
    } else if (data[data.length - 1] == ‘=’) {
    bytes = new byte[(((data.length / 4) - 1) * 3) + 2];
    } else {
    bytes = new byte[((data.length / 4) * 3)];
    }
    for (int i = 0, j = 0; i < (data.length - 4); i += 4, j += 3) {
    b1 = decodingTable[data[i]];
    b2 = decodingTable[data[i + 1]];
    b3 = decodingTable[data[i + 2]];
    b4 = decodingTable[data[i + 3]];
    bytes[j] = (byte) ((b1 << 2) | (b2 >> 4));
    bytes[j + 1] = (byte) ((b2 << 4) | (b3 >> 2));
    bytes[j + 2] = (byte) ((b3 << 6) | b4);
    }
    if (data[data.length - 2] == ‘=’) {
    b1 = decodingTable[data[data.length - 4]];
    b2 = decodingTable[data[data.length - 3]];
    bytes[bytes.length - 1] = (byte) ((b1 << 2) | (b2 >> 4));
    } else if (data[data.length - 1] == ‘=’) {
    b1 = decodingTable[data[data.length - 4]];
    b2 = decodingTable[data[data.length - 3]];
    b3 = decodingTable[data[data.length - 2]];
    bytes[bytes.length - 2] = (byte) ((b1 << 2) | (b2 >> 4));
    bytes[bytes.length - 1] = (byte) ((b2 << 4) | (b3 >> 2));
    } else {
    b1 = decodingTable[data[data.length - 4]];
    b2 = decodingTable[data[data.length - 3]];
    b3 = decodingTable[data[data.length - 2]];
    b4 = decodingTable[data[data.length - 1]];
    bytes[bytes.length - 3] = (byte) ((b1 << 2) | (b2 >> 4));
    bytes[bytes.length - 2] = (byte) ((b2 << 4) | (b3 >> 2));
    bytes[bytes.length - 1] = (byte) ((b3 << 6) | b4);
    }
    return bytes;
    }

    public static byte[] decode(String data) {
    byte[] bytes;
    byte b1;
    byte b2;
    byte b3;
    byte b4;
    data = discardNonBase64Chars(data);
    if (data.charAt(data.length() - 2) == ‘=’) {
    bytes = new byte[(((data.length() / 4) - 1) * 3) + 1];
    } else if (data.charAt(data.length() - 1) == ‘=’) {
    bytes = new byte[(((data.length() / 4) - 1) * 3) + 2];
    } else {
    bytes = new byte[((data.length() / 4) * 3)];
    }
    for (int i = 0, j = 0; i < (data.length() - 4); i += 4, j += 3) {
    b1 = decodingTable[data.charAt(i)];
    b2 = decodingTable[data.charAt(i + 1)];
    b3 = decodingTable[data.charAt(i + 2)];
    b4 = decodingTable[data.charAt(i + 3)];
    bytes[j] = (byte) ((b1 << 2) | (b2 >> 4));
    bytes[j + 1] = (byte) ((b2 << 4) | (b3 >> 2));
    bytes[j + 2] = (byte) ((b3 << 6) | b4);
    }
    if (data.charAt(data.length() - 2) == ‘=’) {
    b1 = decodingTable[data.charAt(data.length() - 4)];
    b2 = decodingTable[data.charAt(data.length() - 3)];
    bytes[bytes.length - 1] = (byte) ((b1 << 2) | (b2 >> 4));
    } else if (data.charAt(data.length() - 1) == ‘=’) {
    b1 = decodingTable[data.charAt(data.length() - 4)];
    b2 = decodingTable[data.charAt(data.length() - 3)];
    b3 = decodingTable[data.charAt(data.length() - 2)];
    bytes[bytes.length - 2] = (byte) ((b1 << 2) | (b2 >> 4));
    bytes[bytes.length - 1] = (byte) ((b2 << 4) | (b3 >> 2));
    } else {
    b1 = decodingTable[data.charAt(data.length() - 4)];
    b2 = decodingTable[data.charAt(data.length() - 3)];
    b3 = decodingTable[data.charAt(data.length() - 2)];
    b4 = decodingTable[data.charAt(data.length() - 1)];
    bytes[bytes.length - 3] = (byte) ((b1 << 2) | (b2 >> 4));
    bytes[bytes.length - 2] = (byte) ((b2 << 4) | (b3 >> 2));
    bytes[bytes.length - 1] = (byte) ((b3 << 6) | b4);
    }
    return bytes;
    }

    private static byte[] discardNonBase64Bytes(byte[] data) {
    byte[] temp = new byte[data.length];
    int bytesCopied = 0;
    for (int i = 0; i < data.length; i++) {
    if (isValidBase64Byte(data[i])) {
    temp[bytesCopied++] = data[i];
    }
    }
    byte[] newData = new byte[bytesCopied];
    System.arraycopy(temp, 0, newData, 0, bytesCopied);
    return newData;
    }

    private static String discardNonBase64Chars(String data) {
    StringBuffer sb = new StringBuffer();
    int length = data.length();
    for (int i = 0; i < length; i++) {
    if (isValidBase64Byte((byte) (data.charAt(i)))) {
    sb.append(data.charAt(i));
    }
    }
    return sb.toString();
    }

    private static boolean isValidBase64Byte(byte b) {
    if (b == ‘=’) {
    return true;
    } else if ((b < 0) || (b >= 128)) {
    return false;
    } else if (decodingTable[b] == -1) {
    return false;
    }
    return true;
    }

    public static void main(String[] args) {
    String codeStr = “RON”;
    byte[] codeByteArr = codeStr.getBytes();
    byte[] encodeByteArr = CustomBase64.encode(codeByteArr);
    String encodeStr = new String(encodeByteArr);
    byte[] decodeByteArr = CustomBase64.decode(encodeByteArr);
    String decodeStr = new String(decodeByteArr);
    System.out.println(“Source:” + codeStr);
    System.out.println(“自己实现编码:” + encodeStr);
    System.out.println(“自己实现解码:” + decodeStr);
    }
    }

以上仅仅只是根据算法的实现原理,我们对Base64编码解码做了一个简单的实现。但是在实际的应用过程中,我们并不用关心具体的实现方案,我们直接引入org.apache.commons.codec.binary.Base64包,调用已经实现的编码解码方法即可。

/**

  • @Comment Base64解码
  • @Author Ron
  • @Date 2017年9月12日 下午4:45:19
  • @return
    */
    public static byte[] decryptBASE64(String key) {
    return Base64.decodeBase64(key);
    }

/**

  • @Comment Base64编码
  • @Author Ron
  • @Date 2017年9月12日 下午4:47:55
  • @return
    */
    public static String encryptBASE64(byte[] bytes) {
    return Base64.encodeBase64String(bytes);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值