Created
February 18, 2016 12:00
-
-
Save w4kfu/89713cea3f27c3e1b064 to your computer and use it in GitHub Desktop.
PBE with MD5 and DES
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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