Skip to content

Instantly share code, notes, and snippets.

@rohitshampur
Last active December 19, 2021 12:51
Show Gist options
  • Save rohitshampur/da5f79c34260150aafc1 to your computer and use it in GitHub Desktop.
Save rohitshampur/da5f79c34260150aafc1 to your computer and use it in GitHub Desktop.
Mimic Java's PBEWithMD5AndDES algorithm to produce a DES key
from Crypto.Hash import MD5
from Crypto.Cipher import DES
import re
import base64
_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)
hasher.update(_salt)
result = hasher.digest()
for i in range(1, _iterations):
hasher = MD5.new()
hasher.update(result)
result = hasher.digest()
return DES.new(result[:8], DES.MODE_CBC, result[8:16])
def encrypt(password):
plaintext_to_encrypt = password
# Pad plaintext per RFC 2898 Section 6.1
padding = 8 - len(plaintext_to_encrypt) % 8
plaintext_to_encrypt += chr(padding) * padding
encoder = getCrypt()
encrypted = encoder.encrypt(plaintext_to_encrypt)
return encrypted.encode('base64')
def decrypt(encodedpass):
plaintext_to_encrypt = encodedpass
enc = base64.b64decode(encodedpass)
decoder = getCrypt()
decrypted = decoder.decrypt(enc)
return decrypted.rstrip('\2,\1,\3,\4,\5,\6,\7,\0')
@brijeshprasad89
Copy link

Where can I get the java version ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment