3

I need a way to ignore the 'incorrect padding' exception when trying to decode a base32 string using base64 lib.

I have seen this post Python: Ignore 'Incorrect padding' error when base64 decoding which resolves the problem for base64 (b64decode) decoding. I tried to do the same (add the maximum number of accepted paddings, which if I'm not msitaken is 6 for base32) like this

b32decode(str(decoding) + "======", True, None)

But the exception raises anyway.

The expected result is to have a base32 string decoded anyway even without the correct padding:

decoding = JBSWY3DPEBZXIYLDNMQG65TFOJTGY33XEE== #this string should have 6 '=' as padding
print(b32decode(str(decoding) + "======", True, None))
>> Hello stack overflow! 
Carlos Mediavilla
  • 184
  • 1
  • 1
  • 14

3 Answers3

3

As far as I've tested. This line of code will make it works.

pad_length = math.ceil(len(b32_string) / 8) * 8 - len(b32_string)
bytes_data = base64.b32decode(b32_string.encode('ascii') + b'=' * pad_length)
pakawinz
  • 155
  • 5
2

Background

A base32 character contains 5 bits of data. The input to the encoder comes in the form of bytes (8 bits). This creates some awkwardness. Like when encoding one byte, you get 5+3 bits, with two bytes, you get 5+5+5+1 bits, and so on.

The only time things are not awkward is when there's 40 bits, because it will perfectly fit 5 bytes (ASCII characters) of input, and 8 base32-characters of output.

And so, the RFC4648 standard states that when things do not align, padding characters (=) are added until it does.

So, if an unpadded string is evenly divisible by 8, no action needs to be taken. Otherwise, padding characters must be added so that it aligns with the 40-bit a.k.a. 8 base32-character blocks.

Solution

last_block_width = len(unpadded_str) % 8
if last_block_width != 0:
  unpadded_str += (8 - last_block_width) * '='
Sebastian N
  • 759
  • 7
  • 13
0

You should not append whole 6 '=' characters, you need to pad suffix to have 6 equality chars:

> base64.b32decode("JBSWY3DPEBZXIYLDNMQG65TFOJTGY33XEE======")
'Hello stack overflow!'

To correctly pad message you should follow https://www.rfc-editor.org/rfc/rfc4648#section-6

Community
  • 1
  • 1
Nickolay Olshevsky
  • 13,706
  • 1
  • 34
  • 48
  • I know this specific string needs 6 '=' as padding. What I meant is if there is a way to decode any character using b32decode even when the string itself is not correctly padded. In the post I link, you can append three '=' signs to the string, and the command itself rounds up the padding and deletes the padding symbols not needed. For example, If, when using b64decode, I put a string which should have two padding characters '==' with one, and then I add the three padding charters at the end `+ '==='`, the command auto deletes two of them so the string ends up having two padding characters – Carlos Mediavilla Dec 24 '18 at 11:02
  • The stupid but easiest solution would be to add from 1 to 6 '=' to the end of the message and try to decode :) The more correct solution would be to check number of characters (except trailing padding), and according to the link I specified add correct number of '=' – Nickolay Olshevsky Dec 25 '18 at 13:02