Your problem is related to the repeated transforming between byte and String representations.
I made a change to your code to have the decodeBase64 method return the generated byte rather than transforming it to a String (lines 23 and 24), and also made a corresponding change to where the method is called (lines 72 and 73). After that change it does seem to work, but I think you need to refactor your code and remove most of where you convert between bytes and Strings.
posted 3 weeks ago
Thanks a lot Ron ! That was the key to solving the issue.
On a sidenote, may I know why does transforming from byte and String affect the bytes ?
In my experience, both "new String(byte)" and "String.getBytes()" are the source of many bugs. Whenever you're thinking about using them, consider if there are alternatives. Character data (which is what strings are) and binary data (like byte arrays) simply aren't the same thing, and converting between them can cause numerous issues.
WeiJie Lim wrote:On a sidenote, may I know why does transforming from byte and String affect the bytes ?
It's because a String isn't an array of bytes. It's an array of characters, instead. And a "character" is (approximately) a character of Unicode.
If you read that Wikipedia article a bit you'll soon notice that 256 different bytes aren't anywhere near enough to cover all of the Unicode characters. It so happens that the first 128 characters of Unicode are the ASCII character set, so if all your data fall into that category then you won't run into trouble. But once you get beyond that then you're going to have to decide how to represent the non-ASCII characters as bytes. You've already encountered UTF-8, which is one common way to do that.