Java one-Way encryption algorithm Summary (1)--base64 algorithm

Source: Internet
Author: User
Tags base64


Java one-Way encryption algorithm Summary (1)--base64 algorithm


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 pm.
 9  */
10 public class Base64Test {
11
12 public static void main (String [] args) throws IOException {
13
14 String msg = "wang";
15 byte [] bys = msg.getBytes ("utf-8");
16
17 / **
18 * Use BASE64 encryption (encoding)
19 * /
20 // The first way
21 BASE64Encoder base64Encoder = new BASE64Encoder ();
22 String s = base64Encoder.encodeBuffer (bys);
23 System.out.println (s);
24 System.out.println ("---------------------");
25 // The second way
26 String s1 = Base64.encode (bys);
27 System.out.println (s1);
28 System.out.println ("---------------------");
29 / **
30 * Decrypt (decode) using BASE64
31 * /
32 // The first way
33 BASE64Decoder base64Decoder = new BASE64Decoder ();
34 byte [] bys2 = base64Decoder.decodeBuffer (s);
35 System.out.println (new String (bys2));
36 System.out.println ("---------------------");
37 // Second way
38 byte [] bys3 = Base64.decode (s);
39 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.



Reprint http://www.cnblogs.com/fingerboy/p/6027519.html



Java one-Way encryption algorithm Summary (1)--base64 algorithm


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.