I have got a problem with decrypting a Caesar cipher in C#. We have to do it manually, encrypt and decrypt. I want it to write all possibilities shifting one by one (then it is easy to see the right decryption, because other 25
possibilities are nonsense). The problem is that it writes only one possibility, not 26
. I tried everything. Do you have any idea where is the problem?
string text = "Z programovani{}{}{}";
text = text.ToUpper();
string output = "";
int shift = 3;
foreach (char a in text)
{
int x = (int)a;
if (x >= 65 && x <= 90)
{
x += shift;
}
if (x == 32)
{
x -= shift;
}
if (x >90)
{
x = x - 26;
}
output += (char)x;
}
Console.WriteLine(output);
int i = 0;
do
{
string decoded = "";
foreach (char a in output)
{
int x = (int)a;
if (x >= 65 && x <= 90)
{
x += 1;
}
if (x > 90)
{
x = x + 26;
}
decoded += (char)x;
}
i++;
Console.WriteLine(decoded);
} while (i < 27);
Console.ReadKey();