Http://www.javatips.net/blog/2011/08/how-to-encode-and-decode-in-base64-using-java
http://commons.apache.org/proper/commons-codec/
Official Download link
Encode and Decode in Base64 using Java explains on different techniques for Encode Base64 using Java/decode Base64 using Java.
What is BASE64 encoding?
How to Encode a String to Base64?
BASE64 encoding was commonly used when there was a need to encode/decode binary data stored and transferred over network. It is used to encode and decode images, photos, audio etc as attachments in order to send through emails.
For Example Evolution and Thunderbird e-mail clients use Base64 to obfuscate email passwords.
Note
If you is working on Java version 8, you can use the Java 8 new API. Please check Java 8 encode/decode base64 Example
1. Using Sun.misc.base64encoder/sun.misc.base64decoder Example
The best-of-encode/decode in Base64 without using a third party libraries, you can use using Sun.misc.BASE64Encode R/sun.misc.base64decoder. sun.* packages was not "officially supported" by Sun as it was proprietary classes, so there was chance to remove from JDK I N Future versions.
ImportJava.io.IOException;
ImportSun.misc.BASE64Decoder;
ImportSun.misc.BASE64Encoder;
Java Base64 Encoder/java Base64 Decoder Example
public classBase64test{
public staticvoidMain(String[]Args) {
Base64decoder decoder =NewBase64decoder();
Base64encoder encoder =NewBase64encoder();
Try{
String encodedbytes = Encoder.encodebuffer("Javatips.net". getBytes());
System.out.println("Encodedbytes"+ encodedbytes);
byte[] decodedbytes = Decoder.decodebuffer (encodedbytes system.out.println ( "decodedbytes" + new string ( Decodedbytes catch ( IOException e) {
e.printstacktrace< Span class= "Java8" > ()
}
} Outputencodedbytes Smf2yvrpchmubmv0
Decodedbytes Javatips.net2. Using javax.mail.internet.MimeUtility Example
Javax.mail.internet.MimeUtility is a part of JavaMail package and you can download the jar file from the following link Ja Vamail and should is in Class path
ImportJava.io.ByteArrayInputStream;
ImportJava.io.ByteArrayOutputStream;
ImportJava.io.IOException;
ImportJava.io.InputStream;
ImportJava.io.OutputStream;
ImportJavax.mail.MessagingException;
Importjavax.mail.internet.MimeUtility;
How to Encode A String to Base64 Using Java
public classBase64test{
public staticvoidMain(String[]Args)ThrowsMessagingexception,ioexception{
String test ="Javatips.net";
Bytearrayoutputstream BAOs =NewBytearrayoutputstream();
OutputStream Base64outputstream = Mimeutility.encode(BAOs,"Base64");
Base64outputstream.write(Test.getbytes());
Base64outputstream.close();
System.out.println("Encoded String"+ baos.tostring());
Bytearrayinputstream Bais =NewBytearrayinputstream(Baos.tobytearray());
InputStream Base64inputstream = Mimeutility.decode(Bais,"Base64");
Byte[]TMP =NewByte[Baos.tobytearray(). length];
Intn = base64inputstream.read(Tmp byte[] res = New byte[n system.arraycopy (tmp, 00 system.out.println ( "decoded String" + new string ( res)) }
} outputencoded String Smf2yvrpchmubmv0
Decoded String javatips.net 3. Using Apache Commons Codec Example
You can also with Apache Commons Codec for Encode/decode in Base64. You can download the jar from the following link Apache Commons Codec and should is in Class path
ImportOrg.apache.commons.codec.binary.Base64;
Apache Commons Codec Base64 Example
public classBase64test{
public staticvoidMain(String[]Args) {
Byte[]Encodedbytes = Base64.encodebase64("Javatips.net". getBytes());
System.out.println("encodedbytes" + new String(encodedbytes));
byte[] decodedbytes = base64.decodebase64(encodedbytes);
System.out.println("decodedbytes" + new String(decodedbytes));
}
} Outputencodedbytes Smf2yvrpchmubmv0
Decodedbytes Javatips.net4. Using MiGBase64 Example
If you is preferring performance based solution, then migbase64 outperform other libraries, you can download Migbase64 ja R from the following link MiGBase64 and should is in ClassPath
MiGBase64 Example
public classBase64test{
public staticvoidMain(String[]Args) {
String encodetostring = base64.encodetostring("Javatips.net". getBytes()true system.out.println ( "encodetostring" + encodetostring) byte[] decodedbytes = Base64.decode (encodetostring.getbytes ()) system.out.println ( "decodedtostring" + new string ( Decodedbytes } Outputencodetostring Smf2yvrpchmubmv0
Decodedtostring javatips.net
Java Base64 [Encode and Decode in Base64 Using Java]