Skip to content

Instantly share code, notes, and snippets.

@mikeecb
Created May 9, 2017 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeecb/84bfc914d0e1f6737889718ccff2446d to your computer and use it in GitHub Desktop.
Save mikeecb/84bfc914d0e1f6737889718ccff2446d to your computer and use it in GitHub Desktop.
Unpad Valid PKCS#7
def unpad_valid_pkcs7(buffer):
padding = buffer[-1]
for i in range(len(buffer)-1, len(buffer)-padding, -1):
if buffer[i] != buffer[-1]:
raise Exception("Bad PKCS#7 padding.")
new_buffer = bytearray()
new_buffer[:] = buffer[:-padding]
return new_buffer
unpad_valid_pkcs7(bytearray("ICE ICE BABY\x04\x04\x04\x03"))
Traceback (most recent call last):
File "15.py", line 10, in <module>
print unpad_valid_pkcs7(bytearray("ICE ICE BABY\x04\x04\x04\x03"))
File "15.py", line 5, in unpad_valid_pkcs7
raise Exception("Bad PKCS#7 padding.")
Exception: Bad PKCS#7 padding.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment