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.

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.

Comments

Popular posts from this blog

SHA-256(Source Code in C# using .Net in-built Cryptography library) - Part II

Basic of Cryptography (Type of Cryptography)- Part II