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.
Above code produce below result, the above code took 17 iteration to get the actual message.
Note: please make sure to read articles Brute force and Caesar Cipher before this article.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Post a Comment