Created
October 12, 2021 12:14
-
-
Save elmoallistair/cebcde311d5df515cef387cf92ca0087 to your computer and use it in GitHub Desktop.
advanced-encryption-standard
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 Cryptodome.Cipher import AES | |
import argparse | |
import traceback | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-f", dest="FILE_PATH", required=True, help="Encrypted file path") | |
parser.add_argument("-k", dest="KEY_PATH", required=True, help="Key file path") | |
parser.parse_args() | |
args = parser.parse_args() | |
key = open(args.k, "rb").read() | |
data = open(args.f, "rb") | |
nonce, tag, ciphertext = [ data.read(x) for x in (16, 16, -1) ] | |
print("Decrypting message...") | |
try: | |
cipher = AES.new(key, AES.MODE_EAX, nonce) | |
data = cipher.decrypt_and_verify(ciphertext, tag) | |
print("Message: ", repr(data.decode('UTF-8'))) | |
except Exception: | |
print("Incorrect decryption: ") | |
traceback.print_exc() |
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 Cryptodome.Cipher import AES | |
from Cryptodome.Random import get_random_bytes | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-i", required=True, help="Input file path") | |
parser.add_argument("-o", default="encrypted-file.bin", | |
help="Path to save encryption file (Default: encrypted-file.bin") | |
parser.add_argument("-k", default="encrypted-key.bin", | |
help="Path to save generated key (Default: encrypted-key.bin") | |
parser.parse_args() | |
args = parser.parse_args() | |
print(args) | |
file_in = args.i | |
file_out = args.o | |
key_out = args.i | |
data = open(file_in, "rb").read() | |
key = get_random_bytes(16) | |
cipher = AES.new(key, AES.MODE_EAX) | |
ciphertext, tag = cipher.encrypt_and_digest(data) | |
with open(file_out, "wb") as fout: | |
for x in (cipher.nonce, tag, ciphertext): | |
fout.write(x) | |
print(f"Ciphertext written to '{file_out}'") | |
with open(key_out, "wb") as writer: | |
writer.write(key) | |
print(f"Key written to '{key_out}'") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment