Skip to content

Instantly share code, notes, and snippets.

@btall
Forked from crmccreary/AESCipher.py
Last active January 13, 2021 02:06
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save btall/f528411c95cbce86f284 to your computer and use it in GitHub Desktop.
Save btall/f528411c95cbce86f284 to your computer and use it in GitHub Desktop.
Encryption using pycrypto, AES/ECB/PKCS5Padding
from Crypto.Cipher import AES
from Crypto import Random
BS = 16
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s: s[0:-ord(s[-1])]
class AESCipher:
def __init__(self, key):
"""
Requires hex encoded param as a key
"""
self.key = key.decode("hex")
def encrypt(self, raw):
"""
Returns hex encoded encrypted value!
"""
raw = pad(raw)
cipher = AES.new(self.key, AES.MODE_ECB)
return cipher.encrypt(raw).encode("hex")
def decrypt(self, enc):
"""
Requires hex encoded param to decrypt
"""
enc = enc.decode("hex")
cipher = AES.new(self.key, AES.MODE_ECB)
return unpad(cipher.decrypt(enc))
if __name__ == "__main__":
key = "140b41b22a29beb4061bda66b6747e14"
ciphertext = "6c94326b6b726eb7c48b049a6e2fcf008a10ea14b603e6a6a38709b77d2af03f119dc26f9522e3ae43f1a5e0c5bb8493"
key = key[:32]
decryptor = AESCipher(key)
plaintext = decryptor.decrypt(ciphertext)
print "%s" % plaintext
@Lujeni
Copy link

Lujeni commented Aug 3, 2015

GG !

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