Copyright © infotec016 . Powered by Blogger.

Thursday, April 20, 2017

Cryptography : Playfair Cipher


Playfair Cipher

Sample java code for Playfair Cipher


Encryption




//playfair encryption: i and j count as one letter
import java.util.*;
public class playfair_encry{
public static void main(String args[]){
Scanner myscanner=new Scanner(System.in);
System.out.println("Enter the keyword");
String key=myscanner.nextLine().toLowerCase().replace(" ","").replace("j","i");
String alpha="abcdefghiklmnopqrstuvwxyz";
char array[][];
array=new char[5][5];
char arraykey[];
arraykey=new char[key.length()];
arraykey[0]=key.charAt(0);
int m=1;
//inserting the keyword into arraykey without repeating letters
for(int i=1;i<key.length();i++){
char a=key.charAt(i);
boolean found=false;
for(int j=0;j<key.length();j++){
if(a==arraykey[j]){
found=true;
}
}
if(found==false){
arraykey[m]=a;
m++;
}
}
//inserting into the array from arraykey
int k=0;
for(int i=0;i<=4;i++){
for(int j=0;j<=4;j++){
if(k<key.length()){
if(arraykey[k]!='\u0000'){
array[i][j]=arraykey[k];
k++;
}
}
}
}
//inserting the alphabet letters-arraykey into the arraykey2
char arraykey2[];
arraykey2=new char[alpha.length()];
int p=0;
for(int i=0;i<alpha.length();i++){
boolean found=false;
char a=alpha.charAt(i);
for(int j=0;j<arraykey.length;j++){
if(arraykey[j]==a){
found=true;
}
}
if(found==false){
arraykey2[p]=a;
p++;
}
}
//insert the elements from arraykey2 into array
int q=0;
for(int j=k%5;j<=4;j++){
if(q<arraykey2.length){
if(arraykey2[q]!='\u0000'){
array[k/5][j]=arraykey2[q];
q++;
}
}
}
for(int i=k/5+1;i<=4;i++){
for(int j=0;j<=4;j++){
if(q<arraykey2.length){
if(arraykey2[q]!='\u0000'){
array[i][j]=arraykey2[q];
q++;
}
}
}
}
System.out.println();
//print  the matrix
for(int i=0;i<=4;i++){
for(int j=0;j<=4;j++){
System.out.print(array[i][j]);
}
System.out.println();
}
System.out.println();
System.out.println("plain text");
String word=myscanner.nextLine().toLowerCase().replace(" ","").replace("j","i");
System.out.println();
System.out.println("cipher text (i & j count as one letter)");
//changing the plaintext to be pair
for(int i=0;i<word.length();i=i+2){
char a=word.charAt(i);
char b='\u0000';
if(i+1<word.length()){
b=word.charAt(i+1);
}
if(a==b){
word=word.substring(0,i+1).concat("x".concat(word.substring(i+1,word.length())));
}
}
if(word.length()%2==1){
word=word.concat("x");
}
//finding the array index and substituting the letters
int ai=0,aj=0,bi=0,bj=0;
for(int n=0;n<word.length();n=n+2){
char a=word.charAt(n);
char b=word.charAt(n+1);
for(int i=0;i<=4;i++){
for(int j=0;j<=4;j++){
if(a==array[i][j]){
ai=i;
aj=j;
}
if(b==array[i][j]){
bi=i;
bj=j;
}
}
}
if(bj==aj && ai!=4){
System.out.print(array[ai+1][bj]);
}
if(bj==aj && ai==4){
System.out.print(array[0][bj]);
}
if(bj==aj &&  bi!=4){
System.out.print(array[bi+1][aj]);
}
if(bj==aj && bi==4){
System.out.print(array[0][bj]);
}
if(bi==ai && aj!=4){
System.out.print(array[bi][aj+1]);
}
if(bi==ai && aj==4){
System.out.print(array[ai][0]);
}
if(bi==ai && bj!=4){
System.out.print(array[ai][bj+1]);
}
if(bi==ai &&  bj==4){
System.out.print(array[bi][0]);
}
if(ai!=bi && bj!=aj){
System.out.print(array[ai][bj]);
System.out.print(array[bi][aj]);
}
}
}
}

