public static void Main()
string reference = "abcdefghijklmnopqrstuvwxyz";
string rotor1 = "abcdefghijklmnopqrstuvwxyz";
string rotor1b = "wkdtxqbvngmzajcsyrhefupilo";
string rotor2 = "abcdefghijklmnopqrstuvwxyz";
string rotor2b = "efavpthknudlryobscgjxiqwzm";
string rotor3 = "abcdefghijklmnopqrstuvwxyz";
string rotor3b = "zmcwgsvbhtpyilajxfkdqruoen";
int[] turnAround = {23, 19, 14, 13, 1, 7, 12, 8, 3, 5, 16, 11, 0, 22, 4, 6, 10, 17, 24, 21, 9, 18, 20, 15, 2, 25};
char[] message = new char[100];
string[] key = {"a","b","c"};
Console.WriteLine($"Kod: {key[0]}{key[1]}{key[2]}\n\n");
while(rotor1[0] != Char.Parse(key[0]))
rotateSetup(ref rotor1, ref rotor1b);
while(rotor2[0] != Char.Parse(key[1]))
rotateSetup(ref rotor2, ref rotor2b);
while(rotor3[0] != Char.Parse(key[2]))
rotateSetup(ref rotor3, ref rotor3b);
write(rotor1, rotor1b, 1);
write(rotor2, rotor2b, 2);
write(rotor3, rotor3b, 3);
read(ref message, out userInput);
Console.WriteLine(cipher(userInput, message, reference, ref rotor1, ref rotor1b, ref rotations1, ref rotor2, ref rotor2b, ref rotations2, ref rotor3, ref rotor3b, ref rotations3, turnAround));
static void rotateSetup(ref string rotor1, ref string rotor1b)
char rotorFirst = rotor1[0];
for(int i = 0; i < rotor1.Length-1; i++)
rotor1.Replace(rotor1[i], rotor1[i+1]);
Console.WriteLine("Success");
rotor1.Replace(rotor1[rotor1.Length-1], rotorFirst);
char rotorbFirst = rotor1b[0];
for(int i = 0; i < rotor1b.Length-1; i++)
rotor1.Replace(rotor1b[i], rotor1b[i+1]);
rotor1.Replace(rotor1[rotor1.Length-1], rotorFirst);
static void rotate1(ref string rotor1, ref string rotor1b, ref int rotations1, ref string rotor2, ref string rotor2b, ref int rotations2, ref string rotor3, ref string rotor3b, ref int rotations3)
char rotorFirst = rotor1[0];
for(int i = 0; i < rotor1.Length-1; i++)
rotor1.Replace(rotor1[i], rotor1[i+1]);
rotor1.Replace(rotor1[rotor1.Length-1], rotorFirst);
char rotorbFirst = rotor1b[0];
for(int i = 0; i < rotor1b.Length-1; i++)
rotor1.Replace(rotor1b[i], rotor1b[i+1]);
rotor1.Replace(rotor1[rotor1.Length-1], rotorFirst);
if(rotations1 == rotor1.Length)
rotate2(ref rotor2, ref rotor2b, ref rotations2, ref rotor3, ref rotor3b, ref rotations3);
static void rotate2(ref string rotor2, ref string rotor2b, ref int rotations2, ref string rotor3, ref string rotor3b, ref int rotations3)
char rotorFirst = rotor2[0];
for(int i = 0; i < rotor2.Length-1; i++)
rotor2.Replace(rotor2[i], rotor2[i+1]);
rotor2.Replace(rotor2[rotor2.Length-1], rotorFirst);
char rotorbFirst = rotor2b[0];
for(int i = 0; i < rotor2b.Length-1; i++)
rotor2.Replace(rotor2b[i], rotor2b[i+1]);
rotor2.Replace(rotor2[rotor2.Length-1], rotorFirst);
if(rotations2 == rotor2.Length)
rotate3(ref rotor3, ref rotor3b, ref rotations3);
static void rotate3(ref string rotor1, ref string rotor1b, ref int rotations)
char rotorFirst = rotor1[0];
for(int i = 0; i < rotor1.Length-1; i++)
rotor1.Replace(rotor1[i], rotor1[i+1]);
rotor1.Replace(rotor1[rotor1.Length-1], rotorFirst);
char rotorbFirst = rotor1b[0];
for(int i = 0; i < rotor1b.Length-1; i++)
rotor1.Replace(rotor1b[i], rotor1b[i+1]);
rotor1.Replace(rotor1[rotor1.Length-1], rotorFirst);
static void write(string rotor, string rotorb, int num)
Console.WriteLine($"rotor {num}: ");
for(int i = 0; i < rotor.Length-1; i++)
Console.WriteLine($"Position {i}, värde a: {rotor[i]}, värde b: {rotorb[i]}");
Console.WriteLine($"Position {rotor.Length-1}, värde a: {rotor[rotor.Length-1]}, värde b: {rotorb[rotorb.Length-1]}\n\n");
static void read(ref char[] message, out string userInput)
userInput = Console.ReadLine();
for(int i = 0; i < userInput.Length; i++)
message[i] = userInput[i];
for(int j = 0; j < userInput.Length; j++)
Console.WriteLine(message[j]);
static char[] cipher (string userInput, char[] message, string reference, ref string rotor1, ref string rotor1b, ref int rotations1, ref string rotor2, ref string rotor2b, ref int rotations2, ref string rotor3, ref string rotor3b, ref int rotations3, int[] turnAround)
char[] answer = new char[userInput.Length];
int[] indexReference = new int [userInput.Length];
int[] indexRotor1 = new int [userInput.Length];
int[] indexRotor1b = new int [userInput.Length];
int[] indexRotor2 = new int [userInput.Length];
int[] indexRotor2b = new int [userInput.Length];
int[] indexRotor3 = new int [userInput.Length];
int[] indexRotor3b = new int [userInput.Length];
for(int i = 0; i < userInput.Length; i++)
indexReference[i] = reference.IndexOf(message[i]);
indexRotor1[i] = indexReference[i];
indexRotor1b[i] = rotor1b.IndexOf(rotor1[indexRotor1[i]]);
if(indexRotor1b[i] == -1)
Console.WriteLine("Rotor1b not properly indexed");
indexRotor2[i] = indexRotor1b[i];
indexRotor2b[i] = rotor2b.IndexOf(rotor2[indexRotor2[i]]);
if(indexRotor2b[i] == -1)
Console.WriteLine("Rotor2b not properly indexed");
indexRotor3[i] = indexRotor2b[i];
Console.WriteLine(indexRotor3[i]);
indexRotor3b[i] = rotor3b.IndexOf(rotor3[indexRotor3[i]]);
if(indexRotor3b[i] == -1)
Console.WriteLine("Rotor3b not properly indexed");
Console.WriteLine("Rotor3b properly indexed");
indexRotor3b[i] = turnAround[indexRotor3b[i]];
indexRotor3[i] = rotor3.IndexOf(rotor3b[indexRotor3b[i]]);
Console.WriteLine("Rotor3 not properly indexed");
Console.WriteLine("Rotor3 properly indexed");
indexRotor2b[i] = indexRotor3[i];
Console.WriteLine(indexRotor3[i]);
indexRotor2[i] = rotor2.IndexOf(rotor2b[indexRotor2b[i]]);
Console.WriteLine("Rotor2 not properly indexed");
Console.WriteLine("Rotor2 properly indexed");
indexRotor1b[i] = indexRotor2[i];
Console.WriteLine(indexRotor1b[i]);
indexRotor1[i] = rotor1.IndexOf(rotor1b[indexRotor1b[i]]);
Console.WriteLine("Rotor1 not properly indexed");
Console.WriteLine("Rotor1 properly indexed");
answer[i] = reference[indexRotor1b[i]];
rotate1(ref rotor1, ref rotor1b, ref rotations1, ref rotor2, ref rotor2b, ref rotations2, ref rotor3, ref rotor3b, ref rotations3);