I. Principles of Base64 encoding

Base64 encoding is based on 64 characters a-z, A-Z, 0-9, +, /, because 2 ^ 6 is exactly 64, so 64 characters can be represented by 6 bits, eg:000000 corresponds to A, 000001 corresponds to B.

**BASE64 encoding principle: ** is a string length in groups of 3 characters (1Byte=8bit). For each group, first get the ASCII encoding of each character (‘a’=97=01100001), then convert the ASCII encoding to 8bit binary. You get a set of 3 * 8=24 bits. Then divide the 24 bits into 4 6-bit bytes and fill in two high-order zeros before each 6-bit byte to get 4 8-bit bytes. Then convert the 4 8-bit bytes to decimal. Compare the BASE64 encoding table (below). Get the corresponding encoded character.

Note: 1. The encoded characters are required to be 8bit, so they must be within the ASCII encoding range, \u0000-\u00ff, not Chinese. 2. If the length of the encoded character is not a multiple of 3, the output character is “=”.

Base64 encoding is essentially a scheme for converting binary data into text data. For non-binary data, first convert it to binary form, then calculate the decimal value of each consecutive 6 bits (2 ^ 6 =64), and then find the corresponding character among the 64 characters A–Z, A–Z, 0–9,+,/ based on that value, resulting in A text string. The ground rules are as follows:

  1. Standard Base64 has only 64 characters (upper and lower case, numbers, and +, /) and is used as a suffix equals sign;
  2. Base64 turns three bytes into four printable characters, so Base64 encoded strings must be divisible by four (not counting the suffix equals sign);
  3. The equal sign must be used as a suffix, and the number must be zero, one, or two. This is because if the original length is not divisible by 3, Base64 adds \0 to make up 3n bits. In order to restore correctly, add several equal signs if you add \0. Obviously the number of equals can only be 0, 1, or 2;
  4. Strictly speaking, Base64 is not an encryption, only an encoding conversion.

Second, Base64 decoding principle

The decoding principle is to convert 4 bytes into 3 bytes. Read in four six bits (using the or operation), shift them six bits to the left and eight bits to the right three times, and you are restored.

Base64 encoded string instance

1, the character length is divisible by 3: for example, “Tom” :

2, when the length of a string is not divisible by 3, such as “Lucy” :

Application of Base64 encoding

  1. To achieve simple data encryption, the user at a glance can not see the real data content, base64 algorithm is less complex, high efficiency is relatively high.

  2. The primary purpose of Base64 encoding is not security, but error-free transmission of content across gateways, which is the core purpose of Base64 encoding.

  3. In a computer, all data is stored in ASCII, and ASCII values between 128 and 255 are invisible characters. When exchanging data on the network, for example, from point A to point B, it often has to go through multiple routing devices, because different devices have some different ways of processing characters, so those invisible characters may be processed incorrectly, which is not good for transmission. So make the data Base64 encoded, make it all visible, and you’re less likely to make an error.

  4. Base64 encoding in URLS:

    Base64 encoding can be used to pass long identity information in an HTTP environment. For example, in Hibernate, the Java persistence system, Base64 is used to encode a long unique identifier (typically a 128-bit UUID) into a string that can be used as a parameter in HTTP forms and HTTP GET urls. In other applications, there is often a need to encode binary data into a form suitable for placing in urls (including hidden form fields). At this point, Base64 encoding is not only shorter, but also unreadable, in that the encoded data is not directly visible to the naked eye.

    However, standard Base64 is not suitable for direct transmission in urls, because the URL encoder converts the “/” and “+” characters in standard Base64 into forms such as “%XX,” and these “%” symbols need to be converted when stored in the database because “%” is used as a wildcard in ANSI SQL.

    (1) To solve this problem, an improved Base64 encoding for URLS can be adopted, which does not fill in the ‘=’ at the end, and changes the ‘+’ and ‘/’ in standard Base64 to ‘-‘ and ‘ ‘, respectively, so as to avoid the conversion needed for URL codec and database storage, and avoid the increase in the length of encoded information in the process. The format of object identifiers in databases and forms is unified. (2) There is an improved Base64 variant for regular expressions that replaces “+” and “/” with “!” And “-“, because “+”, “*”, and the “[” and”] “previously used in IRCu can all have special meanings in regular expressions. There are also variations that change the “+/” to “-” or “.” (used as an identifier Name in a programming language) or “.- “(for Nmtoken in XML) or even” : “(for Name in XML).

    Many download websites provide “thunderbolt download” links, its address is usually encrypted thunderbolt dedicated download address. Such as thunder://QUFodHRwOi8vd3d3LmJhaWR1LmNvbS9pbWcvc3NsbTFfbG9nby5naWZaWg== in fact thunderbolt special “address” is also use Base64 encryption, the encryption process is as follows:

    • 1. Add AA and ZZ before and after the address

    Such as www.baidu.com/img/sslm1_logo.gif into AAwww.baidu.com/img/sslm1_l…

    • Second, Base64 encoding the new string

    Such as AAwww.baidu.com/img/sslm1_logo.gifZZ QUFodHRwOi8vd3d3LmJhaWR1LmNvbS9pbWcvc3NsbTFfbG9nby5naWZaWg = = with Base64 encoding

    • 3. Precede the above string with “thunder://”

    thunder://QUFodHRwOi8vd3d3LmJhaWR1LmNvbS9pbWcvc3NsbTFfbG9nby5naWZaWg==

