-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathm15.py
More file actions
executable file
·31 lines (25 loc) · 805 Bytes
/
m15.py
File metadata and controls
executable file
·31 lines (25 loc) · 805 Bytes
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
#!/usr/bin/env python3
"""PKCS#7 padding validation"""
import m09
def pkcs7(plaintext: bytes, blocksize: int = 16) -> bytes:
return m09.pkcs7(plaintext, blocksize)
def de_pkcs7(plaintext: bytes) -> bytes:
pad_length = plaintext[-1]
if not pad_length * bytes([pad_length]) == plaintext[-pad_length:]:
raise m09.PKCS7PaddingError(f"Expected {pad_length} bytes of padding")
return plaintext[:-pad_length]
def main() -> None:
s1 = b"ICE ICE BABY\x04\x04\x04\x04"
s2 = b"ICE ICE BABY\x05\x05\x05\x05"
s3 = b"ICE ICE BABY\x01\x02\x03\x04"
print(de_pkcs7(s1))
try:
print(de_pkcs7(s2))
except m09.PKCS7PaddingError:
pass
try:
print(de_pkcs7(s3))
except m09.PKCS7PaddingError:
pass
if __name__ == "__main__":
main()