AI Caesar Cipher

Hello everyone. This article is all about creating a smart code that secures your data in a better way.

Here, I will be discussing about AI (Artificially Intelligent) Caesar Cipher. The basic Caesar Cipher algorithm remains the same but the change that has been introduced here is the randomness of the key value, which encrypts the plain text. For example, 3 is normally added with every character of the plain text, which is giving rise to the cipher text.

Plain Text

H

e

l

l

o

Key

+3

+3

+3

+3

+3

Cipher Text

K

h

o

o

r


In this special case, key value is not fixed, as it is random. It can be 3, 4, 5 or anything. Before going into the details, I need to explain something. It is not an artificially intelligent algorithm. Rather, it is an artificially intelligent encryptor-decryptor. The reason behind this statement is that there should not be any alteration done on the algorithm, while the message is in transit. The decryptor may not be able to retrieve the original message from the cipher text because of the changed algorithm. Whatever change must be done, must be done before sending the message and after receiving the message and not while in transit.

Let’s dive into my code.

First of all, it generates a random key of 5 characters. The key is a combination of { 3, 4, 5, C, D, E }. I chose this combination out of nowhere. You can have yours. For example, the key can be 3EE4D, C553D, D5CCE, etc. This key plays an important role in this code. With this key, the encryptor knows, which value should be added or subtracted from the plain text. For example, for the key values 3, 4 & 5; 3, 4 or 5 will be subtracted from the plain text and for the key values C, D & E; 3, 4 or 5 will be added to the plain text. As the key is of 5 characters, the plain text is encrypted 5 times, using Caesar Cipher along with the corresponding key value. The randomness of the key adds an extra security layer to our message. Anyone who tries to hack the message won’t be able to guess the logic, as there is no fixed sequence of operations. Now, how will the decryptor know about the sequence of operations? The answer is hidden inside the key. The sequence of the characters in the key will tell the decryptor what operation to do next. This logic can be used not only for one algorithm, but for multiple algorithms. Each character in the key can denote a different algorithm. This is how the encryptor and the decryptor becomes smart (artificially intelligent).

Here is the MainWindow.xaml code.

  1. <Window x:Class="WpfAppAICryptoAlgo.MainWindow"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  6.         xmlns:local="clr-namespace:WpfAppAICryptoAlgo"  
  7.         mc:Ignorable="d"  
  8.         Title="MainWindow" Height="380" Width="525" WindowStartupLocation="CenterScreen">  
  9.     <Grid>  
  10.         <Button x:Name="GenerateButton" Content="Generate Random Key" HorizontalAlignment="Left" Height="20" Margin="10,10,0,0" VerticalAlignment="Top" Width="128" Click="GenerateButton_Click"/>  
  11.         <Label x:Name="KeyLabel" HorizontalAlignment="Left" Height="29" Margin="155,10,0,0" VerticalAlignment="Top" Width="125"/>  
  12.         <TextBlock HorizontalAlignment="Left" Margin="41,69,0,0" TextWrapping="Wrap" Text="Key" VerticalAlignment="Top"/>  
  13.         <TextBox x:Name="KeyTextBox" HorizontalAlignment="Left" Height="23" Margin="136,69,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>  
  14.         <TextBlock HorizontalAlignment="Left" Margin="46,121,0,0" TextWrapping="Wrap" Text="Plain Text" VerticalAlignment="Top"/>  
  15.         <TextBox x:Name="PlainTextBox" HorizontalAlignment="Left" Height="23" Margin="136,120,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="213"/>  
  16.         <Button x:Name="EncryptButton" Content="Encrypt" HorizontalAlignment="Left" Height="22" Margin="105,167,0,0" VerticalAlignment="Top" Width="132" Click="EncryptButton_Click"/>  
  17.         <TextBlock HorizontalAlignment="Left" Margin="46,218,0,0" TextWrapping="Wrap" Text="Cipher Text" VerticalAlignment="Top"/>  
  18.         <TextBox x:Name="CipherTextBox" HorizontalAlignment="Left" Height="23" Margin="136,217,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="213"/>  
  19.         <TextBlock HorizontalAlignment="Left" Margin="46,287,0,0" TextWrapping="Wrap" Text="Plain Text" VerticalAlignment="Top"/>  
  20.         <TextBox x:Name="PlainTextBox_Copy" HorizontalAlignment="Left" Height="23" Margin="136,286,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="213"/>  
  21.         <Button x:Name="DecryptButton" Content="Decrypt" HorizontalAlignment="Left" Height="22" Margin="105,252,0,0" VerticalAlignment="Top" Width="132" Click="DecryptButton_Click"/>  
  22.         <Button x:Name="ResetButton" Content="Reset" HorizontalAlignment="Left" Height="29" Margin="411,10,0,0" VerticalAlignment="Top" Width="85" Click="ResetButton_Click"/>  
  23.   
  24.     </Grid>  
  25. </Window>  
