Skip to content

Instantly share code, notes, and snippets.

@elprup
Created April 12, 2016 03:40
Show Gist options
  • Save elprup/520a9de6efe969b421b366901bb5e088 to your computer and use it in GitHub Desktop.
Save elprup/520a9de6efe969b421b366901bb5e088 to your computer and use it in GitHub Desktop.
compare for pycrypto aes and pyaes aes
import os
from Crypto.Cipher import AES
import pyaes
import base64
def _create_secret_key(size):
randlist = map(lambda xx: (hex(ord(xx))[2:]), os.urandom(size))
return (''.join(randlist))[0:16]
def _aes_encrypt2(text, sec_key):
pad = 16 - len(text) % 16
text = text + pad * chr(pad)
aes = pyaes.AESModeOfOperationCBC(sec_key, iv='0102030405060708')
ciphertext = ''
while text != '':
ciphertext += aes.encrypt(text[:16])
text = text[16:]
ciphertext = base64.b64encode(ciphertext)
return ciphertext
def _aes_encrypt(text, sec_key):
pad = 16 - len(text) % 16
text = text + pad * chr(pad)
encryptor = AES.new(sec_key, 2, '0102030405060708')
ciphertext = encryptor.encrypt(text)
ciphertext = base64.b64encode(ciphertext)
return ciphertext
if __name__ == '__main__':
sk = _create_secret_key(16)
text = 'hello,dasfdasfsdafasdfsdafdsaf'
print _aes_encrypt(text, sk)
print _aes_encrypt2(text, sk)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment