java custom implements base64 encoding conversion

  • 2020-06-15 08:26:04
  • OfStack

1.base64 code conversion

The base64 encoding is the conversion of characters into characters in the character set "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" according to the rules. Specific rules are as follows:

a. Divide every 3 bytes into 1 group for a total of 24bit. For every 6bit1 group, add 00 before each group to make 32bit. The three bytes are thus extended into four sections, encoded according to the above character set.

b. If the number of bytes is less than 3:

1) When the number of bytes is 2, the total is 16bit. For every 6bit1 group, the last group has only 4bit, not only 00 is added in the front, but also 00 is added in the back, according to the above character set encoding, and the last complement is =.

2) When the number of bytes is 1, the total is 8bit. For every 6bit1 group, the last group has only 2bit, not only 00 in the front, but also 0000 in the back, according to the above character set encoding, the last complement ==.

2. How do computers represent negative Numbers? (Take byte basic Type as an example)

In java, an byte is 8bit per byte, which can represent a range of 00000000 -- 11111111(0 ~ 255). In the computer, 0 -- 01111111 is 0 ~ 127, and 10 million -- 11111111 is -128 ~ -1. So, for example, -127+127, 01111111+10000001=100000000, the sum is equal to the modulus (256), that is, the complement of each other.

3.java bit operation.

In java (add, subtract, multiply, divide, right shift, left shift, unsigned right shift, bit and, bit or, bit or, bit or), byte,short,char will be converted to int before corresponding operation. For example:


public class Test {
 public static void main(String[] args) {
 byte s1 = (byte) 0xFF;// -1
 byte s2 = (byte) 0x80;// -128
  System.out.println((byte)(s1+s2));//s1+s2=-129, Coerced into byte , overflow, java Handle overflow ( +- ) 256*n,256 for byte Of type, the result is -129+256=127;
 byte s5 = -28;
 System.out.println(s5 << 2);//  The results for -112,  To convert int Type, right hand complement 0, High up 
 System.out.println(s5>>2);// The results for -7, To convert int Type, high order complement symbol bit, low order discard 
 System.out.println(s5>>>2);// The results for 1073741817, To convert int Type, high complement 0, Low abandon 
 System.out.println((s5&0xFC)>>2);
 }
}

4.java implements base64 encoding method


/**
 * @author zyw 2017 years 2 month 21 day 
 */
package test; 
import java.io.UnsupportedEncodingException;
/**
 * 1. complement  2. An operation  3.base64
 *
 * @description  learning base64 encryption   The first 1 Step, each 3 Two bytes as 1 Group, 1 Total is 24 a 2 Into the system. 
 *   The first 2 Step, will it 24 a 2 The base bit is divided into 4 Groups, each group has 6 a 2 Into the system.   The first 3 Step: Add two in front of each group 00 , expanded into 32 a 2 Base bit, that is 4 Bytes. 
 *
 */
public class Base64 {
 static private final int SIXTEENBIT = 16;
 static private final int EIGHTBIT = 8;
 static private final char PAD = '=';
 public static void main(String[] args) throws UnsupportedEncodingException {
 System.out.println(Base64.toBase64(" China fggfgfgf234234%#$%^#$$", "UTF-8"));//5Lit5Zu9ZmdnZmdmZ2YyMzQyMzQlIyQlXiMkJA==
 }
 /**
 * base64 encryption 
 * @param str
 * @param charsetName
 * @return
 * @throws UnsupportedEncodingException
 */
 public static String toBase64(String str, String charsetName) throws UnsupportedEncodingException {
 if (str.length() < 0)
  return "";
 byte[] text = str.getBytes(charsetName);
 char[] base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();//  encryption 
 int lengthDataBits = text.length * 8;
 int fewerThan24bits = lengthDataBits % 24;//  Whether the length of the encrypted string is exceeded 24
 int numberTriplets = lengthDataBits / 24;
 int number = fewerThan24bits != 0 ? numberTriplets + 1 : numberTriplets;//  Calculates the total number of characters after the string is encrypted 
 char[] toBase64Text = new char[number * 4];//  To save the results 
 byte s1, s2, s3;
 int index = 0, order = 0;
 for (int i = 0; i < numberTriplets; i++) {
  s1 = text[index++];
  s2 = text[index++];
  s3 = text[index++];
  toBase64Text[order++] = base[(s1 & 0xFC) >> 2];//  The first 1 a 6 position 
  toBase64Text[order++] = base[((s1 & 0x03) << 4) + ((s2 & 0xF0) >> 4)];//  The first 2 a 6 position 
  toBase64Text[order++] = base[((s2 & 0x0F) << 2) + ((s3 & 0xC0) >> 6)];//  The first 3 a 6 position 
  toBase64Text[order++] = base[s3 & 0x3f];//  The first 4 a 6 position 
 }
 /**
  * 1 Case of 2 bytes: will this 1 Bytes of 8 a 2 The base bit is last 1 Group in addition to the front 2 a 0 In addition, add later 4 a 0 . So get 1 a 2 bit Base64 Coding, 
  *  Add two more at the end "=" Number. 
  */
 if (fewerThan24bits == EIGHTBIT) {
  byte last = text[index++];
  toBase64Text[order++] = base[(last & 0xFC) >> 2];
  toBase64Text[order++] = base[((last & 0x03) << 4)];
  toBase64Text[order++] = PAD;
  toBase64Text[order++] = PAD;
 }
 /**
  * 2 Case of 2 bytes: will this 2 Bytes of 1 A total of 16 a 2 Base a , into 3 Group, and finally 1 Add two to the front of the group 0 Besides, two should be added after that 0 . 
  *  So get 1 a 3 bit Base64 Code, and then fill in the end 1 a "=" Number. 
  */
 if (fewerThan24bits == SIXTEENBIT) {
  s1 = text[index++];
  s2 = text[index++];
  toBase64Text[order++] = base[(s1 & 0xFC) >> 2];
  toBase64Text[order++] = base[(s1 & 0x03) << 4 + ((s2 & 0xF0) >> 4)];
  toBase64Text[order++] = base[(s2 & 0x0f) << 2];
  toBase64Text[order++] = PAD;
 }
 return new String(toBase64Text);
 }
}

Related articles: