How to encode/decode in base64 in Java

[CG]BOUGA
1,263 views

Open Source Your Knowledge, Become a Contributor

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

Create Content
1
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// {...}
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);
//{...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content