Enigma in Python

Implementation of Enigma in Python

To implement Enigma within python, we have put together a tutorial to talk you though how to code an Enigma function: This class is to implement the rotors and permutations (plugboard and reflector) used by Enigma. Methods: • __init__ - This method takes a single parameter permutation (an array integers that stores the relative changes of different characters under the permutation). It instantiates an object of the class and stores permutation as an attribute. • permute - This method performs the permutation of the rotor on a given character (given as an integer) with the given rotor position which is also given as an integer. With the parameters letters are given as integers with 'A' as 0, 'B' as 1, 'C' as 2, etc. • inverse - This method creates a rotor object that is the inverse of this one. • __str__ - This method returns a string that is the alphabet 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' under the permutation of this rotor in position 0. This function turns a string representing the result of a permutation on the alphabet into that permutation for use in constructing rotors. It does this by checking that the lowercase string is 26 characters long and contains letter. It then constructs an array of the relative ordinal values of each letter before and after the permutation. Parameters: • in_str - The string representing the permutation to be created. The returned value is an array of integers representing the permutation. This function takes a string containing pairs of characters to be swapped by a plugboard and create a plugboard from this. Parameters: • in_str - This string contains space separated pairs of letters to be swapped by the plugboard being created. The returned value is a rotor object that is the plugboard created. Other functions from this module used: • StrToPerm This function creates an identity rotor object (it creates a rotor that does not change any letter). This function increments a rotor positions array. Parameters: • pos - An array of integers that is the rotor position array to be incremented. The returned value is the incremented array. This function creates an array of rotor objects in the order they need to be performed for enigma encryption. Parameters: • rotors - The array of rotors to be used. • reflector - A rotor object that is the reflector to be used. • plugboard - A rotor object that is the plugboard to be used. The returned value is an array of rotor objects in the order they need to be performed. This function creates an array of rotor objects in the order they need to be performed for enigma decryption. The only difference with getRotorArr is that this function uses the inverse of the reflector. Parameters: • rotors - The array of rotors to be used. • reflector - A rotor object that is the reflector to be used. • plugboard - A rotor object that is the plugboard to be used. The returned value is an array of rotor objects in the order they need to be performed. This function creates an array of rotor positions to use in enigma encryption/decryption. Parameters: • pos - An array of integers that is the rotor positions to use. The returned value is the array of rotor positions created. This function runs a given character through an array of rotors using the given rotor positions. Parameters: • char_num - An integer that is a 0-25 encoding of a letter where 'A' is 1, 'B' is 2, etc. • rotors - An array of rotor objects that are the rotors to use to encrypt/decrypt the given character. • pos - The array of integers to be used as rotor positions. The returned value is the encrypted/decrypted letter. This function performs enigma encryption of a given string using given rotors, reflector, plugboard and starting positions. It does this by iterating through the string and if the character is a letter it encrypts it and otherwise it is ignored. Parameters: • text - The string to be encrypted. • rotors - The array of rotors to use. It has a default value of an array containing the identity rotor. • pos - An array of integers that are the starting positions to be used. It has a default value of an empty array (the function fills this in with zeros). • reflector - A rotor object that is the reflector to be used. It has a default value of the identity rotor (Note: when this is the identity rotor the text will not be altered by encryption). • plugboard- A rotor object that is the plugboard to be used. It has a default value of the identity rotor. The returned value is a string that is the encrypted text. Other functions from this module used: • getRotorArr • getPosArr • inc_pos • EncDecChar This function performs enigma decryption of a given string using given rotors, reflector, plugboard and starting positions. It does this by iterating through the string and if the character is a letter it decrypts it and otherwise it is ignored. The only difference between this and Enc is that it uses getInvRotorArr instead of getRotorArr. Parameters: • text - The string to be decrypted. • rotors - The array of rotors to use. It has a default value of an array containing the identity rotor. • pos - An array of integers that are the starting positions to be used. It has a default value of an empty array (the function fills this in with zeros). • reflector - A rotor object that is the reflector to be used. It has a default value of the identity rotor (Note: when this is the identity rotor the text will not be altered by decryption). • plugboard- A rotor object that is the plugboard to be used. It has a default value of the identity rotor. The returned value is a string that is the decrypted text. Other functions from this module used: • getInvRotorArr • getPosArr • inc_pos • EncDecChar This function performs Enigma M3 encryption/decryption. Parameters: • text - The string that is to be encrypted/decrypted. • rotors - An array of integers that are the rotor numbers to be used. It has a default value of an empty array. If it's length is less than 3 or any of the integers are invalid then rotor I is used. • pos - An array of integers that is the starting positions to use. It has a default value of an empty array. • ring_setting - An array of integers that are the ring settings to use. It has a default value of an empty array. • reflector - An integer referring to which reflector to use. It has a default value of zero. A value of 0 means using UKW-B and a value of 1 means using UKW-C. • plugboard - A string of space separated letter pairs that indicates the plugboard to be used. It has a default value of an empty string. The returned value is the encrypted/decrypted string. Other functions from this module used: • createPlugboard • Enc Written by Abby