Principle:
Base64 is one of the most common encoding methods for transmitting 8Bit bytecode on the network. Base64 is a way to represent binary data based on 64 printable characters.
About the coding rule:
Change 3 characters into 4 characters.
(2) Add a newline character for every 76 characters. (In the example, to deal with this rule)
(3) The final terminator should also be dealt with. (The total number of bytes% 3 is 1: fill 2===. The total number of bytes% 3 is 2 to complement 1=)
package test; import java.util.Base64; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; @SuppressWarnings("restriction") public class Base64Util{ //The subtitle char array and index are worthy of correspondence. static private char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" .toCharArray(); // static private byte[] codes = new byte[256]; //Complete the corresponding relationship between index and b64 value under array static{ //Array assignment initialization for (int i = 0; i < 256; i++) codes[i] = -1; //A-Z assignment for (int i = 'A'; i <= 'Z'; i++) codes[i] = (byte) (i - 'A'); //a-z assignment for (int i = 'a'; i <= 'z'; i++) codes[i] = (byte) (26 + i - 'a'); //Number 0-9 assignment for (int i = '0'; i <= '9'; i++) codes[i] = (byte) (52 + i - '0'); codes['+'] = 62; codes['/'] = 63; } /** * BASE64 Decrypt * sun Company's original base64 decoding * @param key * @return * @throws Exception */ public static byte[] decryptBASE64(String base64Str) throws Exception{ return (new BASE64Decoder()).decodeBuffer(base64Str); } /** * BASE64 encryption * sun Company's original base64 coding * @param key * @return * @throws Exception */ public static String encryptBASE64(byte[] key){ return (new BASE64Encoder()).encodeBuffer(key); } /** * Custom coding */ static public String encode(byte[] data){ //Prepare to extend the char array (data.length + 2) / 3 to ensure that the multiple of 3 is rounded up //Expanded bytes are 4/3 of the original 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 new String(out); } /** * Custom Decoding */ static public byte[] decode(char[] data){ //Calculate byte length after conversion int len = ((data.length + 3) / 4) * 3; //Fill in an equal sign at the end of the process if (data.length > 0 && data[data.length - 1] == '=') --len; //Complete two equal signs at the end of the process if (data.length > 1 && data[data.length - 2] == '=') --len; //Byte length after final decryption 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; } /** * BASE64 encryption * util base64 Decoding under Packet * @param key * @return * @throws Exception */ public static String decrypt64(String base64Str) throws Exception{ //String encodeToString = Base64.getEncoder().encodeToString(base64Str.getBytes()); //return (new BASE64Decoder()).decodeBuffer(base64Str); byte[] decode = Base64.getDecoder().decode(base64Str); return new String(decode,"utf-8"); } /** * BASE64 encryption * util base64 encoding under package * @param key * @return * @throws Exception */ public static String encrypt64(byte[] key){ return Base64.getEncoder().encodeToString(key); } }