Here is the MainWindow.xaml.cs code, as given below.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Windows;  
  7. using System.Windows.Controls;  
  8. using System.Windows.Data;  
  9. using System.Windows.Documents;  
  10. using System.Windows.Input;  
  11. using System.Windows.Media;  
  12. using System.Windows.Media.Imaging;  
  13. using System.Windows.Navigation;  
  14. using System.Windows.Shapes;  
  15.   
  16. namespace WpfAppAICryptoAlgo  
  17. {  
  18.     /// <summary>  
  19.     /// Interaction logic for MainWindow.xaml  
  20.     /// </summary>  
  21.     public partial class MainWindow : Window  
  22.     {  
  23.         Random rnd;  
  24.   
  25.         public MainWindow()  
  26.         {  
  27.             InitializeComponent();  
  28.   
  29.             rnd = new Random();  
  30.         }  
  31.   
  32.         private int GenerateRandomNumber()  
  33.         {  
  34.             int n = -1;  
  35.   
  36.             n = rnd.Next(3, 6);  
  37.   
  38.             return n;  
  39.         }  
  40.   
  41.         private char GenerateRandomCharacter()  
  42.         {  
  43.             char ch = '!';  
  44.   
  45.             int n = rnd.Next(3, 6);  
  46.   
  47.             if (n == 3)  
  48.                 ch = 'C';  
  49.             else if (n == 4)  
  50.                 ch = 'D';  
  51.             else  
  52.                 ch = 'E';  
  53.   
  54.             return ch;  
  55.         }  
  56.   
  57.         private string GenerateKey()  
  58.         {  
  59.             string key = "";  
  60.   
  61.             for (int i = 0; i < 5; i++)  
  62.             {  
  63.                 bool IsItCharacter = Convert.ToBoolean(rnd.Next(0,2));  
  64.   
  65.                 if(IsItCharacter)  
  66.                 {  
  67.                     key += GenerateRandomCharacter();  
  68.                 }  
  69.                 else  
  70.                 {  
  71.                     key += GenerateRandomNumber().ToString();  
  72.                 }  
  73.             }  
  74.   
  75.             return key;  
  76.         }  
  77.   
  78.         private void GenerateButton_Click(object sender, RoutedEventArgs e)  
  79.         {  
  80.             KeyLabel.Content = GenerateKey();  
  81.             KeyTextBox.Text = KeyLabel.Content.ToString();  
  82.         }  
  83.   
  84.         private int GetNumber(char v)  
  85.         {  
  86.             int vc = -1;  
  87.   
  88.             if (v == 'C')  
  89.                 vc = 3;  
  90.             else if (v == 'D')  
  91.                 vc = 4;  
  92.             else  
  93.                 vc = 5;  
  94.   
  95.             return vc;  
  96.         }  
  97.   
  98.         private string AIEncryption(string plaintext, string keytext)  
  99.         {  
  100.             string ciphertext = null;  
  101.   
  102.             ciphertext = plaintext;  
  103.             char[] keychars = keytext.ToCharArray();  
  104.             int valuechanger;  
  105.   
  106.             CaesarCipher ccAlgo = new CaesarCipher();  
  107.   
  108.             for (int i = 0; i < keychars.Length; i++)  
  109.             {  
  110.                 if (char.IsDigit(keychars[i]))  
  111.                 {  
  112.                     valuechanger = -(int.Parse(keychars[i].ToString()));  
  113.                 }  
  114.                 else  
  115.                 {  
  116.                     valuechanger = GetNumber(keychars[i]);  
  117.                 }  
  118.   
  119.                 ciphertext = ccAlgo.Encrypt(ciphertext, valuechanger);  
  120.             }  
  121.   
  122.             return ciphertext;  
  123.         }  
  124.   
  125.         private string AIDecryption(string ciphertext, string keytext)  
  126.         {  
  127.             string plaintext = null;  
  128.   
  129.             plaintext = ciphertext;  
  130.             char[] keychars = keytext.ToCharArray();  
  131.             int valuechanger;  
  132.   
  133.             CaesarCipher ccAlgo = new CaesarCipher();  
  134.   
  135.             for (int i = 0; i < keychars.Length; i++)  
  136.             {  
  137.                 if (char.IsDigit(keychars[i]))  
  138.                 {  
  139.                     valuechanger = -(int.Parse(keychars[i].ToString()));  
  140.                 }  
  141.                 else  
  142.                 {  
  143.                     valuechanger = GetNumber(keychars[i]);  
  144.                 }  
  145.   
  146.                 plaintext = ccAlgo.Decrypt(plaintext, valuechanger);  
  147.             }  
  148.   
  149.             return plaintext;  
  150.         }  
  151.   
  152.         private void EncryptButton_Click(object sender, RoutedEventArgs e)  
  153.         {  
  154.             CipherTextBox.Text = AIEncryption(PlainTextBox.Text, KeyTextBox.Text);  
  155.         }  
  156.   
  157.         private void DecryptButton_Click(object sender, RoutedEventArgs e)  
  158.         {  
  159.             string revkey = new string(KeyTextBox.Text.Reverse().ToArray());  
  160.             PlainTextBox_Copy.Text = AIDecryption(CipherTextBox.Text, revkey);  
  161.         }  
  162.   
  163.         private void ResetButton_Click(object sender, RoutedEventArgs e)  
  164.         {  
  165.             KeyLabel.Content = "";  
  166.             KeyTextBox.Text = "";  
  167.             PlainTextBox.Text = "";  
  168.             CipherTextBox.Text = "";  
  169.             PlainTextBox_Copy.Text = "";  
  170.         }  
  171.     }  
  172. }  
