Java implementation Base64 encryption and decryption algorithm _java

Source: Internet
Author: User
Tags base64

Base64 is one of the most common coding methods for transmitting 8Bit byte code on the network, and you can view the rfc2045~rfc2049, which has a MIME detail specification. The BASE64 encoding can be used to pass longer identity information in an HTTP environment. For example, in the Java Persistence System hibernate, Base64 is used to encode a long unique identifier (typically a 128-bit uuid) as a string, as a parameter in an HTTP form and an HTTP GET URL. In other applications, it is often necessary to encode binary data into a form appropriate to the URL, including hidden form fields.

At this time, the use of BASE64 encoding is not only relatively short, but also has the readability, that is, the encoded data will not be directly visible to the naked eye.
Java encryption Way There are many, now to share a Base64 encryption of the way

Package com.crypt;
 
Import Sun.misc.BASE64Decoder;
Import Sun.misc.BASE64Encoder;
/**
 *  BASE64 Encryption Decryption
 * @author Yuanwei/public
class BASE64 {
  /** 
   * BASE64 decryption 
   * 
   * @param key 
   * @return 
   * @throws Exception 
  /public static byte[] decryptBASE64 (String key) throws E Xception {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 encoding: Because only ASCII characters are used in some systems. Base64 is a way to convert data from non-ASCII characters to ASCII characters. It uses the characters and encodings used in the following table.

And Base64 is particularly suitable for fast data transmission under the Http,mime protocol.

Base64 is actually not a security domain encryption and decryption algorithm. Although sometimes the so-called Base64 encryption decryption is often seen. In fact, Base64 can only be regarded as a coding algorithm, the data content encoding to fit the transmission. Although the Base64 code has become a character format that cannot be seen, this approach is very elementary and simple.

The Base64 encoding method requires that every three 8Bit bytes be converted to four 6Bit bytes, where each 6 valid bit in the converted four bytes is valid data, and the free two bits are filled with 0 to become a byte. Therefore, the data redundancy caused by Base64 is not very serious, and Base64 is a popular encoding method nowadays because it is fast and simple to weave.

Knowledge Supplement:

The standard Base64 is not suitable for transmission directly to the URL. Because the URL encoder changes the "/" and "+" characters in the standard Base64 to form "%xx", these "%" numbers need to be converted when they are stored in the database, because the "%" number is used as a wildcard in ANSI SQL.

To solve this problem, you can use an improved BASE64 encoding for URLs, it does not populate the ' = ' number at the end, and changes the "+" and "/" in the standard Base64 to "*" and "-", thus eliminating the conversion to be made when the URL is encoded and stored in the database. It avoids the increase of the length of encoded information in this process, and unifies the format of object identifiers such as databases, forms, and so on.

There is also an improved BASE64 variant for regular expressions, which changes "+" and "/" to "!" and "-", because "+", "*", and "[" and "]" previously used in IRCU may have special meanings in regular expressions.

In addition, there are variants that change "+/" to "_-" or ". _" (used as the identifier name in the programming language) or ".-" (for NmToken in XML) or even "_:" (for name in XML).

BASE64 requires converting every three 8Bit bytes to four 6Bit bytes (3*8 = 4*6 = 24), and then adding 6Bit two-bit high 0 to form four 8Bit bytes, that is, the converted string is theoretically going to be 1/3 longer than the original.

The above is about BASE64, Base64 encryption and decryption algorithm of the entire content, I hope that the encryption and decryption of the learning to help.

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.