Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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(); 

//bs.encodeBytes(byteArray); 

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

//bs.decode(bs.encodeBytes(byteArray));

System.out.println(bs.decode(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 http://iharder.net/base64

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

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...