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
2 changes: 1 addition & 1 deletion projects/Get meta information of images/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### usage

python get_meta_from_pic image_name
python get_meta_from_pic.py image_file

### note

Expand Down
7 changes: 7 additions & 0 deletions projects/create script to encode and decode text/README.md
Original file line number Diff line number Diff line change
@@ -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
39 changes: 39 additions & 0 deletions projects/create script to encode and decode text/aes_encode.py
Original file line number Diff line number Diff line change
@@ -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()