-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathdecode_base64_with_Incorrect_padding.py
35 lines (22 loc) · 1.72 KB
/
decode_base64_with_Incorrect_padding.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "ipetrash"
# SOURCE: https://stackoverflow.com/a/32517907/5909792
import base64
def base64_decode(text: str) -> bytes:
text += "=" * (-len(text) % 4)
return base64.b64decode(text)
if __name__ == "__main__":
bad_b64_string = "7P1pbxvJtjXqfr+/Qnd92huoKrFVk8ABXhUl1+td7iDJ68L3+IBISbTMpYa+pMQq1cH57zeSndjMmZEyqYwYkwN7G7UsU1SMZIQ4Hmb3f9Yq1cNfK41fK7Wd6l5SqSTNyv"
try:
print(base64.b64decode(bad_b64_string))
except Exception as e:
print(e) # Incorrect padding
print(base64_decode(bad_b64_string))
# b'\xec\xfdio\x1b\xc9\xb65\xea~\xbf\xbfBw}\xda\x1b\xa8*\xb1U\x93\xc0\x01^\x15%\xd7\xeb]\xee \xc9\xeb\xc2\xf7\xf8\x80HI\xb4\xcc\xa5\x86\xbe\xa4\xc4*\xd5\xc1\xf9\xef7\x92\x9d\xd8\xcc\x99\x912\xa9\x8c\x18\x93\x03{\x1b\xb5,ST\x8cd\x848\x1ef\xf7\x7f\xd6*\xd5\xc3_+\x8d_+\xb5\x9d\xea^R\xa9$\xcd\xca'
print()
good_b64_string = "7P1pbxvJtjXqfr+/Qnd92huoKrFVk8ABXhUl1+td7iDJ68L3+IBISbTMpYa+pMQq1cH57zeSndjMmZEyqYwYkwN7G7UsU1SMZIQ4Hmb3f9Yq1cNfK41fK7Wd6l5SqSTNyv=="
print(base64.b64decode(good_b64_string))
# b'\xec\xfdio\x1b\xc9\xb65\xea~\xbf\xbfBw}\xda\x1b\xa8*\xb1U\x93\xc0\x01^\x15%\xd7\xeb]\xee \xc9\xeb\xc2\xf7\xf8\x80HI\xb4\xcc\xa5\x86\xbe\xa4\xc4*\xd5\xc1\xf9\xef7\x92\x9d\xd8\xcc\x99\x912\xa9\x8c\x18\x93\x03{\x1b\xb5,ST\x8cd\x848\x1ef\xf7\x7f\xd6*\xd5\xc3_+\x8d_+\xb5\x9d\xea^R\xa9$\xcd\xca'
print(base64_decode(good_b64_string))
# b'\xec\xfdio\x1b\xc9\xb65\xea~\xbf\xbfBw}\xda\x1b\xa8*\xb1U\x93\xc0\x01^\x15%\xd7\xeb]\xee \xc9\xeb\xc2\xf7\xf8\x80HI\xb4\xcc\xa5\x86\xbe\xa4\xc4*\xd5\xc1\xf9\xef7\x92\x9d\xd8\xcc\x99\x912\xa9\x8c\x18\x93\x03{\x1b\xb5,ST\x8cd\x848\x1ef\xf7\x7f\xd6*\xd5\xc3_+\x8d_+\xb5\x9d\xea^R\xa9$\xcd\xca'