· 7 years ago · Mar 08, 2018, 10:42 PM
1# Ryan Finley
2# 3/8/2018
3# AesEncryption.py
4
5import os
6import binascii
7import hashlib
8from Crypto import Random
9from Crypto.Cipher import AES
10
11# Sets the constant for block's size to 16
12BLOCK_SIZE = 16
13
14# Sets the current directory to 'src' so it can run on all systems
15os.chdir(os.path.dirname(os.path.realpath(__file__)))
16
17# This is used when encoding plaintext when about to encrypt with AES
18def pad(size):
19 return size + (BLOCK_SIZE - len(size) % BLOCK_SIZE) * chr(BLOCK_SIZE - len(size) % BLOCK_SIZE)
20
21# Used to decode the ciphertext after it has been decoded with the secret key
22def reverse_pad(size):
23 return size[:-ord(size[len(size) - 1:])]
24
25# This is used to get text when it is called from any of the Get Functions below
26def get_text_from_file(filename):
27 my_file = open(filename)
28 file_text = my_file.read().replace('\n', '')
29 my_file.close()
30 return file_text
31
32# ----------------------------------------------------------------------------
33# All "Get" functions obtain the file's text drom the data folder when called
34def get_secret_key():
35 return get_text_from_file('../data/key.txt')
36
37def get_plaintext():
38 return get_text_from_file('../data/plaintext.txt')
39
40def get_ciphertext():
41 return get_text_from_file('../data/ciphertext.txt')
42
43def get_randomized_iv():
44 hex_str = get_text_from_file('../data/iv.txt')
45 return binascii.unhexlify(hex_str)
46# ----------------------------------------------------------------------------
47
48# Creates and returns a randomized IV
49def create_randomized_iv():
50 iv = Random.new().read(BLOCK_SIZE)
51 my_file = open('../data/iv.txt', 'w')
52 my_file.write(iv.hex())
53 my_file.close()
54 return iv
55
56# Creates and returns a randomized secret key
57def generate_key():
58 secret_key = hashlib.sha256(os.urandom(16)).digest()
59 my_file = open('../data/key.txt', 'w')
60 my_file.write(secret_key.hex())
61 my_file.close()
62 return secret_key
63
64# --- Encryption Function ---
65# Inputs: secret key, plaintext message
66# Outputs: ciphertext from provided secret key and message
67def encryption(key, plaintext, mode):
68 iv = create_randomized_iv()
69 ciphertext = AES.new(key, mode, iv).encrypt(pad(plaintext)).hex()
70 my_file = open('../data/ciphertext.txt', 'w')
71 my_file.write(ciphertext)
72 my_file.close()
73 return ciphertext
74
75# Encrypts plaintext using ECB with no writing to any files
76def enc_for_ecb(key, plaintext):
77 return encryption(key, plaintext, AES.MODE_ECB)
78
79# Encrypts plaintext using CBC with no writing to any files
80def enc_for_cbc(key, plaintext):
81 return encryption(key, plaintext, AES.MODE_CBC)
82
83# --- Decryption Function ---
84# Inputs: secret key, ciphertext, IV, and a mode type (CBC or ECB)
85# Outputs: decrypted ciphertext and stores value in appropriate file
86def decryption(key, ciphertext, iv, mode):
87 temp_cipher = AES.new(key, mode, iv)
88 plaintext_dec = reverse_pad(temp_cipher.decrypt(binascii.unhexlify(ciphertext))).decode('utf-8')
89 my_file = open('../data/result.txt', 'w')
90 my_file.write(plaintext_dec)
91 my_file.close()
92 return plaintext_dec
93
94def compare_aes_enc_modes():
95 for i in range(0, 3):
96 secret_key = generate_key()
97 plaintext = get_plaintext()
98 ecb_enc = enc_for_ecb(secret_key, plaintext)
99 cdc_enc = enc_for_ecb(secret_key, plaintext)
100 print('Encrptyion Rounnd ' + str(i+1))
101 print('Enc with ECB: ' + ecb_enc)
102 print('Enc with CBC: ' + cbc_enc)
103 print('\n')
104
105sk = generate_key()
106
107# ---------------------- Displayed output begins here ----------------------
108# EBC
109print('\n')
110ecb_enc = encryption(sk, get_plaintext(), AES.MODE_ECB)
111ecb_dec = decryption(sk, ecb_enc, get_randomized_iv(), AES.MODE_ECB)
112print('Encrypted with ECB: ' + ecb_enc)
113print('Decrypted with ECB: ' + ecb_dec)
114
115# CBC
116print('\n')
117cbc_enc = encryption(sk, get_plaintext(), AES.MODE_CBC)
118cbc_dec = decryption(sk, cbc_enc, get_randomized_iv(), AES.MODE_CBC)
119print('Enc with CBC: ' + cbc_enc)
120print('Decrypt with CBC: ' + cbc_dec)
121
122print('\n')
123compare_aes_enc_modes()
124print('\n')