Skip to content

Instantly share code, notes, and snippets.

@w4kfu
Created February 18, 2016 12:00
Show Gist options
  • Save w4kfu/89713cea3f27c3e1b064 to your computer and use it in GitHub Desktop.
Save w4kfu/89713cea3f27c3e1b064 to your computer and use it in GitHub Desktop.
PBE with MD5 and DES
from Crypto.Hash import MD5
from Crypto.Cipher import DES
d_password = "PBE.class"
d_salt = "\xC7\x73\x21\x8C\x7E\xC8\xEE\x99"
def unpad_pkcs7(text, blocklength=16):
full_len = len(text)
pad_val = ord(text[-1])
pos = full_len - pad_val
if pad_val == 0 or text[-pad_val:] != chr(pad_val) * pad_val:
raise ValueError('bad padding')
return text[:pos]
def compute_key_iv(password, salt, iterations=20):
hasher = MD5.new()
hasher.update(password)
hasher.update(salt)
result = hasher.digest()
for i in xrange(1, iterations):
hasher = MD5.new()
hasher.update(result)
result = hasher.digest()
return result[:8], result[8:16]
def decrypt(plain, password, salt, iterations=20):
key, iv = compute_key_iv(password, salt, iterations)
decode = DES.new(key, DES.MODE_CBC, iv)
decrypted = decode.decrypt(plain)
decrypted = unpad_pkcs7(decrypted)
print decrypted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment