Java Base64 Encryption/decryption

Source: Internet
Author: User
Tags base64

Base64 is commonly used to denote the contents of string encryption, using Java programming language to implement BASE64 encoding and decoding functions.

1. Do Base64 encoding and decoding in Java, will use the JDK Sun.misc suite under the Base64encoder and Base64decoder these two categories, the code is as follows

The 2.Apache Commons codec has encoding and decoding capabilities for BASE64, using the Base64 category under the Org.apache.commons.codec.binary Suite

http://commons.apache.org/proper/commons-codec/download_codec.cgi Download Commons-codec-1.11-bin Code

In the Java.util suite of 3.java 8, a new category of Base64 is added to handle BASE64 encoding and decoding

 Packagecom.test;Importorg.apache.commons.codec.binary.Base64;Importorg.junit.Test;ImportSun.misc.BASE64Decoder;ImportSun.misc.BASE64Encoder;/** * @authorCeshi * @Title: JUnitBase64 * @ProjectName Ceshi * @Description: TODO * @date 2018/6/1320:59*/ Public classJUnitBase64 {@Test Public voidTest ()throwsexception{//Base64encoderBase64encoder encoder =NewBase64encoder (); Base64decoder Decoder=NewBase64decoder (); String text= "Hello!" "; byte[] Textbyte = Text.getbytes ("UTF-8"); //CodingString Encodedtext =Encoder.encode (Textbyte); System.out.println (Encoder.encode (Text.getbytes ("UTF-8")); //decodingSystem.out.println (NewString (Decoder.decodebuffer (Encodedtext), "UTF-8")); //Apache Commons CodecBase64 base64 =NewBase64 (); Textbyte= Text.getbytes ("UTF-8"); //CodingEncodedtext =base64.encodetostring (Textbyte);        System.out.println (Encodedtext); //decodingSystem.out.println (NewString (Base64.decode (Encodedtext), "UTF-8")); //Java 8Base64.decoder Decoder =Base64.getdecoder (); Base64.encoder Encoder=Base64.getencoder (); //CodingEncodedtext =encoder.encodetostring (Textbyte);        System.out.println (Encodedtext); //decodingSystem.out.println (NewString (Decoder.decode (Encodedtext), "UTF-8")); }}

Java Base64 Encryption/decryption

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.