Aleem
Aleem

Reputation: 3291

Base64 decoding a normal unencoded string in android not give any exception

Decoding with base64 an unencoded string on Android does not gives any error but returns a string with some special characters e.g encoded like. It should throw some IllegalArgumentException. Is there some native way in android to check that other than regex ?

private String decodeThisString = "I am a java String"; bytes[] deocdedBytes = Base64.decode(decodeThisString.getBytes(), Base64.DEFAULT);

Upvotes: 1

Views: 569

Answers (1)

Sultan Mahmud
Sultan Mahmud

Reputation: 1285

I think you do not need to remove the character when you will decode it, automatically they will be discarded at the time of decode. I have tested with encoding and decoding with the provided code and get the exact string after decode.

String decodeThisString = "I am a java String";
//encode
byte[] data = Base64.encode(decodeThisString.getBytes(StandardCharsets.UTF_8), Base64.DEFAULT);
String text = new String(data, StandardCharsets.UTF_8);

//decode
byte[] datas = Base64.decode(text, Base64.DEFAULT);
String texts = new String(datas, StandardCharsets.UTF_8);

Upvotes: 1

Related Questions