binascii.Error: Incorrect padding How to decode the end with /

2024/11/17 23:45:05

I received a string encoded with base64, I am using python to decode it, but decoding failed, I found that the string is followed by / ends, I don't know how to decode it, I haven't found the answer, who can Help me

data = 'dXN1c19pZD0xMDg2P2RvY01kPTE3Mzc4JnR5cGU9bmV3/'print(base64.urlsafe_b64decode(data))
print(base64.standard_b64decode(data))
print(base64.b64decode(data))
Answer

data is a normal base64 encoded string, containing only characters from the base64 character set. The problem is indeed the / on the end, because the length of a base64 string should be dividable by 4 without remainder. So there should be padding on the end if necessary to achieve this. With the / on the end data is 45 characters long, which means 44 base64 characters that could be decoded to 33 bytes and then the last character which encodes only 6 bits.

Just adding padding would not solve it, because you can only add two padding characters (=) but you need one more for the missing two bits.

So you can either cut it off like this:

lenmax = len(data) - len(data)%4   
print(base64.b64decode(data[0:lenmax]).decode())

or add something like 0== to fill it up to 48 characters. But then you would get an error in decode(), and I'm not a friend of inventing extra data.

Or ask/check the code of the sender to find out, why there is this lonely / on the end.

https://en.xdnf.cn/q/118717.html

Related Q&A

How to match words in a list with user input in python?

I am working on program which takes user input and replaces the words in a list with x. eg is the word is sucks and user input is "this word is sucks". the output should be "this word i…

How to plot a ROC curve using dataframe converted from CSV file

I was trying to plot a ROC curve by using the documentation provided by sklearn. My data is in a CSV file, and it looks like this.It has two classes Goodand Badscreenshot of my CSV fileAnd my code look…

SyntaxError: Non-ASCII character. Python

Could somebody tell me which character is a non-ASCII character in the following:Columns(str) – comma-seperated list of values. Works only if format is tab or xls. For UnitprotKB, some possible column…

A pseudocode algorithm for integer addition based on binary operation

I have tried for ages to come up with a solution but just cant get my head around it.It needs to be based on two integers on the use of standard logical operations which have direct hardware implementa…

How to efficiently split overlapping ranges?

I am looking for an efficient method to split overlapping ranges, I have read many similar questions but none of them solves my problem. The problem is simple, given a list of triplets, the first two e…

pass 2D array to linear regression (sklearn)

I want to pass 2D array to linear regression: x = [[1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 3, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1],[0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0,…

How do I fix this OverflowError?

I keep getting a "OverflowError: math range error". No matter what I input, the result is the same. Im running Python 3.3, and its finding the problem at the last line. How do I fix this? (A…

Pyinstaller subprocess.check_output error

Ive bundled my app with pyinstaller to 2 *.exegui_app.exe (onefile) config.ini \libs (onedir)winservice.exe+ all DLLs and libsWhen I manually install service with command winservice.exe install everyth…

Exception handler to check if inline script for variable worked

I need to add exception handling that considers if line 7 fails because there is no intersection between the query and array brands. Im new to using exception handlers and would appreciate any advice o…

Parameter list with single argument

When testing Python parameter list with a single argument, I found some weird behavior with print.>>> def hi(*x): ... print(x) ... >>> hi() () >>> hi(1,2) (1, 2) >>…