Skip to content

Instantly share code, notes, and snippets.

@h0rn3t
Forked from lopes/aes-ecb.py
Last active December 23, 2022 07:19
Show Gist options
  • Save h0rn3t/4216cf787b43060b5afc2d50086918bc to your computer and use it in GitHub Desktop.
Save h0rn3t/4216cf787b43060b5afc2d50086918bc to your computer and use it in GitHub Desktop.
Simple Python example of AES in ECB mode.
from hashlib import md5
from base64 import b64decode
from base64 import b64encode
from Crypto.Cipher import AES
# Padding for the input string --not
# related to encryption itself.
BLOCK_SIZE = 16 # Bytes
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * \
chr(BLOCK_SIZE - len(s) % BLOCK_SIZE)
unpad = lambda s: s[:-ord(s[len(s) - 1:])]
class AESCipher:
"""
Usage:
c = AESCipher('password').encrypt('message')
m = AESCipher('password').decrypt(c)
Tested under Python 3 and PyCrypto 2.6.1.
"""
def __init__(self, key):
self.key = md5(key.encode('utf8')).hexdigest()
def encrypt(self, raw):
raw = pad(raw)
cipher = AES.new(self.key.encode("utf8"), AES.MODE_ECB)
return b64encode(cipher.encrypt(raw.encode('utf8')))
def decrypt(self, enc):
enc = b64decode(enc)
cipher = AES.new(self.key.encode("utf8"), AES.MODE_ECB)
return unpad(cipher.decrypt(enc)).decode('utf8')
##
# MAIN
# Just a test.
msg = input('Message...: ')
pwd = input('Password..: ')
print('Ciphertext:', AESCipher(pwd).encrypt(msg))
@dimitris1343
Copy link

I tried to run this but i had this problem:

Message...: hello
Password..: 1234
Traceback (most recent call last):
File "aes-ecb.py", line 43, in
print('Ciphertext:', AESCipher(pwd).encrypt(msg))
File "aes-ecb.py", line 28, in encrypt
cipher = AES.new(self.key, AES.MODE_ECB)
File "/home/papei1/.local/lib/python3.8/site-packages/Crypto/Cipher/AES.py", line 232, in new
return _create_cipher(sys.modules[name], key, mode, *args, **kwargs)
File "/home/papei1/.local/lib/python3.8/site-packages/Crypto/Cipher/init.py", line 79, in _create_cipher
return modes[mode](factory, **kwargs)
File "/home/papei1/.local/lib/python3.8/site-packages/Crypto/Cipher/_mode_ecb.py", line 215, in _create_ecb_cipher
cipher_state = factory._create_base_cipher(kwargs)
File "/home/papei1/.local/lib/python3.8/site-packages/Crypto/Cipher/AES.py", line 103, in _create_base_cipher
result = start_operation(c_uint8_ptr(key),
File "/home/papei1/.local/lib/python3.8/site-packages/Crypto/Util/_raw_api.py", line 238, in c_uint8_ptr
raise TypeError("Object type %s cannot be passed to C code" % type(data))
TypeError: Object type <class 'str'> cannot be passed to C code

@h0rn3t
Copy link
Author

h0rn3t commented Dec 24, 2020

@dimi

I tried to run this but i had this problem:

Message...: hello
Password..: 1234
Traceback (most recent call last):
File "aes-ecb.py", line 43, in
print('Ciphertext:', AESCipher(pwd).encrypt(msg))
File "aes-ecb.py", line 28, in encrypt
cipher = AES.new(self.key, AES.MODE_ECB)
File "/home/papei1/.local/lib/python3.8/site-packages/Crypto/Cipher/AES.py", line 232, in new
return _create_cipher(sys.modules[name], key, mode, *args, **kwargs)
File "/home/papei1/.local/lib/python3.8/site-packages/Crypto/Cipher/init.py", line 79, in _create_cipher
return modes[mode](factory, **kwargs)
File "/home/papei1/.local/lib/python3.8/site-packages/Crypto/Cipher/_mode_ecb.py", line 215, in _create_ecb_cipher
cipher_state = factory._create_base_cipher(kwargs)
File "/home/papei1/.local/lib/python3.8/site-packages/Crypto/Cipher/AES.py", line 103, in _create_base_cipher
result = start_operation(c_uint8_ptr(key),
File "/home/papei1/.local/lib/python3.8/site-packages/Crypto/Util/_raw_api.py", line 238, in c_uint8_ptr
raise TypeError("Object type %s cannot be passed to C code" % type(data))
TypeError: Object type <class 'str'> cannot be passed to C code

fixed

@dimitris1343
Copy link

ok now the output is:

Message...: hello
Password..: 1234
Ciphertext: b'NGTNQiu5fQEO7rfk9VxsNg=='

What about the decryption ?

i expected something like this for the decryption :
Ciphertext: NGTNQiu5fQEO7rfk9VxsNg==
Password..: 1234
Message...: hello

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