Could you try...
byte[] bytes = new byte[]{1,2,3,4,5,6,7,8,9};
String stringToStore = new String(Base64.encode(bytes));
byte[] restoredBytes = Base64.decode(stringToStore.getBytes());
Answer from nullpotent on Stack OverflowCould you try...
byte[] bytes = new byte[]{1,2,3,4,5,6,7,8,9};
String stringToStore = new String(Base64.encode(bytes));
byte[] restoredBytes = Base64.decode(stringToStore.getBytes());
Base64.encode(bytes).toString() does not return the String that you expect.
You should use
new String(Base64.encode(bytes))
as suggested by iccthedral.
base64 string decode error
bash - base64: invalid input error when trying to decode contents of yaml file - Unix & Linux Stack Exchange
Python Base64 Decode Exception in Server - Stack Overflow
java - How to make Android Base64.decode reliable throws exception (Or returning null) given an invalid base64 encoded string? - Stack Overflow
How do I decode a Base 64 URL encoded string in Mule 4?
Why won't the fromBase64 DataWeave function decode my URL-encoded string?
Why does my URI component string throw an Illegal base64 character error?
Hi everyone,
i need to decode a base64 string. Some strings that i have fails with those 2 errors:
- First
string_base64 = "+gAAAB8A5AC4AQADAAAbAQAAAAAABQAA9gDfxsAuAHuAgAACwAAAAAAAAC0ALoAAAAAAAAAAAACAPUAqAAAAPMAtAAFAGDqgwxPAAAAAADf08AAAAAADAAAAAwAAAAOQAAALoA8yAAAAAQAAAAHAAMyMDIyLTAyLTAxVDAyOjIwOjA5LjAwNTAwMFo8CgI9AMgwACDUMAAgYDEAIAEAAAD7AQAAqAEAAPsBAAABAAAAIYABCN4zACAiNQA"
Error
binascii.Error: Incorrect padding
- Second
string_base64 = "DgEAABwA9wC4AQADAAAbAQAAAAAABQAADwHfx4AuAHuAgAACwAAAAAAAAC0ALsAAAAAAAAAAAACAPUAqAAAAPMAtAAFAGDqgwxPAAAAAADf08AAAAAADAAAAAwAAAANwAAAAMAAADKAi8AAQAAAAoAAAAyMDIyLTAyLTAxVDAxOjIwOjA3LjA1NjU2OFoAzDAAIMgwACDUMAAgYDEAIAEAAAD7AQAAqAEAAPsBAAABAAAAIYABCN4zACAiNQAghjUAIAAAAAA="
Error
binascii.Error: Invalid base64-encoded string: number of data characters (265) cannot be 1 more than a multiple of 4
You can run them in this code and see the result:
decodedBytes = base64.b64decode(string_base64) print(decodedBytes)
I already tried to use "urlsafe_b64decode" instead of "b64decode", but the result is the same.
From xxd's man page
xxd creates a hex dump of a given file or standard input.
You're taking a hex dump for the decoded value.
You can reproduce it with the utility provided here
System.out.println(Hex.encodeHexString(outAsByte));
generates
3ce353e2e6264d8c223f4a26700c591bc6b7bca2
which matches your
// 3ce353e2e6264d8c223f4a26700c591bc6b7bca2
String expectedOut = "3ce353e2e6264d8c223f4a26700c591bc6b7bca2";
How did you come up with the expected outcome?
Decoding your input using base64 and UTF-8 results in <ãSâæ&M"?J&pYÆ·¼¢, which is your actual outcome, apart from the UTF-8 specific characters.