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 Overflow
🌐
Coderanch
coderanch.com › t › 784478 › java › Base-getDecoder-throwing-exception
Base64.getDecoder() is throwing an exception. (Java in General forum at Coderanch)
September 6, 2024 - It appears like your encoded string has one or more characters (could be non-printing characters) in addition to the encoded value. For example: using the encoded String of "OQ==", it decodes as expected But if a space is added to the end of the String, it fails in the same way that you experienced
Discussions

base64 string decode error
I think both of your strings are incorrectly padded, they should be in multiples of 3 characters. https://stackoverflow.com/a/49459036 This answer says you can just add extra padding in all cases and it will be truncated. More on reddit.com
🌐 r/learnpython
11
1
April 29, 2022
bash - base64: invalid input error when trying to decode contents of yaml file - Unix & Linux Stack Exchange
printf '%s' "$myVar" | base64 --decode --ignore-garbage · Here I have also switched from using echo to printf (without a trailing newline) so that the output is better controlled. * Except when you really know you have a necessary reason not to quote. These cases are few and far between. More on unix.stackexchange.com
🌐 unix.stackexchange.com
April 14, 2022
Python Base64 Decode Exception in Server - Stack Overflow
This is unrelated to the encryption part. The Incorrect padding error refers to the padding in the base64 encoding, i.e. the trailing one or two =, ==. The b64decode function should be able to decode the output of a previous b64encode call. It would seem very likely to me that the problem is ... More on stackoverflow.com
🌐 stackoverflow.com
java - How to make Android Base64.decode reliable throws exception (Or returning null) given an invalid base64 encoded string? - Stack Overflow
In Java SE, if given an invalid base64 encoded String, the decoder will throw exception. More on stackoverflow.com
🌐 stackoverflow.com
September 24, 2019
People also ask

