Skip to content

Instantly share code, notes, and snippets.

@fmeyer
Last active January 22, 2019 17:14
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 fmeyer/de2702ba56e6283e214c07ac9a6d730d to your computer and use it in GitHub Desktop.
Save fmeyer/de2702ba56e6283e214c07ac9a6d730d to your computer and use it in GitHub Desktop.
# from cryptography
# key = None
# iv = None
#
# cipher = None
# encryptor = None
# ciphertext = None
# decryptor = None
# plaintext = None
from Crypto import Random
from Crypto.Cipher import AES
from base64 import b64decode
from base64 import b64encode
def decrypt(cipher, ciphertext):
enc = b64decode(ciphertext)
iv = enc[:16]
decryptor = AES.new(cipher, AES.MODE_CBC, iv)
result = decryptor.decrypt(enc[16:]).decode('utf8')
return result
def encrypt(cipher, plaintext):
iv = Random.new().read(AES.block_size)
encryptor = AES.new(cipher, AES.MODE_CBC, iv)
ciphertext = b64encode(iv + encryptor.encrypt(plaintext))
return ciphertext
def main():
cipher = 'batata0000000000'
plaintext = 'secret0000000000'
ciphertext = encrypt(cipher, plaintext)
print(ciphertext)
print(decrypt(cipher, ciphertext))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment