As wiki says:
The Playfair cipher or Playfair square is a manual symmetric encryption technique and was the first literal digraph substitution cipher. The scheme was invented in 1854 by Charles Wheatstone, but bears the name of Lord Playfair who promoted the use of the cipher.
The technique encrypts pairs of letters (digraphs), instead of single letters as in the simple substitution cipher and rather more complex Vigenère cipher systems then in use. The Playfair is thus significantly harder to break since the frequency analysis used for simple substitution ciphers does not work with it. Frequency analysis can still be undertaken, but on the 600[1] possible digraphs rather than the 26 possible monographs. The frequency analysis of digraphs is possible, but considerably more difficult – and it generally requires a much larger ciphertext in order to be useful.
See:wiki-Playfair_cipher for details
I tried it at my end at my college times for network security subject. The C# code for the same is below:
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace Playfair
{
class Program
{
public static char[,] matrix
= new char[5,
5];
public static string cipher
= "";
static void Main(string[]
args)
{
create_matrix(); // matrix created
Console.WriteLine("enter plaintext:
");
int
p = 0;
string
plaintext = Console.ReadLine();
string
nwplain = plaintext.Replace(" ", "");
plaintext = nwplain;
int
size = plaintext.Length;
plaintext = test(size, plaintext);
if
(size % 2 == 1)
{
plaintext = plaintext + "x";
}
double
q = (double)size / 2;
int
s = Convert.ToInt32(Math.Ceiling(q));
char[,]
qw = new char[s,
2];
for
(int i = 0; i <= size - 1; i = i + 2)
{
if
(plaintext.Substring(i, 1) == "i")
{
qw[p, 0] = 'j';
}
else
{
qw[p, 0] = Convert.ToChar(plaintext.Substring(i, 1));
}
if
(plaintext.Substring(i + 1, 1) != null)
{
if
(plaintext.Substring(i + 1, 1) == "i")
{
qw[p, 1] = 'j';
}
else
{
qw[p, 1] = Convert.ToChar(plaintext.Substring(i + 1, 1));
}
}
p = p + 1;
}
for
(int i = 0; i < p; i++)
{
string
posi = pos(qw[i, 0]);
string[]
str = posi.Split('.');
int[]
first = new int[2];
int[]
second = new int[2];
first[0] = Convert.ToInt32(str[0]);//
x pos
first[1] = Convert.ToInt32(str[1]);// y pos
posi = pos(qw[i, 1]);
string[]
str1 = posi.Split('.');
second[0] = Convert.ToInt32(str1[0]);//
x pos
second[1] = Convert.ToInt32(str1[1]);//
y pos
if
(first[0] == second[0])// same row
{
if
(first[1] == 4)
{
first[1] = -1;
}
if
(second[1] == 4)
{
second[1] = -1;
}
cipher = cipher +
matrix[(first[0]), (first[1] + 1)];
cipher = cipher +
matrix[(second[0]), (second[1] + 1)];
}
else
if (first[1] == second[1])// same column
{
if
(first[0] == 4)
{
first[0] = -1;
}
if
(second[0] == 4)
{
second[0] = -1;
}
cipher = cipher +
matrix[(first[0] + 1), (first[1])];
cipher = cipher +
matrix[(second[0] + 1), (second[1])];
}
else
{
cipher = cipher +
matrix[first[0], second[1]];
cipher = cipher +
matrix[second[0], first[1]];
}
}
Console.Write("Cipher Text:");
Console.WriteLine(cipher.ToUpper());
string
df = Console.ReadLine();
}// end main
static string pos(char a)
{
string
w="";
for (int i = 0; i < 5; i++)
{
for
(int j = 0; j < 5; j++)
{
if
(a == matrix[i, j])
{
w = i.ToString() +"."+ j.ToString();
break;
}
}
}
return
(w);
}
static string test(int size,
string pt) //doublet
{
ch:
for
(int i = 0; i <=size - 1; i = i + 2)
{
if
(pt.Substring(i, 1) == pt.Substring(i + 1, 1))
{
pt= pt.Insert(i+1, "x");
goto
ch;
}
}
return
(pt);
}
static void create_matrix()
{
string k1 =
"";
Console.WriteLine("Enter Key (alphabetic): ");
string
key = Console.ReadLine();
string
reference = "abcdefghjklmnopqrstuvwxyz";
int
size = key.Length;
string[]
key_arry = new string[size];
for
(int i = 0; i < size; i++)
{
key_arry[i] = key.Substring(i,
1);
}
for
(int i = 0; i < size; i++)
{
for
(int j = 0; j < size; j++)
{
if
(i != j)
{
if (key_arry[i] == "i")
{
key_arry[i] = "j";
}
if (key_arry[i] == key_arry[j])
{
key_arry[j] = "1";
}
}
}
}
for
(int i = 0; i < size; i++)
{
k1 = k1 + key_arry[i].ToString();
}
k1 = k1.Replace('i', 'j');
k1 = k1.Replace('1', ' ');
k1 = k1.Replace(" ", "");
foreach
(string c in
key_arry)
{
reference =
reference.Replace(c, "");
}
int
si = 0;
int
p=0;
int
len = k1.Length;
for
(int i = 0; i < 5; i++)
{
for
(int j = 0; j < 5; j++)
{
if
(si < size)
{
if (key_arry[si] != ""
&& key_arry[si] != "1")
{
matrix[i, j] = Convert.ToChar(key_arry[si]);
si++;
}
else
{
si++;
j--;
}
}
else
{
si++;
if (si >= size)
{
matrix[i, j] = Convert.ToChar(reference.Substring(p, 1));
j++;
p++;
}
j--;
}
}
}
Console.WriteLine("THE PLAYFAIR MATRIX IS:");
for
(int i = 0; i < 5; i++)
{
Console.Write("\n");
for
(int j = 0; j < 5; j++)
{
Console.Write(" ");
Console.Write(matrix[i,
j]);
}
}
Console.WriteLine("");
}
}// end class
program
}//end
namespace
The Output:
hope It Helps U...
No comments:
Post a Comment