Skip to content

Instantly share code, notes, and snippets.

@mckelvin
Created December 8, 2017 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mckelvin/d21f76918207148a98c4e147bc68e91d to your computer and use it in GitHub Desktop.
Save mckelvin/d21f76918207148a98c4e147bc68e91d to your computer and use it in GitHub Desktop.
# coding: utf-8
import md5
from Crypto import Random
from Crypto.Cipher import AES
import base64
BLOCK_SIZE = 16
def trans(key):
return md5.new(key).digest()
def encrypt(message, passphrase):
passphrase = trans(passphrase)
IV = Random.new().read(BLOCK_SIZE)
aes = AES.new(passphrase, AES.MODE_CFB, IV)
return base64.b64encode(IV + aes.encrypt(message))
def decrypt(encrypted, passphrase):
passphrase = trans(passphrase)
encrypted = base64.b64decode(encrypted)
IV = encrypted[:BLOCK_SIZE]
aes = AES.new(passphrase, AES.MODE_CFB, IV)
return aes.decrypt(encrypted[BLOCK_SIZE:])
def main():
passphrase = '123'
message = 'TheXX'
encrypted = encrypt(message, passphrase)
decrypted = decrypt(encrypted, passphrase)
assert decrypted == message
print("passphrase: %s" % passphrase)
print("message: %s" % message)
print("encrypted: %s" % encrypted)
print("decrypted: %s" % decrypted)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment