Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace EnigmaCode
- {
- /**
- * THE ROTOR CLASS
- *
- * Used to store rotor information, such as its cycle size, the number of turns it has made, and the internal alphabet order.
- */
- class Rotor
- {
- /**
- * THE CLASS PROPERTIES
- */
- private List<char> _In = new List<char>();
- private List<char> _Out = new List<char>();
- private int cycle = 0;
- private int turns = 1;
- /**
- * THE ACCESSORS
- */
- public List<char> In
- {
- get { return _In; }
- set { _In = value; }
- }
- public List<char> Out
- {
- get { return _Out; }
- set { _Out = value; }
- }
- /**
- * THE CONSTRUCTOR
- */
- public Rotor(int Rslot)
- {
- if (Rslot == 1)
- {
- cycle = 1;
- } else {
- Rslot -= 1;
- cycle = Convert.ToInt32(Math.Pow(28, Rslot));
- }
- }
- /**
- * THE PUBLIC METHODS
- */
- /**
- * Returns the number of turns. Only meant for debugging.
- */
- public int getT
- {
- get { return turns; }
- }
- /**
- * Returns the size of the rotor's cycle. Only meant for debugging.
- */
- public int getC
- {
- get { return cycle; }
- }
- /**
- * Turn the rotor.
- *
- * If the rotor has made its full cycle, reset the turn count.
- */
- public void Turn()
- {
- if(turns == cycle) {
- int last = _Out.Count() - 1;
- char chr = _Out[last];
- Out.RemoveAt(last);
- Out.Insert(0, chr);
- turns = 1;
- } else {
- turns += 1;
- }
- }
- }
- class Switch
- {
- private char from;
- private char to;
- public char F
- {
- get { return F; }
- }
- public char T
- {
- get { return T; }
- }
- public Switch(char F, char T)
- {
- from = F;
- to = T;
- }
- public char Reverse(char C)
- {
- if (C != to)
- {
- return C;
- }
- return from;
- }
- public char Transform(char C)
- {
- if(C != from)
- {
- return C;
- }
- return to;
- }
- }
- /**
- * THE ENIGMA CLASS.
- *
- * Use this class to encrypt and decrypt phrases and sentences.
- */
- class Enigma
- {
- /**
- * THE CLASS PROPERTIES
- */
- /**
- * Class lists
- */
- private List<Rotor> Rotors = new List<Rotor>();
- private List<Switch> Board = new List<Switch>();
- /**
- * Class constants
- */
- private const string CHARS = "ABCDEFGHIJKLMNOPQRSTUWVXYZÖÄ1234567890";
- private const int TOTALROTORS = 5;
- private const int TOTALSWITCHES = 19;
- /**
- * Class variables
- */
- private bool spaces = false;
- private char whitespace;
- private int Rset = 0;
- /**
- * PRIVATE METHODS
- */
- private char Transform(char chr, bool R = false)
- {
- char output;
- for (int i = 0; i < Board.Count(); i++)
- {
- if (R == true)
- {
- output = Board[i].Reverse(chr);
- } else {
- output = Board[i].Transform(chr);
- }
- if (output != chr)
- {
- return output;
- }
- }
- return chr;
- }
- /**
- * Decrypts one character.
- *
- * Validate each character by checking it against accepted alphabet before encrypting one character.
- */
- private char DecryptChar(char chr)
- {
- char output;
- if(Char.IsWhiteSpace(chr))
- {
- return chr;
- }
- if (!isValid(chr))
- {
- throw new ArgumentException("Invalid character input.");
- }
- output = chr;
- for (int i = Rotors.Count(); i > 0; i--)
- {
- int e = i - 1;
- int alphalen = Rotors[e].In.Count();
- for (int n = 0; n < alphalen; n++)
- {
- if (Rotors[e].Out[n] == output)
- {
- output = Rotors[e].In[n];
- break;
- }
- }
- }
- if ((output == whitespace) && (spaces == true))
- {
- return ' ';
- }
- output = Transform(output, true);
- return output;
- }
- /**
- * Encrypts one character.
- *
- * Validate each character by checking it against accepted alphabet before encrypting one character.
- */
- private char EncryptChar(char chr)
- {
- char output = Transform(chr);
- if (Char.IsWhiteSpace(chr))
- {
- if (spaces == true)
- {
- output = whitespace;
- } else {
- return ' ';
- }
- }
- if (!isValid(output))
- {
- throw new ArgumentException("Invalid character input.");
- }
- for (int i = 0; i < Rotors.Count(); i++)
- {
- int alphalen = Rotors[i].In.Count();
- for (int n = 0; n < alphalen; n++)
- {
- if (Rotors[i].In[n] == output)
- {
- output = Rotors[i].Out[n];
- break;
- }
- }
- }
- return output;
- }
- /**
- * Validates characters.
- *
- * Check the chr (=Character) against the list of valid characters.
- * Return FALSE if chr is not a valid character.
- */
- private bool isValid(char chr)
- {
- bool valid = false;
- List<char> alphabet = CHARS.ToList();
- for (int i = 0; i < alphabet.Count(); i++)
- {
- if (alphabet[i] == chr)
- {
- valid = true;
- }
- }
- return valid;
- }
- /**
- * Setup a rotor.
- *
- * Parses the rotor settings (name and initial position) from the passes parameter R (=Rotor). The information is validated
- * and then the correct rotor is pulled and stored.
- */
- private void SetupRotor(double R)
- {
- /* Separate the rotor data into number and position */
- var data = R.ToString().Split(',');
- int Rnumber = Convert.ToInt32(data[0]);
- int Rpos = 0; int Rcount = 0;
- /* Make sure rotor exists. */
- if(Rnumber > TOTALROTORS)
- {
- throw new ArgumentException("Invalid rotor number.");
- }
- /* Pull and store the rotor alphabet. */
- switch (Rnumber)
- {
- case 1:
- Rotors.Add(new Rotor(1));
- Rotors[Rset].In = new RotorSetI().In;
- Rotors[Rset].Out = new RotorSetI().Out;
- Rset += 1;
- break;
- case 2:
- Rotors.Add(new Rotor(2));
- Rotors[Rset].In = new RotorSetII().In;
- Rotors[Rset].Out = new RotorSetII().Out;
- Rset += 1;
- break;
- case 3:
- Rotors.Add(new Rotor(3));
- Rotors[Rset].In = new RotorSetIII().In;
- Rotors[Rset].Out = new RotorSetIII().Out;
- Rset += 1;
- break;
- }
- /* Turn the rotors to setup the initial settings. */
- if (data.Count() > 1)
- {
- Rpos = Convert.ToInt32(data[1]);
- while (Rcount < Rpos)
- {
- TurnRotors();
- Rcount++;
- }
- }
- }
- /**
- * Turn the rotors.
- *
- * Loop through each rotor and call the Turn() method for the rotor object.
- */
- private void TurnRotors()
- {
- for(int i=0; i<Rotors.Count(); i++)
- {
- Rotors[i].Turn();
- }
- }
- /**
- * CONSTRUCTORS
- */
- public Enigma(double RI)
- {
- SetupRotor(RI);
- }
- public Enigma(double RI, double RII)
- {
- SetupRotor(RI);
- SetupRotor(RII);
- }
- public Enigma(double RI, double RII, double RIII)
- {
- SetupRotor(RI);
- SetupRotor(RII);
- SetupRotor(RIII);
- }
- /**
- * PUBLIC METHODS
- */
- /**
- * Adds a character switch.
- *
- * Character pairs are added, which will be switched during the encryption process.
- */
- public void AddSwitch(char F, char T)
- {
- if (Board.Count() < TOTALSWITCHES)
- {
- Switch Switch = new Switch(F, T);
- Board.Add(Switch);
- } else {
- throw new ArgumentException("Too many switches.");
- }
- }
- /**
- * Begins the decryption process.
- *
- * To avoid repeating identical code, Decrypt() only serves as a front to Encrypt().
- */
- public string Decrypt(string P)
- {
- string output = "";
- output = Encrypt(P, true);
- return output;
- }
- /**
- * Begins the encryption process.
- *
- * Breakdown the P (=Phrase) into a list of characters; encrypt one character (by calling EncryptChar()) at a time and
- * turn rotors after each character.
- */
- public string Encrypt(string P, bool D = false)
- {
- List<char> phrase = P.ToList();
- string output = "";
- for(int i=0; i<phrase.Count(); i++)
- {
- if (D == true)
- {
- output += DecryptChar(phrase[i]);
- } else {
- output += EncryptChar(phrase[i]);
- }
- TurnRotors();
- }
- return output;
- }
- public bool IsWsEnabled()
- {
- return spaces;
- }
- public void DisableWhiteSpaces()
- {
- spaces = false;
- }
- public void SetWhiteSpace(char W)
- {
- if(!isValid(W))
- {
- throw new ArgumentException("Invalid character input.");
- }
- whitespace = W;
- spaces = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement