Break caesar-cipher with brute force attack

Caesar Cipher (Cracking Caesar Cipher with the Brute-Force Technique) – Part II

In Part I, I have written code to encrypt the plain text for demonstration purpose . In this article we will use Brute Force technique to decrypt the encrypted message.

Note: please make sure to read articles Brute force and Caesar Cipher before this article.

Here is C# Code snippet for the same

using System;
using System.Linq;
using System.Text;

namespace SecureHashingProject
{
    public class BruteForceCaesarCipher
    {
        private const string Letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        static void Main()
        {
            var message = "CQRB RB CNBC VNBBJPN";

            for (var shiftKey = 0; shiftKey < Letters.Length; shiftKey++)
            {
                var result = new StringBuilder();
                foreach (var m in message)
                {
                    if (Letters.Contains(m))
                    {
                        var num = Letters.IndexOf(m);
                        num += shiftKey;

                        if (num >= Letters.Length)
                            num -= Letters.Length;

                        result = result.Append(Letters[num]);
                    }
                    else
                    {
                        result = result.Append(m);
                    }
                }
                Console.WriteLine($"shift key: {shiftKey}, value: {result}");
            }
            Console.ReadLine();
        }
    }
}

Above code produce below  result, the above code took 17 iteration  to get the actual message.

Output BRUTE-FORCE TECHNIQUE C# Source code to creak caesar cipher

Leave a Reply

Your email address will not be published. Required fields are marked *