Hello everyone. In my last blog of Cryptography, I demonstrated Caesar Cipher.
In this blog, I will be describing Mono Alphabetic Cipher algorithm.
Mono Alphabetic Cipher is another substitution technique where each character
of the plain text is substituted with another different character. There is
no mathematical relation between the original character and the substituted
character. But whenever a specific character is encountered, it will be always
replaced by the character that is defined in the substitution table. For
example, whenever 'a' is encountered in the plain text, it will always be
replaced by 'q' in the cipher text. The relation between 'a' and 'q' is
random.
The substitution table I have defined here is as follows :
Original Characters : a b c d e f g h i j k l m n o p q r s t u v w x y z
Substituted Characters: q w e r t y u i o p a s d f g h j k l z x c v b n m
So, accordingly if I write
Plain Text : h e l l o
Cipher Text: i t s s g
I have done it for only lower case alphabets. But if you want, you can do
it for upper case alphabets as well as digits.
Here is the code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfAppCryptography.ViewModel;
namespace WpfAppCryptography.Algorithms.TechniqueBased.Substitution
{
public class MonoAlphabeticCipher : Algorithm
{
public new string AlgorithmName = "Mono Alphabetic Cipher";
private char[] o = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
private char[] m = new char[] { 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm' };
private int GetIndex(char ch, char[] ca)
{
int i, result = 0;
for (i = 0; i < 26; ++i)
{
if (ca[i] == ch)
{
result = i;
break;
}
}
return result;
}
public override string Decrypt(string ciphertext, string key)
{
int i, j;
string plaintext = null;
char[] ctca = ciphertext.ToCharArray();
char[] cc = new char[500];
for (i = 0; i < ctca.Length; ++i)
{
if (ctca[i] == ' ')
{
cc[i] = ctca[i];
}
else
{
j = GetIndex(ctca[i], m);
cc[i] = o[j];
}
plaintext += cc[i];
}
return plaintext;
}
public override string Encrypt(string plaintext, string key)
{
int i, j;
string ciphertext = null;
char[] ptca = plaintext.ToCharArray();
char[] cc = new char[500];
for (i = 0; i < ptca.Length; ++i)
{
if (ptca[i] == ' ')
{
cc[i] = ptca[i];
}
else
{
j = GetIndex(ptca[i],o);
cc[i] = m[j];
}
ciphertext += cc[i];
}
return ciphertext;
}
}
}
I hope you find it useful. Keep Coding.
Practice Page Link :
http://handsoncrypto.azurewebsites.net/PracticePage.aspx
In this blog, I will be describing Mono Alphabetic Cipher algorithm.
Mono Alphabetic Cipher is another substitution technique where each character
of the plain text is substituted with another different character. There is
no mathematical relation between the original character and the substituted
character. But whenever a specific character is encountered, it will be always
replaced by the character that is defined in the substitution table. For
example, whenever 'a' is encountered in the plain text, it will always be
replaced by 'q' in the cipher text. The relation between 'a' and 'q' is
random.
The substitution table I have defined here is as follows :
Original Characters : a b c d e f g h i j k l m n o p q r s t u v w x y z
Substituted Characters: q w e r t y u i o p a s d f g h j k l z x c v b n m
So, accordingly if I write
Plain Text : h e l l o
Cipher Text: i t s s g
I have done it for only lower case alphabets. But if you want, you can do
it for upper case alphabets as well as digits.
Here is the code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfAppCryptography.ViewModel;
namespace WpfAppCryptography.Algorithms.TechniqueBased.Substitution
{
public class MonoAlphabeticCipher : Algorithm
{
public new string AlgorithmName = "Mono Alphabetic Cipher";
private char[] o = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
private char[] m = new char[] { 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm' };
private int GetIndex(char ch, char[] ca)
{
int i, result = 0;
for (i = 0; i < 26; ++i)
{
if (ca[i] == ch)
{
result = i;
break;
}
}
return result;
}
public override string Decrypt(string ciphertext, string key)
{
int i, j;
string plaintext = null;
char[] ctca = ciphertext.ToCharArray();
char[] cc = new char[500];
for (i = 0; i < ctca.Length; ++i)
{
if (ctca[i] == ' ')
{
cc[i] = ctca[i];
}
else
{
j = GetIndex(ctca[i], m);
cc[i] = o[j];
}
plaintext += cc[i];
}
return plaintext;
}
public override string Encrypt(string plaintext, string key)
{
int i, j;
string ciphertext = null;
char[] ptca = plaintext.ToCharArray();
char[] cc = new char[500];
for (i = 0; i < ptca.Length; ++i)
{
if (ptca[i] == ' ')
{
cc[i] = ptca[i];
}
else
{
j = GetIndex(ptca[i],o);
cc[i] = m[j];
}
ciphertext += cc[i];
}
return ciphertext;
}
}
}
I hope you find it useful. Keep Coding.
Practice Page Link :
http://handsoncrypto.azurewebsites.net/PracticePage.aspx