java implements Base64 encryption and decryption algorithm

  • 2020-05-09 18:32:44
  • OfStack

Base64 is one of the most common encodings on the network used to transmit 8Bit byte codes. You can check RFC2045 ~ RFC2049 for the detailed specification of MIME. The Base64 encoding can be used to pass long identification information in an HTTP environment. For example, in Java Persistence system Hibernate, Base64 is used to encode a long 1-only identifier (1 is generally 128-bit UUID) into a string that is used as a parameter in the HTTP form and HTTP GET URL. In other applications, it is also often necessary to encode the base 2 data in a form suitable for URL (including hidden form fields).

At this point, the Base64 code is not only short, but also unreadable, that is, the data encoded will not be directly seen by the human eye.
There are many encryption methods of java. Now I would like to share with you one encryption method of Base64


package com.crypt;
 
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
 *  BASE64 Encryption to decrypt 
 * @author YUANWEi
 */
public class BASE64 {
  /** 
   * BASE64 decryption  
   * 
   * @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?

Base64 encodings: because some systems can only use ASCII characters. Base64 is a method for converting data from non-ASCII characters into ASCII characters. It USES the characters and encodings used in the following table.

Moreover, base64 is particularly suitable for fast data transmission under http, mime protocols.

base64 is not an encryption and decryption algorithm in the security domain. Although sometimes it is common to see so-called base64 encryption and decryption. In fact, base64 can only be regarded as a coding algorithm, encoding the data content to be suitable for transmission. Although base64 encodes the original text into an invisible character format, but this way is very elementary, very simple.

The encoding method of Base64 requires converting every 3 bytes of 8Bit into 4 bytes of 6Bit, in which every 6 valid bit of the 4 bytes after conversion are valid data, and the two remaining bit are filled with 0 to become 1 byte. Therefore, the data redundancy caused by Base64 is not very serious. Base64 is a popular encoding method nowadays because it is fast and easy to encode.

Knowledge supplement:

The standard Base64 is not suitable for direct transmission in URL, because the URL encoder converts the "/" and "+" characters in the standard Base64 into the form of "%XX", and these "%" characters need to be converted when they are stored in the database, because the "%" is already used as a wildcard in ANSI SQL.

Can be used to solve this problem, a kind of improvement for URL Base64 coding, it is not the end of the filling '=', and the standard of Base64 "+" and "/" changed to "*" and "-" respectively, which removes the URL codec and database to store the work assigned to the transformation, to avoid the length in the process of encoding information increase, and the place such as 1 of the database system, the form object identifier of the format.

There is also an improved Base64 variant for regular expressions, which changes "+" and "/" to "!" And "-", because "+", "*" as well as the front in IRCu used "[" and"] "in the regular expression may have a special meaning.

There are also variants that change "+/" to "_-" or "._ "(used as an identifier name in programming languages) or".- "(used in XML for Nmtoken) or even" _: "(used in XML for Name).

Base64 requires that every 3 bytes of 8Bit be converted into 4 bytes of 6Bit (3*8 = 4*6 = 24), and then 6Bit be added with two more high-order zeros to form 4 bytes of 8Bit. In other words, the converted string will theoretically be 1/3 longer than the original.

The above is all about the encryption and decryption algorithms of Base64 and Base64. I hope it will be helpful for you to learn encryption and decryption.


Related articles: