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

1 2 Next





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