Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Im trying to do an exercise in a programming book, one of the exercises being decrypting ciphertext that uses the Caesar cipher with an unknown shift.

Here is the ciphertext:

HVWGWGHVSPSGHQCADIHSFSLSFQWGSWVOJSSJSFSLDSFWSBQSR

I don't know the expected output because I haven't determined the algorithm yet.

I've written the following algorithm to try to decrypt it using the Caesar cipher, but this is as far as I've gotten. How do I decrypt this in C#?

What I have tried:

C#
string s = "HVWGWGHVSPSGHQCADIHSFSLSFQWGSWVOJSSJSFSLDSFWSBQSR";

int[] freq = new int[26];

for (int i = 0; i < s.Length; i++)
{
  // converting 
  string temp = s.Substring(i, 1); 

  // converting to an array
  int itemp = (int)temp.ToCharArray()[0];

  freq[itemp - 65]++;  
}

for (int i = 0; i < 26; i++)
{
  Console.WriteLine(i + " " + freq[i]);
}
Posted
Comments
Michael_Davies 16-Oct-16 15:44pm    
This exact question was put on stackoverflow 3 years ago!

http://stackoverflow.com/questions/17688958/how-do-i-write-an-algorithm-to-break-the-caesar-cipher

It also has the answer:

THISISTHEBESTCOMPUTEREXERCISEIHAVEEVEREXPERIENCED

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900