Encode And Decode In Base64 Using Java

Encode And Decode In Base64 Using Java explains about different techniques for Encode Base64 using java / Decode Base64 using java.

What is Base64 encoding?

How to Encode a String to Base64?

Base64 encoding is commonly used when there is 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 email clients use Base64 to obfuscate email passwords.

Note

If you are working on Java version 8, You can use Java 8 new api. Please check Java 8 Encode/Decode base64 Example

1. Using sun.misc.BASE64Encoder / sun.misc.BASE64Decoder Example

The best way to encode / decode in Base64 without using any third party libraries, you can use Using sun.misc.BASE64Encoder / sun.misc.BASE64Decoder. sun.* packages are not "officially supported" by Sun as it is proprietary classes, so there is chance to remove from jdk in future versions.

import java.io.IOException;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

// Java Base64 Encoder / Java Base64 Decoder Example

public class Base64Test {

 
public static void main(String[] args) {
   
BASE64Decoder decoder = new BASE64Decoder();
    BASE64Encoder encoder =
new BASE64Encoder();
   
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();
   
}
  }
}
Output
encodedBytes SmF2YVRpcHMubmV0

decodedBytes JavaTips.net

2. 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 JavaMail and should be in Class path

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.mail.MessagingException;
import javax.mail.internet.MimeUtility;

// How To Encode A String To Base64 Using Java

public class Base64Test {

 
public static void main(String[] args) throws MessagingException,IOException {
   
String test = "JavaTips.net";

    ByteArrayOutputStream baos =
new ByteArrayOutputStream();
    OutputStream base64OutputStream = MimeUtility.encode
(baos, "base64");
    base64OutputStream.write
(test.getBytes());
    base64OutputStream.close
();
    System.out.println
("encoded String " + baos.toString());

    ByteArrayInputStream bais =
new ByteArrayInputStream(baos.toByteArray());
    InputStream base64InputStream = MimeUtility.decode
(bais, "base64");
   
byte[] tmp = new byte[baos.toByteArray().length];
   
int n = base64InputStream.read(tmp);
   
byte[] res = new byte[n];
    System.arraycopy
(tmp, 0, res, 0, n);
    System.out.println
("decoded String " + new String(res));
 
}
}
Output
encoded String SmF2YVRpcHMubmV0

decoded String JavaTips.net

3. Using Apache Commons Codec Example

You can also use Apache Commons Codec for encode / decode in Base64. You can download the jar from the following link Apache Commons Codec and should be in Class path

import org.apache.commons.codec.binary.Base64;

// Apache Commons Codec Base64 Example

public class Base64Test {

 
public static void main(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));
 
}
}
Output
encodedBytes SmF2YVRpcHMubmV0

decodedBytes JavaTips.net

4. Using MiGBase64 Example

If You are preferring performance based solution, then migbase64 outperform other libraries, You can download migbase64 jar from the following link MiGBase64 and should be in ClassPath


// MiGBase64 Example

public class Base64Test {

 
public static void main(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));
 
}
}
Output
encodeToString SmF2YVRpcHMubmV0

decodedToString JavaTips.net








Your email address will not be published. Required fields are marked *