I am trying to make a C# Caesar Cipher for an assignment. I have been trying to do this for quite a while and am making no headway whatsoever.
The problem I am having now is that rather than use my encrypted_text and deciphering it, it just cycles through that alphabet, ignoring the character that it started on.What is supposed to happen is it is meant to take the encrypted_text and cycle through the alphabet changing each letter by a certain number.
This is what I have so far:
using System;
using System.IO;
class cipher
{
public static void Main(string[] args)
{
string encrypted_text = "exxego";
string decoded_text = "";
char character;
int shift = 0;
bool userright = false;
char[] alphabet = new char[26] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
do
{
Console.WriteLine("How many times would you like to shift? (Between 0 and 26)");
shift = Convert.ToInt32(Console.ReadLine());
if (shift > 26)
{
Console.WriteLine("Over the limit");
userright = false;
}
if (shift < 0)
{
Console.WriteLine("Under the limit");
userright = false;
}
if (shift <= 26 && shift >= 0)
{
userright = true;
}
} while (userright == false);
for (int i = 0; i < alphabet.Length; i++)
{
decoded_text = "";
foreach (char c in encrypted_text)
{
character = c;
if (character == '\'' || character == ' ')
continue;
shift = Array.IndexOf(alphabet, character) - i;
if (shift <= 0)
shift = shift + 26;
if (shift >= 26)
shift = shift - 26;
decoded_text += alphabet[shift];
}
Console.WriteLine("\nShift #{0} \n{1}", i + 1, decoded_text);
}
StreamWriter file = new StreamWriter("decryptedtext.txt");
file.WriteLine(decoded_text);
file.Close();
}
}
As you can see from my picture, I am getting close. I just need to be able to crack this. Any help would be greatly appreciated. Please forgive me if this is a simple question/solution, I am really new to this.
Your alphabet contains upper case characters, but your input is fully in lower case. You need to handle this situation either by casting all input to same case, or handling both upper/lower case letters.