diff --git a/projects/Get meta information of images/README.md b/projects/Get meta information of images/README.md index a8751e93..2ee65c01 100644 --- a/projects/Get meta information of images/README.md +++ b/projects/Get meta information of images/README.md @@ -2,7 +2,7 @@ ### usage -python get_meta_from_pic image_name +python get_meta_from_pic.py image_file ### note diff --git a/projects/create script to encode and decode text/README.md b/projects/create script to encode and decode text/README.md new file mode 100644 index 00000000..7c97da1c --- /dev/null +++ b/projects/create script to encode and decode text/README.md @@ -0,0 +1,7 @@ +## create script to encode and decode text + +### usage + +python aes_encode.py file.txt + +An encrypted file("encrypted.bin") will be generated after the program is run \ No newline at end of file diff --git a/projects/create script to encode and decode text/aes_encode.py b/projects/create script to encode and decode text/aes_encode.py new file mode 100644 index 00000000..94f84759 --- /dev/null +++ b/projects/create script to encode and decode text/aes_encode.py @@ -0,0 +1,39 @@ +from Cryptodome.Cipher import AES +from Cryptodome import Random +from binascii import b2a_hex +import sys + +# get the plaintext +f = open(sys.argv[1]) +plain_text = f.read() + +# The key length must be 16 (AES-128), 24 (AES-192), or 32 (AES-256) Bytes. +key = b'this is a 16 key' + +# Generate a non-repeatable key vector with a length equal to the size of the AES block +iv = Random.new().read(AES.block_size) + +# Use key and iv to initialize AES object, use MODE_CFB mode +mycipher = AES.new(key, AES.MODE_CFB, iv) + +# Add iv (key vector) to the beginning of the encrypted ciphertext and transmit it together +ciphertext = iv + mycipher.encrypt(plain_text.encode()) + + +# To decrypt, use key and iv to generate a new AES object +mydecrypt = AES.new(key, AES.MODE_CFB, ciphertext[:16]) + +# Use the newly generated AES object to decrypt the encrypted ciphertext +decrypttext = mydecrypt.decrypt(ciphertext[16:]) + +# output +file_out = open("encrypted.bin", "wb") +file_out.write(ciphertext[16:]) +file_out.close() + +print("The key k is: ", key) +print("iv is: ", b2a_hex(ciphertext)[:16]) +print("The encrypted data is: ", b2a_hex(ciphertext)[16:]) +print("The decrypted data is: ", decrypttext.decode()) + +f.close()