Starting with the simplest of the most common cryptographic algorithms and demos used in Java, start with this article.
Easy to understand
Base64 is not strictly a cryptographic algorithm, but a method of encoding/decoding implementation.
We all know that the data in the computer network is the use of byte-stream transmission, all the information must eventually be converted to 0101 binary, which in itself involves coding, decoding the application.
Base64, as the name implies, is the use of 64 basic characters to encode arbitrary data is a way of implementation, since there is Base64, is not there base32,base16? The answer is yes, there is.
Let's take a look at the Base64 code table:
Visible from the diagram, Base64 uses 26-letter case, which is 52 characters, plus 10 numbers and two special "+", "/" composition, a total of 64 characters, where the last two characters are not necessarily always two.
Java Code Implementation
First look at the code, and then elaborate on the principle and application of BASE64 implementation:
1 package com.wang.encryption; 2 Import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; 3 Import Sun.misc.BASE64Decoder; 4 Import Sun.misc.BASE64Encoder; 5 Import java.io.IOException; 6/** 7 * @author Yogo.wang 8 * @date 2016/11/03-3:58. 9 */10 public class Base64test {All public static void main (string[] args) throws IOException {+ String Msg= "Wang"; byte[] Bys=msg.getbytes ("Utf-8"); 16 17/**18 * Using BASE64 encryption (encoded) 19 */20 The first way is Base64encoder base64encoder=new base64encoder (); String s = Base64encoder.encodebuffer (by s); System.out.println (s), System.out.println ("---------------------"); 25//The second way of St Ring S1 = Base64.encode (bys); System.out.println (S1); System.out.println ("---------------------"); 29 /**30 * Decryption (decoding) using BASE64 31 */32//First way Base64decoder base64decoder=new BASE64DE Coder (); 34 byte[] Bys2 = Base64decoder.decodebuffer (s); Bys2 System.out.println (New String (System.out.print)); ln ("---------------------"); 37//Second way byte[] Bys3 = Base64.decode (s), System.out.println (new String (BYS3)); 40 41 42}43}
The output results are as follows:
d2fuzw==
---------------------
d2fuzw==
---------------------
Wang
---------------------
Wang
Principles and Applications
According to the code, you can see that the character "Wang" is transformed into a string "d2fuzw==" by Base64 's code, so what is the internal implementation?
BASE64 uses 64 of the characters, then only 6 bits can be represented, but a Base64 character is 8 bits, that is, 8Bit, that is Base64 on the basis of 6Bit on the left add two 0, all its left two bits will always be 0.
We know that a character is accounted for 8 bits, then how to use 6Bit to represent the number of 8Bit, this is good, because 8 and 6 of the least common multiple is 24, then, I use 4 Base64 characters to represent 3 regular character on it.
Knowing this, we can see how the string "Wang" was encoded as "d2fuzw==".
The first step: convert regular characters to ASCII.
Step two: Convert ASCII code to 8-bit 2 binary
Step three: Each 6-bit 2 binary number is divided into one set
Fourth step: Convert 2 binary to 10 binary
Fifth step: Find the corresponding character according to the BASE64 encoding table
Corresponds to the following diagram:
Because every three regular characters, corresponding to four Base64 characters, if not enough, will be in the back of 0, where the "Wang" is 4 characters, not 3 of the integer times, so need to fill 0, then why the result of the code is "d2fuzw==", rather than "D2FUZWAA" it?
The reason is actually very simple, because the last two AA is no practical significance, does not carry the effective information, in order to decode conveniently, BASE64 code chooses uses "=" to replace the last "A", so is we see "d2fuzw==".
Through the above process we can find that, in fact, Base64 is a way of encoding rather than encryption, because its encoding and decoding process is completely reversible, and do not need additional information, you just have a Base64 of the Code table can be, so, do not use him for data encryption.
Application of Base64
Having said so much, it seems that there is no reason why the code must exist .....
We know that any data in the computer is stored in ASCII code, and the value between the 128~255 of the ASCII code is invisible. When exchanging data on the network, for example, from A to B, often through a number of routing devices, because different devices on the processing of characters have some differences, so that those invisible characters can be processed errors, which is not conducive to transmission. So first the data is a Base64 code, all become visible characters, so the likelihood of error is greatly reduced (this paragraph from the knowledge).
Not only that, for example, we can use HTML to embed BASE64 encoded images, so as to avoid unnecessary external resource loading, but still want to possibilities, this scenario is generally suitable for small size pictures, if it is high-definition images, in this way will lead to Base64 encoded string is very large, Instead, it affects the loading speed.
Java Encryption algorithm