How do I decode a Base 64 URL encoded string in Mule 4?
You can't use the `fromBase64` DataWeave 2.0 function for a URL-encoded string, but you can use the Mule 4 Scripting Module: drop in an Execute component, select the Groovy engine, and run `Base64.getUrlDecoder().decode(payload)`. Make sure to add a Transform Message before it that casts the payload to a Java String (`output application/java` with `payload as String`).
🌐
prostdev.com
prostdev.com › home › blog › understanding the "illegal base64 character" error (java, groovy and mule 4 - dw 2.0)
Understanding the "illegal base64 character" error (Java, Groovy ...
Why won't the fromBase64 DataWeave function decode my URL-encoded string?
The `fromBase64` function transforms a basic Base 64 string, whose alphabet does not include the minus or underline characters used by the Base 64 URL encoding. When it hits an unrecognized character it throws an error, such as `Illegal base64 character 5f` for an underscore or `Illegal base64 character 2d` for a minus.
🌐
prostdev.com
prostdev.com › home › blog › understanding the "illegal base64 character" error (java, groovy and mule 4 - dw 2.0)
Understanding the "illegal base64 character" error (Java, Groovy ...
Why does my URI component string throw an Illegal base64 character error?
Because a URI component string is not a Base64 encoded payload. As Alex notes in the comments, a string like `%7B%22type%22%3A%22block_actions%22` contains a percentage character (% - 25 in Hex) that doesn't exist in either Base 64 alphabet, and it's human-readable and lacks the trailing `==` of an encoded string. To convert it to JSON, use `read(decodeURIComponent(payload))` with the `dw::core::URL` module instead.
🌐
prostdev.com
prostdev.com › home › blog › understanding the "illegal base64 character" error (java, groovy and mule 4 - dw 2.0)
Understanding the "illegal base64 character" error (Java, Groovy ...
🌐
Python
docs.python.org › 3 › library › base64.html
base64 — Base16, Base32, Base64, Base85 Data Encodings
Decode the Base64 encoded bytes-like object or ASCII string s and return the decoded bytes. Optional altchars must be a bytes-like object or ASCII string of length 2 which specifies the alternative alphabet used instead of the + and / characters. A binascii.Error exception is raised if s is incorrectly padded.
🌐
Reddit
reddit.com › r/learnpython › base64 string decode error
r/learnpython on Reddit: base64 string decode error
April 29, 2022 -

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.

🌐
Apache JIRA
issues.apache.org › jira › browse › CODEC-279
[CODEC-279] Base64.decode fails on Java11 for certain valid base 64 encoded String - ASF Jira
java.lang.IllegalArgumentException: Last encoded character (before the paddings if any) is a valid base 64 alphabet but not a possible value at org.apache.commons.codec.binary.Base64.validateCharacter(Base64.java:798) at org.apache.commons.codec.binary.Base64.decode(Base64.java:472) at ...
🌐
Opster
opster.com › failed to decode base64 string – – how to solve this elasticsearch error
Failed to decode base64 string - - Common causes and quick fixes
September 11, 2023 - Briefly, this error occurs when Elasticsearch tries to decode a string that is not in valid Base64 format. Base64 is a binary-to-text encoding scheme used to represent binary data in an ASCII string format.
🌐
Apache JIRA
issues.apache.org › jira › browse › CODEC-263
[CODEC-263] Base64.decodeBase64 throw exception - ASF Jira
Codec upgrade to 1.13, code throw exception as follows: · @Test public void test(){ Base64.decodeBase64("publishMessage"); } exception like: ·
Find elsewhere
🌐
ProstDev
prostdev.com › home › blog › understanding the "illegal base64 character" error (java, groovy and mule 4 - dw 2.0)
Understanding the "illegal base64 character" error (Java, Groovy and Mule 4 - DW 2.0) | ProstDev
July 21, 2020 - In other words, if you have an encoded string like “cHJvc3RkZXY_YmxvZw==”, you wouldn’t be able to transform it using a basic Base64 decoder because it contains characters that are not recognized by its alphabet (the underline character).
🌐
Base64.Guru
base64.guru › tools › repair
Repair Base64 | Tools | Base64
August 17, 2026 - Quite often, users cannot decode Base64, because they got a malformed or invalid string. For example, this can happen if the source outputs an unexpected Base64 standard, the user copied unnecessary data, or something damaged the Base64 value. As a rule, such strings cannot be decoded without ...
🌐
Stack Overflow
stackoverflow.com › questions › 37013144 › python-base64-decode-exception-in-server
Python Base64 Decode Exception in Server - Stack Overflow
This is unrelated to the encryption part. The Incorrect padding error refers to the padding in the base64 encoding, i.e. the trailing one or two =, ==. The b64decode function should be able to decode the output of a previous b64encode call. It would seem very likely to me that the problem is ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Base64.Decoder.html
Base64.Decoder (Java Platform SE 8 )
July 21, 2026 - If there is a padding character present in the final unit, the correct number of padding character(s) must be present, otherwise IllegalArgumentException ( IOException when reading from a Base64 stream) is thrown during decoding.
🌐
Snowflake Community
community.snowflake.com › s › article › How-to-avoid-string-is-not-a-legal-base64-encoded-value-error
How to avoid "string is not a legal base64-encoded value" error for BASE64_DECODE_STRING function
December 19, 2023 - Similar to traditional OLTP systems like MySQL and PostgreSQL, the BASE64_DECODE_STRING functions in Snowflake don't append '=' if the padding '='s are missing in the original encoded string. This can sometimes cause the Snowflake queries to fail with the error 'string is not a legal base64-encoded value' while other tools can decode the string correctly.
🌐
GitHub
github.com › bcgit › bc-java › issues › 236
org.bouncycastle.util.encoders.DecoderException: unable to decode base64 string: invalid characters encountered in base64 data · Issue #236 · bcgit/bc-java
October 6, 2017 - In my project I've updated version of Bouncy Castle: from bcprov-jdk16-146.jar to bcprov-jdk15on-1.57.jar And now in my integration test I get an exception: org.bouncycastle.util.encoders.DecoderException: unable to decode base64 string: invalid characters encountered in base64 data at org.bouncycastle.util.encoders.Base64.decode(Unknown Source) at com.project.isx.server.AuthenticationTicket.<init>(AuthenticationTicket.java:570) at com.project.isx.server.AuthenticationTicket.readTicket(AuthenticationTicket.java:388) at com.project.isx.server.ResourceProcessor.init(ResourceProcessor.java:218) a
Author: bcgit
🌐
Thisdevtool
thisdevtool.com › home › blog › fix base64 decode errors: padding & invalid char
Fix Base64 Decode Errors: Padding & Invalid Char | ThisDevTool
May 27, 2026 - A Base64 string that refuses to decode almost always fails for one of three reasons: padding was lost, an illegal character snuck in, or the wrong alphabet was used. The error messages are vague on purpose.
🌐
Stack Overflow
stackoverflow.com › questions › 59583668 › which-string-could-give-an-unsupportedencodingexception-when-using-base64-encodi
java - Which String could give an UnsupportedEncodingException when using Base64 encoding - Stack Overflow
April 1, 2020 - ... No Base 64 encoded string can cause an UnsupportedEncodingException in your program. The exception is raised if the charsetName parameter of String.getBytes(String) or String(byte[], String) is invalid.
🌐
InventiveHQ
inventivehq.com › home › blog › web development › what are common base64 errors?
What Are Common Base64 Errors?
July 18, 2026 - Most Base64 errors come from one of six causes: broken padding (the string's length isn't a multiple of 4), mixing the standard alphabet (+ /) with the URL-safe alphabet (- _), whitespace or newlines left in by MIME/PEM wrapping, decoding a data: URI without stripping its prefix, a character-encoding mismatch on non-ASCII text, or treating Base64 as encryption when it is neither secret nor a cipher. Almost every "invalid Base64" or "incorrect padding" exception traces back to one of these — and each has a mechanical, repeatable fix.
Address: 2305 Historic Decatur Rd, Suite 100, 92106, San Diego