How to encode/decode in base64 in Java


Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content
// {...}
String string = "some random data";
// Encoding
Encoder encoder = Base64.getEncoder();
byte[] data = string.getBytes(UTF_8);
String encodedString = encoder.encodeToString(data);
System.out.println("Encoded: " + encodedString);
// Decoding
Decoder decoder = Base64.getDecoder();
byte[] bytes = decoder.decode(encodedString);
String decodedString = new String(bytes, UTF_8);
System.out.println("Decoded: " + decodedString);
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content