Message from Julius Caesar
Below is the complete code for the Message from Julius Caesar Try It Out! problem.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSharpBook.Examples.CaesarCipher
{
class Program
{
static void Main(string[] args)
{
// PART 1: Basic encryption and decryption
string message = "Hello World!";
int key = 3;
string encrypted = Encrypt(message, key);
Console.WriteLine(encrypted);
string decrypted = Decrypt(encrypted, key);
Console.WriteLine(decrypted);
Console.ReadKey();
// PART 2: Cracking the Caesar cipher with an unknown key by
// trying all possible keys. One of them will be readable, the
// rest will be nonsense.
string encryptedMessageToHack = "UX LNKX MH WKBGD RHNK HOTEMBGX";
for (int index = 0; index <= 26; index++)
{
Console.WriteLine(Decrypt(encryptedMessageToHack, index));
}
Console.ReadKey();
}
/// <summary>
/// Encrypts a complete message using a Caesar cipher with a given key.
/// </summary>
private static string Encrypt(string message, int key)
{
message = message.ToUpper();
string encryptedMessage = "";
for (int index = 0; index < message.Length; index++)
{
char encryptedLetter = Encrypt(message[index], key);
encryptedMessage += encryptedLetter;
}
return encryptedMessage;
}
/// <summary>
/// Encrypts a single letter with the given key using a Caesar cipher.
/// </summary>
private static char Encrypt(char letter, int key)
{
// Skip encryption of non-alphabetical letters.
if (letter < 'A' || letter > 'Z') { return letter; }
// Figure out what letter of the alphabet this one is.
int letterAsNumber = (int)letter - 'A';
// Do the actual encryption
int encryptedLetter = (letterAsNumber + key) % 26;
// Turn back into a letter
return (char)(encryptedLetter + 'A');
}
/// <summary>
/// Decrypts a complete message using a Caesar cipher with a given key.
/// </summary>
private static string Decrypt(string message, int key)
{
// We don't want to be worried about case issues, so we'll make it all
// upper case.
message = message.ToUpper();
// Start with an empty message.
string encryptedMessage = "";
// Convert one letter at a time and tack it on the end of our message.
for (int index = 0; index < message.Length; index++)
{
char encryptedLetter = Decrypt(message[index], key);
encryptedMessage += encryptedLetter;
}
return encryptedMessage;
}
/// <summary>
/// Decrypts a single letter, using a Caesar cipher with the given
/// key.
/// </summary>
private static char Decrypt(char letter, int key)
{
// Skip decryption of non-alphabetical letters.
if (letter < 'A' || letter > 'Z') { return letter; }
// Figure out what letter of the alphabet this one is.
int letterAsNumber = (int)letter - 'A';
// Do the actual decryption.
// Depending on the letter and the key, this could go negative.
// To compensate, we add the number of letters in the alphabet.
int encryptedLetter = (letterAsNumber - key + 26) % 26;
// Turn back into a letter
return (char)(encryptedLetter + 'A');
}
}
}
The correct decoded message is:
BE SURE TO DRINK YOUR OVALTINE
Which comes from a well known movie: A Christmas Story