5. Base64 implementation

1. Base64 encoding the string

Public void base64Encode(View View) {String STR ="a";
        stringBase64 = Base64.encodeToString(str.getBytes(), Base64.NO_PADDING);
        
        test.setText(The Base64 encoding of A is:+stringBase64);
    }
Copy the code

2. Base64 decoding of the string

Public void base64Decode(View View) {byte[] decode = base64.decode (stringBase64, base64.default); String string = new String(decode); test.setText("Base64 decodes as:"+string);
    }
Copy the code

3. Encode the file in Base64

File file = new File("/storage/emulated/0/pimsecure_debug.txt");
FileInputStream inputFile = null;
try {
    inputFile = new FileInputStream(file);
    byte[] buffer = new byte[(int) file.length()];
    inputFile.read(buffer);
    inputFile.close();
    encodedString = Base64.encodeToString(buffer, Base64.DEFAULT);
    Log.e("Base64"."Base64---->" + encodedString);
} catch (Exception e) {
    e.printStackTrace();
}
Copy the code

4. Encode the file in Base64

File desFile = new File("/storage/emulated/0/pimsecure_debug_1.txt");
FileOutputStream  fos = null;
try {
    byte[] decodeBytes = Base64.decode(encodedString.getBytes(), Base64.DEFAULT);
    fos = new FileOutputStream(desFile);
    fos.write(decodeBytes);
    fos.close();
} catch (Exception e) {
    e.printStackTrace();
}
Copy the code

5. Base64.DEFAULT parameter description

Both encoding and decoding have a parameter, Flags. Android provides the following

  1. DEFAULT this parameter is the DEFAULT, use the DEFAULT method to encrypt

    Base64 encoding of “a” results in: YQ== and a newline appears after encoding

  2. The NO_PADDING parameter skips the “=” at the end of the encrypted string.

    The result of Base64 encoding “a” is: YQ

  3. NO_WRAP: omit all line breaks (CRLF is useless)

    Base64 encoding of “a” results in: YQ== and no newlines after encoding

  4. CRLF: CRLF: CRLF: CRLF: CRLF: CRLF: CRLF: CRLF: CRLF: CRLF: CRLF: CRLF: CRLF: CRLF

  5. The URL_SAFE parameter means that the characters that have special meaning for urls and filenames are not used as the encryption characters. In particular, the characters are – and _ instead of + and /

    Base64 encoding of “a” results in: YQ== and a newline appears after encoding

Note: BASE64Encoder encodes every 76 characters followed by a carriage return feed. Therefore, when a string of more than 76 characters is encoded, a RuntimeError is generated when the string is decoded. If you remove all the newlines that occur after encoding, as with base64. NO_WRAP, the string decodes properly.

    As long as you learn something you couldn’t before, and as long as the person you are today is better than the person you were yesterday, you’re on the way to advancement.