Created
December 8, 2017 12:59
-
-
Save mckelvin/d21f76918207148a98c4e147bc68e91d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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