· 7 years ago · Mar 08, 2018, 10:26 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 and CBC and stores each in a separate varaible
76# They are then returned as a tuple
77def enc_all(key, plaintext):
78 ecb_enc = encryption(key, plaintext, AES.MODE_ECB)
79 cbc_enc = encryption(key, plaintext, AES.MODE_CBC)
80 return (ecb_enc, cbc_enc)
81
82# --- Decryption Function ---
83# Inputs: secret key, ciphertext, IV, and a mode type (CBC or ECB)
84# Outputs: decrypted ciphertext and stores value in appropriate file
85def decryption(key, ciphertext, iv, mode):
86 temp_cipher = AES.new(key, mode, iv)
87 plaintext_dec = reverse_pad(temp_cipher.decrypt(binascii.unhexlify(ciphertext))).decode('utf-8')
88 my_file = open('../data/result.txt', 'w')
89 my_file.write(plaintext_dec)
90 my_file.close()
91 return plaintext_dec
92
93# Decrypts the 2 ciphertexts it recieves in the form of a tuple and returns them
94def decrypt_all(key, ciphertexts, iv):
95 (ecb, cbc) = ciphertexts
96 ecb_dec = decryption(key, ciphertext, iv, AES.MODE_ECB)
97 cbc_dec = decryption(key, ciphertext, iv, AES.MODE_CBC)
98 return (ecb_dec, cbc_dec)
99
100
101def compare_aes_enc_modes():
102 for i in range(0, 3):
103 sk = generate_key()
104 plaintext = get_plaintext()
105 (ecb_enc, cbc_enc) = enc_all(sk, plaintext)
106 print('Encrptyion Rounnd ' + str(i+1))
107 print('Enc with ECB: ' + ecb_enc)
108 print('Enc with CBC: ' + cbc_enc)
109 print('\n')
110
111sk = generate_key()
112
113# ---------------------- Displayed output begins here ----------------------
114# EBC
115print('\n')
116ecb_enc = encryption(sk, get_plaintext(), AES.MODE_ECB)
117ecb_dec = decryption(sk, ecb_enc, get_randomized_iv(), AES.MODE_ECB)
118print('Encrypted with ECB: ' + ecb_enc)
119print('Decrypted with ECB: ' + ecb_dec)
120
121# CBC
122print('\n')
123cbc_enc = encryption(sk, get_plaintext(), AES.MODE_CBC)
124cbc_dec = decryption(sk, cbc_enc, get_randomized_iv(), AES.MODE_CBC)
125print('Enc with CBC: ' + cbc_enc)
126print('Decrypt with CBC: ' + cbc_dec)
127
128print('\n')
129compare_aes_enc_modes()
130print('\n')