Last active
January 22, 2019 17:14
-
-
Save fmeyer/de2702ba56e6283e214c07ac9a6d730d 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
# 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