add encryption function
This commit is contained in:
parent
e6e7151ba2
commit
ae00a5783c
48
index.py
48
index.py
|
@ -1,26 +1,50 @@
|
|||
import argparse
|
||||
from pathlib import Path
|
||||
|
||||
def encrypt(file):
|
||||
sal = 'hello encryption'
|
||||
return sal
|
||||
from Cryptodome.Cipher import AES
|
||||
from Cryptodome.Random import get_random_bytes
|
||||
|
||||
|
||||
def decrypt(file):
|
||||
greet = 'hello decryption'
|
||||
def encrypt(text, key):
|
||||
# make encryptor
|
||||
encryptor = AES.new(key, AES.MODE_CFB)
|
||||
|
||||
# takes the data and returns ciphertext
|
||||
cipher_text = encryptor.encrypt(text)
|
||||
|
||||
# now to write the ciphertext to the file
|
||||
encrypted_file = open("encrypted_file.txt", "wb")
|
||||
encrypted_file.write(cipher_text)
|
||||
encrypted_file.close()
|
||||
|
||||
# return the cipher_text to show users
|
||||
return cipher_text
|
||||
|
||||
|
||||
def decrypt():
|
||||
greet = '\nhello decryption'
|
||||
return greet
|
||||
|
||||
|
||||
def main():
|
||||
# get arguments from command line
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('-f', '--file', type=open)
|
||||
parser.add_argument('modality', choices=['encrypt', 'decrypt'])
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.modality == 'encrypt':
|
||||
print(encrypt(args.file))
|
||||
elif args.modality == 'decrypt':
|
||||
print(decrypt(args.file))
|
||||
# make random 32B long key
|
||||
key = get_random_bytes(32)
|
||||
|
||||
# read the text file in and make it a string with no line breaks
|
||||
text = args.file.read().replace('\n', '')
|
||||
|
||||
# encrypt the file
|
||||
encrypted_text = encrypt(str.encode(text), key)
|
||||
|
||||
# print encrypted text
|
||||
print("Encrypted file created. Contents:")
|
||||
print(encrypted_text)
|
||||
|
||||
print(decrypt())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
Loading…
Reference in New Issue