diff --git a/projects/create a script to encrypt files and folder/README.md b/projects/create a script to encrypt files and folder/README.md new file mode 100644 index 00000000..e745d28a --- /dev/null +++ b/projects/create a script to encrypt files and folder/README.md @@ -0,0 +1,12 @@ +## create script to encrypt files and folder + +### usage + +python encrypt.py path(file or folder) + +examples: + python encrypt.py test.txt(file) + or + python eccrypt.py ./testdir(folder) + +encrypted files("original_file_name.bin") will be generated in original location after the program running \ No newline at end of file diff --git a/projects/create a script to encrypt files and folder/encrypt.py b/projects/create a script to encrypt files and folder/encrypt.py new file mode 100644 index 00000000..ac109716 --- /dev/null +++ b/projects/create a script to encrypt files and folder/encrypt.py @@ -0,0 +1,39 @@ +import sys +import os +from Cryptodome.Cipher import AES +from Cryptodome import Random +from binascii import b2a_hex + +def encrypt_dir(path): + for root, _, files in os.walk("."): + for file in files: + file_path = os.path.join(root, file) + print(file_path + " is encrypting.") + encrypt_file(file_path) + + +def encrypt_file(path): + # get the plaintext + f = open(path) + 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' + + iv = Random.new().read(AES.block_size) + mycipher = AES.new(key, AES.MODE_CFB, iv) + ciphertext = iv + mycipher.encrypt(plain_text.encode()) + + # output + file_out = open(path + ".bin", "wb") + file_out.write(ciphertext[16:]) + file_out.close() + + +path = sys.argv[1] +if os.path.isdir(path) and os.path.exists(path): + encrypt_dir(path) +elif os.path.isfile(path) and os.path.exists(path): + encrypt_file(path) +else: + print("it's a special file(socket,FIFO,device file)") diff --git a/projects/create script to encode and decode text/README.md b/projects/create script to encode and decode text/README.md index 7c97da1c..cd492532 100644 --- a/projects/create script to encode and decode text/README.md +++ b/projects/create script to encode and decode text/README.md @@ -2,6 +2,9 @@ ### usage -python aes_encode.py file.txt +python aes_encode.py "a text" + +example: + python aes_encode.py "hello world" 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 index 94f84759..34dc077f 100644 --- a/projects/create script to encode and decode text/aes_encode.py +++ b/projects/create script to encode and decode text/aes_encode.py @@ -4,8 +4,7 @@ import sys # get the plaintext -f = open(sys.argv[1]) -plain_text = f.read() +plain_text = sys.argv[1] # The key length must be 16 (AES-128), 24 (AES-192), or 32 (AES-256) Bytes. key = b'this is a 16 key' @@ -35,5 +34,3 @@ 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()