Skip to content
This repository was archived by the owner on May 25, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions projects/create a script to encrypt files and folder/README.md
Original file line number Diff line number Diff line change
@@ -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
39 changes: 39 additions & 0 deletions projects/create a script to encrypt files and folder/encrypt.py
Original file line number Diff line number Diff line change
@@ -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)")
5 changes: 4 additions & 1 deletion projects/create script to encode and decode text/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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()