Posts

Showing posts with the label Decode

Base64 Java Encode And Decode A String

Answer : 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. 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); The accepted answer uses the Apache Commons p...