Here is the CaesarCipher.cs code, as shown below.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace WpfAppAICryptoAlgo  
  8. {  
  9.     public class CaesarCipher  
  10.     {  
  11.         public string Encrypt(string plaintext, int key)  
  12.         {  
  13.             int av, oav, cav, cv, moav;  
  14.             string ciphertext = null;  
  15.             char[] ptca = plaintext.ToCharArray();  
  16.             char[] cc = new char[500];  
  17.   
  18.             for (int i = 0; i < ptca.Length; i++)  
  19.             {  
  20.                 if (ptca[i] == ' ')  
  21.                 {  
  22.                     cc[i] = ptca[i];  
  23.                 }  
  24.                 else  
  25.                 {  
  26.                     if (Char.IsLetter(ptca[i]))  
  27.                     {  
  28.                         if (Char.IsUpper(ptca[i]))  
  29.                         {  
  30.                             av = ptca[i];  
  31.                             oav = av - 65;  
  32.                             moav = oav + key;  
  33.                             cav = ModFunction(moav, 26);  
  34.                             cv = cav + 65;  
  35.                             cc[i] = (char)cv;  
  36.                         }  
  37.                         else  
  38.                         {  
  39.                             av = ptca[i];  
  40.                             oav = av - 97;  
  41.                             moav = oav + key;  
  42.                             cav = ModFunction(moav, 26);  
  43.                             cv = cav + 97;  
  44.                             cc[i] = (char)cv;  
  45.                         }  
  46.                     }  
  47.                     else if (Char.IsDigit(ptca[i]))  
  48.                     {  
  49.                         av = ptca[i];  
  50.                         oav = av - 48;  
  51.                         moav = oav + key;  
  52.                         cav = ModFunction(moav, 10);  
  53.                         cv = cav + 48;  
  54.                         cc[i] = (char)cv;  
  55.                     }  
  56.                     else  
  57.                     {  
  58.                         av = ptca[i];  
  59.                         cv = av + key;  
  60.                         cc[i] = (char)cv;  
  61.                     }  
  62.                 }  
  63.                 ciphertext += cc[i];  
  64.             }  
  65.   
  66.             return ciphertext;  
  67.         }  
  68.   
  69.         public string Decrypt(string ciphertext, int key)  
  70.         {  
  71.             int av, oav, cav, cv, moav;  
  72.             string plaintext = null;  
  73.             char[] ctca = ciphertext.ToCharArray();  
  74.             char[] cc = new char[500];  
  75.             for (int i = 0; i < ctca.Length; i++)  
  76.             {  
  77.                 if (ctca[i] == ' ')  
  78.                 {  
  79.                     cc[i] = ctca[i];  
  80.                 }  
  81.                 else  
  82.                 {  
  83.                     if (Char.IsLetter(ctca[i]))  
  84.                     {  
  85.                         if (Char.IsUpper(ctca[i]))  
  86.                         {  
  87.                             av = ctca[i];  
  88.                             oav = av - 65;  
  89.                             moav = oav - key;  
  90.                             cav = ModFunction(moav, 26);  
  91.                             cv = cav + 65;  
  92.                             cc[i] = (char)cv;  
  93.                         }  
  94.                         else  
  95.                         {  
  96.                             av = ctca[i];  
  97.                             oav = av - 97;  
  98.                             moav = oav - key;  
  99.                             cav = ModFunction(moav, 26);  
  100.                             cv = cav + 97;  
  101.                             cc[i] = (char)cv;  
  102.                         }  
  103.                     }  
  104.                     else if (Char.IsDigit(ctca[i]))  
  105.                     {  
  106.                         av = ctca[i];  
  107.                         oav = av - 48;  
  108.                         moav = oav - key;  
  109.                         cav = ModFunction(moav, 10);  
  110.                         cv = cav + 48;  
  111.                         cc[i] = (char)cv;  
  112.                     }  
  113.                     else  
  114.                     {  
  115.                         av = ctca[i];  
  116.                         cv = av - key;  
  117.                         cc[i] = (char)cv;  
  118.                     }  
  119.                 }  
  120.                 plaintext += cc[i];  
  121.   
  122.             }  
  123.             return plaintext;  
  124.         }  
  125.   
  126.         private int ModFunction(int a, int b)  
  127.         {  
  128.             int c = 0;  
  129.             if (a < 0)  
  130.             {  
  131.                 c = a + b;  
  132.             }  
  133.             else  
  134.             {  
  135.                 c = a % b;  
  136.             }  
  137.   
  138.             return c;  
  139.         }  
  140.     }  
  141. }  
Did you notice one more thing? Even Caesar Cipher (a technique based algorithm) has a key now, to lock and unlock the message.

I hope you found my article useful.

Stay tuned and keep coding.

Up Next
    Ebook Download
    View all
    Learn
    View all