Python decode base64 incorrect padding

[Solved] Error “incorrect padding” in decoding of Base64 module in Python

Problem description
decodes the field information encoded by Base64, which is normally decoded in the base64 encoding and decoding tool, but b64decode and A2B in the modules Base64 and binascii under python_ Decoding error in Base64 and other methods
the error information is as follows

--------------------------------------------------------------------------- Error Traceback (most recent call last) in get_proxies(urls) 14 try: ---> 15 raw = base64.b64decode(response) 16 except Exception as r: c:\program files\python3\lib\base64.py in b64decode(s, altchars, validate) 86 raise binascii.Error('Non-base64 digit found') ---> 87 return binascii.a2b_base64(s) 88 Error: Incorrect padding 

Base64 in Python is read in 4, so the field to be decoded should be a multiple of 4, not enough to add ‘=’

# The field a to be decoded is judged, and if it is a multiple of 4, it is unchanged, and if not, how much is missing, how much is made up a = a + '=' * (4 - len(a) % 4) if len(a) % 4 != 0 else a 

Read More:

  • Django Error: binascii.Error: Incorrect padding [How to Solve]
  • Zlib.error: error – 3 while decompressing: incorrect header check in Python
  • Python Use sqlalchemy Error: pymssql.ProgrammingError: (102, b“Incorrect syntax near ‘(‘.DB-Lib error
  • Importerror: DLL load failed: unable to find the specified module in Python
  • Python: How to Solve multiprocessing module Error in Windows
  • [Solved] Python Networkx Error: Network error: random_state_index is incorrect
  • Python Error: ValueError: invalid literal for int() with base 16: ‘ ‘
  • [Solved] NPM install Error: check python checking for Python executable python2 in the PATH
  • How to Solve no module fcntl Error in Windows
  • How to Fix “HTTP error 403: forbidden” in Python 3. X
  • [example multitasking] Python multithreading module
  • Full explanation of SYS module of Python
  • [Solved] error indicates that your module has parameters that were not used in producing loss
  • Python PIP Fatal error in launcher: Unable to create process using ‘“e:\program files\programdata
  • Python 3 uses the relative path import module
  • Error in installing Python 3.6.8 for MacBook pyenv [Solved]
  • [Solved] ParserError: NULL byte detected. This byte cannot be processed in Python‘s native csv library
  • ModuleNotFoundError: No module named ‘tensorflow.python’ And the pits encountered after installation
Читайте также:  Android room java example

Источник

Python: Ignore ‘Incorrect Padding’ Error When Base64 Decoding

You have at least one string in your CSV file that is either not a Base64 string, is a corrupted (damaged) Base64 string, or is a string that is missing the required = padding. Your example value, ABHPdSaxrhjAWA= , is short one = or is missing another data character.

Base64 strings, properly padded, have a length that is a multiple of 4, so you can easily re-add the padding:

value = csvlines[0]
if len(value) % 4:
# not a multiple of 4, add padding:
value += '=' * (4 - len(value) % 4)
csvlines[0] = value.decode("base64").encode("hex")

If the value then still fails to decode, then your input was corrupted or not valid Base64 to begin with.

For the example error, ABHPdSaxrhjAWA= , the above adds one = to make it decodable:

>>> value = 'ABHPdSaxrhjAWA='
>>> if len(value) % 4:
. # not a multiple of 4, add padding:
. value += '=' * (4 - len(value) % 4)
.
>>> value
'ABHPdSaxrhjAWA=='
>>> value.decode('base64')
'\x00\x11\xcfu&\xb1\xae\x18\xc0X'
>>> value.decode('base64').encode('hex')
'0011cf7526b1ae18c058'

I need to emphasise that your data may simply be corrupted. Your console output includes one value that worked, and one that failed. The one that worked is one character longer, and that’s the only difference:

ABHvPdSaxrhjAWA=
ABHPdSaxrhjAWA=

Note the v in the 4th place; this is missing from the second example. This could indicate that something happened to your CSV data that caused that character to be dropped from the second example. Adding in padding can make the second value decodable again, but the result would be wrong. We can’t tell you which of those two options is the cause here.

How to avoid Incorrect padding error while Base64 Decoding this string in Python

Well, it looks like the message is split by . into three parts. The first two parts are base64 encoded, while the last one is not:

import base64

res = "eyJqa3UiOiJodHRwczovL2U5N2I4YTlkNjcyZTRjZTQ4NDVlYzY5NDdjZDY2ZWY2LXNiLmJhYXMubmludGVuZG8uY29tLzEuMC4wL2NlcnRpZmljYXRlcyIsImtpZCI6ImZlOWRiYmZmLTQ3MGItNDZjOC04YmFmLTFiNzY5OGRlZTViZSIsImFsZyI6IlJTMjU2In0.eyJpc3MiOiJodHRwczovL2U5N2I4YTlkNjcyZTRjZTQ4NDVlYzY5NDdjZDY2ZWY2LXNiLmJhYXMubmludGVuZG8uY29tIiwiZXhwIjoxNTQ1MTg1NDk2LCJ0eXAiOiJpZF90b2tlbiIsImF1ZCI6IjhkOTc1NTllNjNlY2NkNTYiLCJiczpkaWQiOiI2NjJhZTQwOWYwNTQyYTBjIiwic3ViIjoiOTNkYmYwNDdiYTI3NzQ5NSIsImp0aSI6IjY1NDg4ZjJmLTI1NzAtNDBkYy04ODQ3LTMzODNlZWIxMGJiYiIsIm5pbnRlbmRvIjp7ImFpIjoiMDEwMGY4MDAwMDQ5MjAwMCIsImF2IjoiMDAwMCIsImVkaSI6ImJjNTdiYmM3MTZlMDA1MGFmOWRhN2NkYTIzMWRjZDgyIiwiYXQiOjE1NDUxNzQ2OTZ9LCJpYXQiOjE1NDUxNzQ2OTZ9.ZMUIt3wYrbfhXnnDh4WraGlKrZy0YuL5prluY70sU_-0W5XvWIB-xmTrLz7LJWHEGwTskcWf81_HBq_mSb75rMfTAEBwBmOJ4ITmhdnXksz8w7EDOWuPPSEft5XLMNOMD16ztEOYe5ddU_iqNEbT56L7fcAJEXv0FWy6H_OutxOglYpDaNkcj6CWJ7dpA0JbqerR9dEszaLwyn1ZBDPVD0YeAIm5bEr61imeedzMb0amxlTl4R87mqK6epsFUnRy6p6Klr27_DlTLQ-gej09W7NeNzONCj4thHgCr9szAiaN28krfTc2fobz3qFCoC_eQghiIIZBe_-Lksng3Eg6tw"

for i in res.split("."):
print(base64.b64decode(i + '=' * (-len(i) % 4)))

I guess the last one is a signature, which is used to validate the first two parts. Do you get this string from a cookie? Or from a submitted form?

Edit

So for anyone sees this answer, the given string is a JWT string.

binascii.Error: Incorrect padding, even when string length is multiple of 4

by checking your link, your string has 200000 bytes all right, but it contains the header:

strOne = b"data:image/png;base64,iVBORw0KGgoAAAANSU.

This is part of MIME message or something. You have to strip this first.

pad = len(strOne)%4
strOne += b» we believe in team work» 🙂

python b64decode incorrect padding

Your description of what you are doing sounds OK. Choice of the input piece size affects only the efficiency. Padding bytes are minimised if the length of each input piece (except of course the last) is a multiple of 3.

You need to show us both your server code and your client code. Alternatively: on the server, log the input and the pieces transmitted. On the client, log the pieces received. Compare.

Curiosity: Why don’t you just b64encode the whole string, split the encoded result however you like, transmit the pieces, at the client reassemble the pieces using b».join(pieces) and b64decode that?

Further curiosity: I thought the contents of a UDP packet could be any old binary bunch of bytes; why are you doing base64 encoding at all?

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

base64 throws ‘incorrect padding’ exception when the issue is NOT with the padding #77951

base64 throws ‘incorrect padding’ exception when the issue is NOT with the padding #77951

3.7 (EOL) end of life 3.8 only security fixes extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error

Comments

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

assignee = None closed_at = Date 2018-06-11.05:31:13.374> created_at = Date 2018-06-04.19:53:31.414> labels = ['extension-modules', '3.8', 'type-bug', '3.7'] title = "base64 throws 'incorrect padding' exception when the issue is NOT with the padding" updated_at = Date 2018-06-11.05:31:13.373> user = 'https://github.com/dniq'
activity = Date 2018-06-11.05:31:13.373> actor = 'ned.deily' assignee = 'none' closed = True closed_date = Date 2018-06-11.05:31:13.374> closer = 'ned.deily' components = ['Extension Modules'] creation = Date 2018-06-04.19:53:31.414> creator = 'dniq' dependencies = [] files = [] hgrepos = [] issue_num = 33770 keywords = ['patch'] message_count = 23.0 messages = ['318691', '318697', '318701', '318704', '318716', '318721', '318727', '318728', '318729', '318733', '318752', '318755', '318762', '318789', '319093', '319094', '319101', '319102', '319104', '319202', '319248', '319249', '319272'] nosy_count = 7.0 nosy_names = ['terry.reedy', 'mark.dickinson', 'taleinat', 'ned.deily', 'r.david.murray', 'serhiy.storchaka', 'dniq'] pr_nums = ['7416', '7602'] priority = 'normal' resolution = 'fixed' stage = 'resolved' status = 'closed' superseder = None type = 'behavior' url = 'https://bugs.python.org/issue33770' versions = ['Python 3.7', 'Python 3.8']

The text was updated successfully, but these errors were encountered:

Источник

Оцените статью