output of the above code

                                                                     Decryption



//playfair encryption: i and j count as one letter.no of letters in ciphertext be even no;
import java.util.*;
public class playfair_decry{
public static void main(String args[]){
Scanner myscanner=new Scanner(System.in);
System.out.println("Enter the keyword");
String key=myscanner.nextLine().toLowerCase().replace(" ","").replace("j","i");
String alpha="abcdefghiklmnopqrstuvwxyz";
char array[][];
array=new char[5][5];
char arraykey[];
arraykey=new char[key.length()];
arraykey[0]=key.charAt(0);
int m=1;
//inserting the keyword into arraykey without repeating letters
for(int i=1;i<key.length();i++){
char a=key.charAt(i);
boolean found=false;
for(int j=0;j<key.length();j++){
if(a==arraykey[j]){
found=true;
}
}
if(found==false){
arraykey[m]=a;
m++;
}
}
//inserting into the array from arraykey
int k=0;
for(int i=0;i<=4;i++){
for(int j=0;j<=4;j++){
if(k<key.length()){
if(arraykey[k]!='\u0000'){
array[i][j]=arraykey[k];
k++;
}
}
}
}
//inserting the alphabet letters-arraykey into the arraykey2
char arraykey2[];
arraykey2=new char[alpha.length()];
int p=0;
for(int i=0;i<alpha.length();i++){
boolean found=false;
char a=alpha.charAt(i);
for(int j=0;j<arraykey.length;j++){
if(arraykey[j]==a){
found=true;
}
}
if(found==false){
arraykey2[p]=a;
p++;
}
}
//insert the elements from arraykey2 into array
int q=0;
for(int j=k%5;j<=4;j++){
if(q<arraykey2.length){
if(arraykey2[q]!='\u0000'){
array[k/5][j]=arraykey2[q];
q++;
}
}
}

for(int i=k/5+1;i<=4;i++){
for(int j=0;j<=4;j++){
if(q<arraykey2.length){
if(arraykey2[q]!='\u0000'){
array[i][j]=arraykey2[q];
q++;
}
}
}
}
System.out.println();
//print  the matrix
for(int i=0;i<=4;i++){
for(int j=0;j<=4;j++){
System.out.print(array[i][j]);
}
System.out.println();
}

System.out.println();
System.out.println("cipher text");
String word=myscanner.nextLine().toLowerCase().replace(" ","").replace("j","i");
System.out.println();
System.out.println("original text (i & j count as one letter)");
//finding the array index and substituting the letters
int ai=0,aj=0,bi=0,bj=0;
for(int n=0;n<word.length();n=n+2){
char a=word.charAt(n);
char b=word.charAt(n+1);
for(int i=0;i<=4;i++){
for(int j=0;j<=4;j++){
if(a==array[i][j]){
ai=i;
aj=j;
}
if(b==array[i][j]){
bi=i;
bj=j;
}
}
}

if(bj==aj && ai!=0){
System.out.print(array[ai-1][bj]);
}
if(bj==aj && ai==0){
System.out.print(array[4][bj]);
}
if(bj==aj &&  bi!=0){
System.out.print(array[bi-1][aj]);
}
if(bj==aj && bi==0){
System.out.print(array[4][bj]);
}

if(bi==ai && aj!=0){
System.out.print(array[bi][aj-1]);
}
if(bi==ai && aj==0){
System.out.print(array[ai][4]);
}
if(bi==ai && bj!=0){
System.out.print(array[ai][bj-1]);
}
if(bi==ai &&  bj==0){
System.out.print(array[bi][4]);
}

if(ai!=bi && bj!=aj){
System.out.print(array[ai][bj]);
System.out.print(array[bi][aj]);
}
}
}
}





output of the above code

0 comments:

Post a Comment