Skip to content
This repository was archived by the owner on May 25, 2022. It is now read-only.

Commit 3acc28e

Browse files
authored
Merge pull request #71 from Xlgd/master
#58 fixed encode and decode text using key
2 parents 29ae97f + 3382a39 commit 3acc28e

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

projects/Get meta information of images/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
### usage
44

5-
python get_meta_from_pic image_name
5+
python get_meta_from_pic.py image_file
66

77
### note
88

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## create script to encode and decode text
2+
3+
### usage
4+
5+
python aes_encode.py file.txt
6+
7+
An encrypted file("encrypted.bin") will be generated after the program is run
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from Cryptodome.Cipher import AES
2+
from Cryptodome import Random
3+
from binascii import b2a_hex
4+
import sys
5+
6+
# get the plaintext
7+
f = open(sys.argv[1])
8+
plain_text = f.read()
9+
10+
# The key length must be 16 (AES-128), 24 (AES-192), or 32 (AES-256) Bytes.
11+
key = b'this is a 16 key'
12+
13+
# Generate a non-repeatable key vector with a length equal to the size of the AES block
14+
iv = Random.new().read(AES.block_size)
15+
16+
# Use key and iv to initialize AES object, use MODE_CFB mode
17+
mycipher = AES.new(key, AES.MODE_CFB, iv)
18+
19+
# Add iv (key vector) to the beginning of the encrypted ciphertext and transmit it together
20+
ciphertext = iv + mycipher.encrypt(plain_text.encode())
21+
22+
23+
# To decrypt, use key and iv to generate a new AES object
24+
mydecrypt = AES.new(key, AES.MODE_CFB, ciphertext[:16])
25+
26+
# Use the newly generated AES object to decrypt the encrypted ciphertext
27+
decrypttext = mydecrypt.decrypt(ciphertext[16:])
28+
29+
# output
30+
file_out = open("encrypted.bin", "wb")
31+
file_out.write(ciphertext[16:])
32+
file_out.close()
33+
34+
print("The key k is: ", key)
35+
print("iv is: ", b2a_hex(ciphertext)[:16])
36+
print("The encrypted data is: ", b2a_hex(ciphertext)[16:])
37+
print("The decrypted data is: ", decrypttext.decode())
38+
39+
f.close()

0 commit comments

Comments
 (0)