Java Encryption Basics (i)--BASE64 encoding

Source: Internet
Author: User
Tags base64 asymmetric encryption

The first time after working with the Java encryption mechanism, the requirement is to use RSA digital signature. At that time, after seeing a face of a confused force, looked up all kinds of information.

For the learning process I took a lot of detours, mainly because I do not know what to look at first. Let's talk about my study experience.

First of all, to understand the BASE64 algorithm, because Java encryption is basically used in the BASE64, then symmetric and asymmetric encryption (the next section is highlighted, here is not much to describe); and finally the digital signature (which is described in the third section)

We all know that ASCII characters have a total of 256, and the computer records data in the way only 0, 1, so you can only use 8bit to represent a ascll character. BASE64 as the name implies a total of 64 characters, so only need 6bit can be expressed. So how does BASE64 represent all the characters of ASCLL? The least common multiple of 8 and 6 are 24, which means that 3 ASCII characters can be represented by 4 BASE64 characters, guaranteeing the same number of significant digits. That is, every four bit BASE64 as a group, that is, 24bit bit a group, if at the end of the discovery of less than 24bit what to do? Here's the explanation:

The graph represents the parsing encoding of BASE64 at exactly 24bit.

The figure of a BASE64 encoding is expressed as QQ, because only 8bit, the corresponding BASE64 only left 2bit, so to fill 4 0, so it became QQ. The principle is that the smallest unit of the Base64 character is a group of four characters, so for less than four characters is used = padding, so BASE64 can only appear at the end of one or two =, for parsing is not resolved = on.

Finally do a Java in the code and parsing of BASE64

Import java.io.IOException;

Import Org.junit.Test;

Import Sun.misc.BASE64Decoder;
Import Sun.misc.BASE64Encoder;

public class Demo1 {
@Test
public void Demo1 () throws ioexception{
String str = "Hello world!"; agvsbg8gd29ybgqhmq==
Base64encoder encode = new Base64encoder (); Agvsbg8gd29ybgqh
String encodestr = Encode.encode (Str.getbytes ());
System.out.println (ENCODESTR);
Base64decoder decoder = new Base64decoder ();
String decoderstr = new String (Decoder.decodebuffer (ENCODESTR));
System.out.println (DECODERSTR);
}
}

Java Encryption Basics (i)--BASE64 encoding

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.