PlayFairCipher algorytham

 Playfair is one of the popular cryptographic software security algorithms. This technique encrypts pairs of letters at a time and generates more secure encrypted text compare to  the simple substitution cipher like Caesar.


Implementation of playfaircipher:-

package com.clg.PlayFair;

import java.util.Scanner;

public class PlayFairCipher {
static char[][] Matrix = new char[5][5];   // A 5*5 matrix of letter based on a keywords
static String Alphabats = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static String cipher = "";
static String plain = "";

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the plainText ");
String plainText = sc.nextLine();
System.out.println("Enter the Key ");
String key = sc.nextLine();

String encrypted = Encrypt(plainText, key);  //Encrypt method return to plain text to cipher text 
System.out.println("Encrypted Massage");
System.out.println(encrypted);
System.out.println("-------------------------------------------------------------------");
String decryptedText = decrypt(encrypted, key);   // decrypt method return to cipher text to plain text
System.out.println("Decrypted Massage");
System.out.println(decryptedText);
sc.close();

}

// Encrypting
private static String Encrypt(String plainText, String key) {
insertMatrix(key); // in this function inserting the key inside metrix
String cipherText = "";
plainText = plainText.replaceAll("j", "i"); // changing all i into j
plainText = plainText.replaceAll(" ", "");
plainText = plainText.toUpperCase();
int len = plainText.length();

// if a pair reapeted letter insert 'X'
if (len / 2 != 0) {
plainText += "X";
++len;
}

for (int i = 0; i < len - 1; i = i + 2) {
cipherText = EncyptCharacter(plainText.substring(i, i + 2));
cipherText += "";
}
return cipherText;
}
   //converting to plain text to cipherText
private static String EncyptCharacter(String text) {
StringBuffer CiChar = new StringBuffer();
text = text.toUpperCase();
char a = text.charAt(0);
char b = text.charAt(1);
int r1 = RowValue(a);
int r2 = RowValue(b);
int c1 = ColValue(a);
int c2 = ColValue(b);

// if both letter in same colum replace each with the letter below it(top from bottom)
if (c1 == c2) {
r1++;
r2++;
if (r1 > 4)
r1 = 0;
if (r2 > 4)
r2 = 0;

CiChar.append(Matrix[r1][c2]);
CiChar.append(Matrix[r2][c1]);
} 
//if both letter in same row replace each with letter to rigth(back to start from end )
else if (r1 == r2) {
c1++;
c2++;
if (c1 > 4)
c1 = 0;
if (c2 > 4)
c2 = 0;
CiChar.append(Matrix[r1][c1]);
CiChar.append(Matrix[r2][c2]);
}
//other wise each letter is placed by the letter in tha same row and in the colum of the other letter of the pair
else {
CiChar.append(Matrix[r1][c2]);
CiChar.append(Matrix[r2][c1]);
}
cipher += CiChar + " ";
return cipher;
}

//  Decryption
private static String decrypt(String cipherText, String key) {
String plainText = "";
cipherText = cipherText.replaceAll("J", "I");
cipherText = cipherText.replaceAll(" ", "");
cipherText = cipherText.toUpperCase();
int len = cipherText.length();

for (int i = 0; i < len - 1; i = i + 2) {
plainText = decyptCharacter(cipherText.substring(i, i + 2));
plainText += "";
}
return plainText;
}
   // convering cipher text to plain text 
private static String decyptCharacter(String text) {
StringBuffer PLChar = new StringBuffer();
text = text.toUpperCase();
char a = text.charAt(0);
char b = text.charAt(1);
int r1 = RowValue(a);           //RowValue method return the Row inside curresponding char value  
int r2 = RowValue(b);
int c1 = ColValue(a);           //RowValue method return the col inside curresponding char value
int c2 = ColValue(b);
        
// if both letter in same colum replace each with the letter below it(top from bottom)
if (c1 == c2) {
r1--;
r2--;
if (r1 < 0)
r1 = 4;
if (r2 < 0)
r2 = 4;

PLChar.append(Matrix[r1][c2]);
PLChar.append(Matrix[r2][c1]);
}
//if both letter in same row replace each with letter to rigth(back to start from end )
else if (r1 == r2) {
c1--;
c2--;
if (c1 > 4)
c1 = 0;
if (c2 > 4)
c2 = 0;
PLChar.append(Matrix[r1][c1]);
PLChar.append(Matrix[r2][c2]);
}
//other wise each letter is placed by the letter in tha same row and in the colum of the other letter of the pair
else {
PLChar.append(Matrix[r1][c2]);
PLChar.append(Matrix[r2][c1]);
}
plain += PLChar + " ";
return plain;
}
   //this method return col index
private static int ColValue(char a) {
for (int i = 0; i < Matrix.length; i++) {
for (int j = 0; j < Matrix[i].length; j++) {
if (Matrix[i][j] == a)
return j;
}
}
return -1;
}
    //this method return row index
private static int RowValue(char a) {
for (int i = 0; i < Matrix.length; i++) {
for (int j = 0; j < Matrix[i].length; j++) {
if (Matrix[i][j] == a)
return i;
}
}
return -1;
}
   //Inserting the matrix 
private static void insertMatrix(String key) {
key = key.toUpperCase();
key = key.replace("J", "I");
key = key.replace(" ", "");
int x = 0;
int y = 0;

for (int i = 0; i < key.length(); i++) {
//dublicate method check the value of matrix if value is presence then it give true
if (!dublicate(key.charAt(i)))
{
Matrix[x][y++] = key.charAt(i);
if (y > 4) {
y = 0;
x++;
}
}
}

char ch = 'A';
while (x < 5) {
while (y < 5) {
if (!dublicate(ch)) {
Matrix[x][y++] = ch;
}
ch++;
}
y = 0;
x++;
}
System.out.println("############### vikash karma ##############");
for (int i = 0; i < 5; i++) {
System.out.println();
for (int j = 0; j < 5; j++) {
System.out.print("\t" + Matrix[i][j]);
}
}
System.out.println();
System.out.println("################ vikash karma ##############");
}
    //checking the dublicate value inside metrix if dublicate the return true else false
private static boolean dublicate(char ch) {
if (!checkIndex(ch)) {   
return true;
}

for (int i = 0; i < Matrix.length; i++) {
for (int j = 0; j < Matrix.length; j++) {
if (Matrix[i][j] == ch || ch == 'J')
return true;
}
}
return false;
}
    //checking the giver charecter is Alphabes of not 
private static boolean checkIndex(char ch) {
for (int i = 0; i < Alphabats.length(); i++) {
if (Alphabats.charAt(i) == ch)
return true;
}
return false;
}
}

OUTPUT:
Enter the plainText
PLAYCIPHERISGOODCIPHER
Enter the Key
SECRETKEY
############### vikash karma ##############

        S       E       C       R       T
        K       Y       A       B       D
        F       G       H       I       L
        M       N       O       P       Q
        U       V       W       X       Z
################ vikash karma ##############
Encrypted Massage
QI BA RH OI CT FR HN QA RH OI CT
-----------------------------------------------------------------
Decrypted Massage
PL AY CI PH ER IS GO OD CI PH ER 


Explanation


Algorytham

















Comments

Popular posts from this blog

Caesar Cipher Algo

Huffman coding algoritnm

Insertion sort using java