Base64 is one of the most common encoding methods on the network for transmitting 8Bit byte code. You can check RFC2045~RFC2049, which contains detailed MIME specifications. Base64 encoding can be used to pass longer identification information in HTTP environments. For example, in the Java Persistence system Hibernate, Base64 is used to encode a long unique identifier (usually a 128-bit UUID) into a string, which is used as a parameter in HTTP forms and HTTP GET URLs. In other applications, it is often necessary to encode binary data into a form suitable for placement in URLs (including hidden form fields).
At this time, Base64 encoding is not only short, but also unreadable, that is, the encoded data will not be directly seen by the naked eye.
There are many ways to encrypt Java. Now I will share with you a Base64 encryption method.
package com.crypt; import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;/** * BASE64 encryption and decryption* @author YUANWEi */public class BASE64 { /** * BASE64 decrypt* * @param key * @return * @throws Exception */ public static byte[] decryptBASE64(String key) throws Exception { return (new BASE64Decoder()).decodeBuffer(key); } /** * BASE64 encryption* * @param key * @return * @throws Exception */ public static String encryptBASE64(byte[] key) throws Exception { return (new BASE64Encoder()).encodeBuffer(key); } }Why use Base64 encryption?
The role of Base64 encoding: Since some systems can only use ASCII characters. Base64 is a method used to convert data of non-ASCII characters into ASCII characters. It uses the characters and encodings used in the table below.
Moreover, base64 is particularly suitable for quickly transmitting data under http and mime protocols.
Base64 is actually not an encryption and decryption algorithm in the security field. Although sometimes you often see the so-called base64 encryption and decryption. In fact, base64 can only be regarded as an encoding algorithm, encodes the data content to be suitable for transmission. Although the original text becomes a character format that cannot be seen after base64 encoding, this method is very basic and simple.
The Base64 encoding method requires converting every three 8Bit bytes into four 6Bit bytes. Among them, every 6 valid bits of the four bytes after conversion are valid data, and the two spare bits are supplemented with 0 to become one byte. Therefore, the data redundancy caused by Base64 is not very serious. Base64 is a popular encoding method today because it is fast and simple to code.
Knowledge Supplement:
Standard Base64 is not suitable for direct transmission in URLs, because the URL encoder will change the "/" and "+" characters in standard Base64 into a form like "%XX", and these "%" numbers need to be converted when stored in the database, because the "%" numbers have been used as wildcards in ANSI SQL.
To solve this problem, an improved Base64 encoding for URLs can be used. It does not fill in the '=' number at the end, and changes the "+" and "/" in standard Base64 to "*" and "-" respectively, which eliminates the conversion required during URL encoding and decoding and database storage, avoids the increase in the length of the encoding information in this process, and unifies the format of object identifiers in databases, forms, etc.
There is another improved Base64 variant for regular expressions, which changes "+" and "/" to "!" and "-", because "+", "*" and "[" and "]" used in IRCu may have special meanings in regular expressions.
There are also some variants that change "+/" to "_-" or "._" (used as an identifier name in the programming language) or ".-" (used for Nmtoken in XML) or even "_:" (used for Name in XML).
Base64 requires converting every three 8Bit bytes into four 6Bit bytes (3*8 = 4*6 = 24), and then adding two high 0s to 6Bit to form four 8Bit bytes. That is to say, the converted string will theoretically be 1/3 longer than the original one.
The above is all about Base64 and Base64 encryption and decryption algorithms. I hope it will be helpful to everyone's learning encryption and decryption.