189

I want to encode a string into base64 and transfer it through a socket and decode it back.

But after decoding it gives different answer.

Following is my code and result is "77+9x6s="

import javax.xml.bind.DatatypeConverter; public class f{ public static void main(String a[]){ String str = new String(DatatypeConverter.parseBase64Binary("user:123")); String res = DatatypeConverter.printBase64Binary(str.getBytes()); System.out.println(res); } } 

Any idea about how to implement this?

3
  • 2
    see stackoverflow.com/questions/13109588/base64-encoding-in-java Commented Nov 2, 2013 at 16:06
  • 5
    DataTypeConverter works just fine, the original poster has just mixed the order. It should look like this: String str = DatatypeConverter.printBase64Binary("user:123".getBytes()); String res = new String(DatatypeConverter.parseBase64Binary(str)); Commented Sep 12, 2016 at 14:42
  • 1
    parseBase64Binary() is used for decoding and printBase64Binary() is used for encoding. Commented Feb 18, 2021 at 6:33

6 Answers 6

275

You can use following approach:

import org.apache.commons.codec.binary.Base64; // Encode data on your side using BASE64 byte[] bytesEncoded = Base64.encodeBase64(str.getBytes()); System.out.println("encoded value is " + new String(bytesEncoded)); // Decode data on other side, by processing encoded data byte[] valueDecoded = Base64.decodeBase64(bytesEncoded); System.out.println("Decoded value is " + new String(valueDecoded)); 

Hope this answers your doubt.

Sign up to request clarification or add additional context in comments.

8 Comments

I get The method encodeBase64(byte[]) is undefined for the type Base64
String str =Base64.encodeToString(strFinalXML.getBytes(), 0); //For java 1.7 +
@FranciscoCorralesMorales He's using org.apache.commons.codec.binary.Base64
@RupertMadden-Abbott encodeBase64 is not available under package java.util.Base64. To have above code working, You have to import Base64 from org.apache.commons.codec.binary.Base64
|
165

Java 8 now supports BASE64 Encoding and Decoding. You can use the following classes: java.util.Base64, java.util.Base64.Encoder and java.util.Base64.Decoder.

Example usage:

// encode with padding String encoded = Base64.getEncoder().encodeToString(someByteArray); // encode without padding String encoded = Base64.getEncoder().withoutPadding().encodeToString(someByteArray); // decode a String byte [] barr = Base64.getDecoder().decode(encoded); 

3 Comments

When to use with padding vs without padding??
Take care when switching from apache to java 8: org.apache.commons.codec.binary.Base64.decodeBase64 also accepts mime encoded input, but java.util.Base64.getDecoder not. If you also want to decode mime encoded you need to use java.util.Base64.getMimeDecoder.
@IgorGanapolsky here is the anwer to what is padding? stackoverflow.com/questions/4080988/… It has to do with filling the String with 0 bytes if its odd
67

The accepted answer uses the Apache Commons package but this is how I did it using Java's native libraries

Java 8 and up

import java.util.Base64; public class Base64Encoding { public static void main(String[] args) { Base64.Encoder enc = Base64.getEncoder(); Base64.Decoder dec = Base64.getDecoder(); String str = "77+9x6s="; // encode data using BASE64 String encoded = enc.encodeToString(str.getBytes()); System.out.println("encoded value is \t" + encoded); // Decode data String decoded = new String(dec.decode(encoded)); System.out.println("decoded value is \t" + decoded); System.out.println("original value is \t" + str); } } 

Java 6 - 10

import java.io.UnsupportedEncodingException; import javax.xml.bind.DatatypeConverter; public class EncodeString64 { public static void main(String[] args) throws UnsupportedEncodingException { String str = "77+9x6s="; // encode data using BASE64 String encoded = DatatypeConverter.printBase64Binary(str.getBytes()); System.out.println("encoded value is \t" + encoded); // Decode data String decoded = new String(DatatypeConverter.parseBase64Binary(encoded)); System.out.println("decoded value is \t" + decoded); System.out.println("original value is \t" + str); } } 

The better way would be to try/catch the encoding/decoding steps but hopefully you get the idea.

4 Comments

Note that this will not work on Android.
Note that javax.xml.bind (JAXB) is removed in Java 11.
Updated. Thanks, Dennie.
java.util.Base64 is available starting Java 8, not 11.
27

For Spring Users , Spring Security has a Base64 class in the org.springframework.security.crypto.codec package that can also be used for encoding and decoding of Base64. Ex.

 public static String base64Encode(String token) { byte[] encodedBytes = Base64.encode(token.getBytes()); return new String(encodedBytes, Charset.forName("UTF-8")); } public static String base64Decode(String token) { byte[] decodedBytes = Base64.decode(token.getBytes()); return new String(decodedBytes, Charset.forName("UTF-8")); } 

1 Comment

For those of you who are looking this now 'org.springframework.security.crypto.codec.Base64' is deprecated
24

The following is a good solution -

import android.util.Base64; String converted = Base64.encodeToString(toConvert.toString().getBytes(), Base64.DEFAULT); String stringFromBase = new String(Base64.decode(converted, Base64.DEFAULT)); 

That's it. A single line encoding and decoding.

2 Comments

it is renamed to java.util.Base64.
great solution without any libraries!
8
import javax.xml.bind.DatatypeConverter; public class f{ public static void main(String a[]){ String str = new String(DatatypeConverter.printBase64Binary(new String("user:123").getBytes())); String res = DatatypeConverter.parseBase64Binary(str); System.out.println(res); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.