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