Encryption script based on a simplified version of the Advanced Encryption System (AES), implemented using python3.
AES Simplified start by asking the user for the filenames that contain the plaintext to be encrypted and the encryption key. The resulting encrypted text will be outputted and the user will have the option to save the encrypted text to a file.
NOTE:
When executing, ensure the files containing the plaintext and the encryption key are in the same directory.
Plaintext encryption uses various methods implemented by AES such as:
-
Polyalphabetic Substitution via the Encryption Key
A Vigenere table is used to encrypt the plaintext
-
Padding
Further encryption is done on 4x4 character blocks therefore, the character A is used for padding so that the length of the message is divisible by 16
-
Row Shifts
Shifting of the four rows are done in the following fashion: row one is unshifted, row two is shifted one position, row three is shifted two positions, and row four is shifted three positions. All shifts are done to the left
-
Parity Bits
Each character's binary representation will have either an even amount of 1s or an odd amount of 1s. If it is odd then its most significant bit set to 1, if it is even then no changes will occur
-
Mix Columns
This step diffuses the data by transformation. The transformation is performed by multiplying the circulant MDS matrix with each column of the blocks of characters
To execute run the command python3 main.py
get_files()-
Description:
Receives filenames from user
-
Parameters:
None
-
Return:
input_fileString representing the name of the file that contains the plaintext to be encryptedkey_fileString representing the name of the file that contains the encryption keyoutput_fileString representing the name of the file that the cyphertext will be written to
get_text(input_file, key_file)-
Description:
Reads in all contents of the input file and key file
-
Parameters:
input_fileString representing filename containing plaintextkey_fileString representing filename containing encryption key -
Return:
messageList containing all text from the input filekeyString containing encryption key from the key file
parse_text(message)-
Description:
Removes all whitespace and punctuation marks from message leaving only characters A-Z
-
Parameters:
messageList containing plaintext -
Return:
final_messageString containing plaintext with only characters A-Z
output_data(title, data, key_size)-
Description:
Outputs steps in encryption where data is output in 4x4 blocks
-
Parameters:
titleString representing which step is being outputteddataString representing the current data being encryptedkey_sizeInt representing size of encryption key -
Return:
None
Vcipher_encrypt(text, key)-
Description:
Encryption via the Vigenere cipher using polyalphabetic substitution. Encrypts text using an encryption key
-
Parameters:
textString representing text to be encryptedkeyString representing the encryption key -
Return:
encrypted_textString representing text that has been encrypted
padding(text, key_length)-
Description:
Adds padding to the data if necessary, if no padding needed returns original text and groups
-
Parameters:
textString representing ciphertextgroupsList representing ciphertextkey_lengthInt representing length of encryption key -
Return:
updated_textString representing padded ciphertextgroupsList representing padded ciphertext
shift_rows(text, key_length)-
Description:
Shift the rows of each 4x4 block 1st row: no shift, 2nd row: left shift 1 position, 3rd row: left shift 2 positions, 4th row: left shift 3 positions
-
Parameters:
textString representing ciphertextkey_lengthInt representing length of encryption key -
Return:
shifted_textString representing text that has been shifted accordinglyshifted_collections2D list of 4x4 blocks of text that has been shifted accordingly
parity_bit(text, key_length)-
Description:
Adds parity bit to binary ascii value of each char in text Parity bit is needed if there is an odd number of 1s in binary ascii value
-
Parameters:
textString representing padded ciphertextkey_lengthInt representing size of encryption key -
Return:
hex_stringString representing hexadecimal conversions of each char with parity bit added
output_doubles(title, data, key_size)-
Description:
Outputs data in 4x4 block where each element is grouped by two to represent a hexadecimal value
-
Parameters:
titleString representing which step is being outputteddataString representing the current data being encryptedkey_sizeInt representing size of encryption key -
Return:
None
mix_columns(text, key_length)-
Description:
Rearranges 2d list (4x4) and passes one column (size 4) to
rgf()to be transformed After transformation reorder transformed values to their original positions -
Parameters:
textString representing ciphertextkey_lengthInt representing length of encryption key -
Return:
one_strString representing transformed hex values in their original positions
rgf(value, constant)-
Description:
Performs multiplication by using Rijndael Galois field
-
Parameters:
valueList of hex values representing a single column (size 4) in 2d listconstantInt representing what multiplicand -
Return:
finishedList representing transformed hex values
save_data(text)-
Description:
Give users a choice to save data by writing to file specified by user
-
Parameters:
textString representing encrypted text to be saved to file -
Return:
None
output_results(pre, post, key)-
Description:
Outputs the results of encryption
-
Parameters:
preList representing text to be encryptedpostString representing encrypted textkeyString representing encryption key -
Return:
None
Further information regarding AES can be found by watching this video: AES