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