Common encryption and decryption algorithms-base64

Source: Internet
Author: User
Tags base64 md5 encryption asymmetric encryption

I. Overview of BASE64 encryption and decryption

Base64 is one of the most common encoding methods for transmitting 8Bit bytes of code on the network, and 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 128-bit uuid) as a string that is used as a parameter in an HTTP form and an HTTP GET URL. In other applications, it is often necessary to encode binary data as appropriate in the form of URLs (including hidden form fields). At this time, the use of BASE64 encoding is not readable, that is, the encoded data will not be directly visible to the naked eye.

However, the standard Base64 is not suitable for direct transmission in the URL, because the URL encoder will be in the standard Base64 "/" and "+" characters into the form of "%XX", and these "%" number in the database will need to be converted, because ANSI SQL has the "%" Used as a wildcard character.

To solve this problem, you can use an improved BASE64 encoding for URLs, which not only removes the padding ' = ' at the end, but also changes the "+" and "/" in standard Base64 to "-" and "_" respectively, thus eliminating the need for conversion in URL codec and database storage. Avoids the increase in the length of encoded information in this process, and unifies the format of object identifiers in databases, forms, and so on.BASE64 is a symmetric encryption algorithm (PS: Symmetric encryption is encrypted with the same password and decryption password is the same, asymmetric encryption and decryption with the key is not the same)Reference connection: BASE64 encryption and decryptionSecond, Java implementation MD5 encryption and decryption1, MAVEN introduced Apache Jar (not maven project, go online next jar manually introduced)

            <dependency>                <groupId>org.apache.commons</groupId>                <artifactid>commons-lang3 </artifactId>                <version>3.3.2</version>            </dependency>

2. MD5 Use code

 Packagecom.jd.test;Importorg.apache.commons.codec.binary.Base64;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;Importjava.io.UnsupportedEncodingException;/*** * BASE64 General class * *@authorHao make the world *@since2017.04.15 *@version1.0.0_1 **/ Public classBase64util {Private Static FinalLogger Logger = Loggerfactory.getlogger (base64util.class); /*** Base64 decoding of a given string*/     Public Staticstring Decodedata (String inputdata) {Try {            if(NULL==inputdata) {                return NULL; }            return NewString (Base64.decodebase64 (Inputdata.getbytes ("Utf-8")), "Utf-8"); } Catch(unsupportedencodingexception e) {logger.error (Inputdata, E); }        return NULL; }    /*** Base64 encryption operation for a given string*/     Public Staticstring Encodedata (String inputdata) {Try {            if(NULL==inputdata) {                return NULL; }            return NewString (Base64.encodebase64 (Inputdata.getbytes ("Utf-8")), "Utf-8"); } Catch(unsupportedencodingexception e) {logger.error (Inputdata, E); }        return NULL; }    /*** Test Portal * *@paramargs*/     Public  Static voidMain (String args[]) {string Encodestr=base64util.encodedata ("The Dragon does not sing, the tiger does not roar"); System.out.println ("Encodestr=" +encodestr); String Decodestr=Base64util.decodedata (ENCODESTR); System.out.println ("Decodestr=" +decodestr); }}

Common encryption and decryption algorithms-base64

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.