Skip to content

Instantly share code, notes, and snippets.

@pwndad
Forked from rohitshampur/crypto.py
Last active October 14, 2020 12:36
Show Gist options
  • Save pwndad/7322ef6f98b453a7cb90164608212bd2 to your computer and use it in GitHub Desktop.
Save pwndad/7322ef6f98b453a7cb90164608212bd2 to your computer and use it in GitHub Desktop.
Mimic Java's PBEWithMD5AndDES algorithm to produce a DES key
import base64
from Crypto.Hash import MD5
from Crypto.Cipher import DES
try:
from Cryptodome.Util import Padding
except ImportError:
from Crypto.Util import Padding
_password = '111' #use your own pass key
_salt = '\xA9\x9B\xC8\x32\x56\x35\xE3\x03' #use your salt here
_iterations = 19 #change iterations here
def getCrypt():
hasher = MD5.new()
hasher.update(_password.encode())
hasher.update(_salt.encode())
result = hasher.digest()
for i in range(1, _iterations):
hasher = MD5.new()
hasher.update(result)
result = hasher.digest()
return DES.new(result[:DES.block_size], DES.MODE_CBC, result[DES.block_size:DES.block_size*2])
def encrypt(password):
plaintext_to_encrypt = password
# Pad plaintext per RFC 2898 Section 6.1
padding = DES.block_size - len(plaintext_to_encrypt) % DES.block_size
plaintext_to_encrypt += chr(padding) * padding
encoder = getCrypt()
encrypted = encoder.encrypt(plaintext_to_encrypt)
return base64.b64encode( encrypted )
def decrypt(encodedpass):
enc = base64.b64decode(encodedpass)
decoder = getCrypt()
decrypted = decoder.decrypt(enc)
return Padding.unpad(decrypted , DES.block_size)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment