Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
in Java by (3.9k points)

Does anyone know how to decode and encode a string in Base64 using the Base64. I am using the following code, but it's not working.

String source = "password"; 

byte[] byteArray = source.getBytes("UTF-16"); 

Base64 bs = new Base64(); 


System.out.println( bs.encodeBytes(byteArray)); 



1 Answer

0 votes
by (46k points)

To anyone else who ended up here while searching for info on how to decode a string encoded with Base64.encodeBytes(), here was my solution:

// encode

String ps = "techPass";

String tmp = Base64.encodeBytes(ps.getBytes());

// decode

String ps2 = "dGVjaFBhC3M=";

byte[] tmp2 = Base64.decode(ps2); 

String val2 = new String(tmp2, "UTF-8"); 

Also, I'm supporting older versions of Android so I'm using Robert Harder's Base64 library from

Related questions

0 votes
1 answer
asked Jul 16, 2019 in Java by Suresh (3.4k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories