0% found this document useful (0 votes)
77 views12 pages

Playfair

This Java code implements the Playfair cipher encryption algorithm. It takes in a keyword from the user to generate the 5x5 encryption matrix. It then takes plaintext as input, pairs characters, maps them to positions in the matrix, and outputs the ciphertext by concatenating the matrix positions. It handles special cases like duplicate letters, punctuation, and encodes 'j' as 'i'.

Uploaded by

Pawan Pathak
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views12 pages

Playfair

This Java code implements the Playfair cipher encryption algorithm. It takes in a keyword from the user to generate the 5x5 encryption matrix. It then takes plaintext as input, pairs characters, maps them to positions in the matrix, and outputs the ciphertext by concatenating the matrix positions. It handles special cases like duplicate letters, punctuation, and encodes 'j' as 'i'.

Uploaded by

Pawan Pathak
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Code: import java.util.*; class Playfair1 { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.

println(" "); System.out.println("Enter the key :"); System.out.println(" "); String keyword1=sc.nextLine(); String keyword11=keyword1.toLowerCase(); String keyword=keyword11.replaceAll("\\s+",""); //input key //convert it in lower case //remove all blank space

for(int i=0;i<keyword.length();i++) //check if key contains any punctuation mark or number { char cc=keyword.charAt(i); int ci=(int)cc; if(cc<97 || cc>122) { System.out.println(" "); System.out.println("Key should contain only alphabet"); System.exit(0); } } String k1="";

boolean flag=false; boolean flagi=false; boolean flagj=false; char c1; k1=k1+keyword.charAt(0); for(int i=1; i<keyword.length();i++) { for(int j=0;j<k1.length();j++) { if(keyword.charAt(i)==k1.charAt(j)) { flag=true; } } if(flag==false) { k1=k1+keyword.charAt(i); } flag=false; } keyword=k1; for(int i=0;i<keyword.length();i++) { c1=keyword.charAt(i); if(c1 == 'i') //replace all j with i //remove same alphabet from key

{ flagi=true; } if(c1 == 'j') { flagj=true; } } if(flagi == true && flagj == true) { keyword=keyword.replace('j',' '); } keyword=keyword.replaceAll("\\s+",""); char current; String key=keyword; String alphabet="abcdefghijklmnopqrstuvwxyz"; flagi=false; flagj=false; for(int i=0;i<keyword.length();i++) { current=keyword.charAt(i); alphabet=alphabet.replace(current,' '); //remove alphabets that are in key if(current == 'i' || current == 'j') { alphabet=alphabet.replace('i',' '); //remove i & j from list if any one of them is present in the key //strike out all the alphabets in the key

alphabet=alphabet.replace('j',' '); } } for(int i=0;i<keyword.length();i++) { current=alphabet.charAt(i); if(current == 'i') { flagi=true; } if(current == 'j') { flagj=true; } } if(flagi == false && flagj == false) { alphabet=alphabet.replace('j',' '); } alphabet=alphabet.replaceAll("\\s+",""); key=keyword+alphabet; System.out.println(" "); int counter=0; char matrix[][]= new char[5][5]; for (int i=0 ; i<5 ;i++) //place the key and other alphabets in matrix //remove j if j is present in the key

{ for (int j=0 ; j<5 ; j++) { matrix[i][j]=key.charAt(counter); System.out.printf("%s ",matrix[i][j]); counter++; } System.out.println("\n"); } System.out.println("Enter the message :"); System.out.println(" "); String text=sc.nextLine(); text=text.toLowerCase(); text=text.replaceAll("\\s+",""); //convert the message to lower case //remove all blank space

for(int i=0;i<text.length();i++)//check if there are any punctuation marks and numbers { char cc=text.charAt(i); int ci=(int)cc; if(cc<97 || cc>122) { System.out.println(" "); System.out.println("Messge should contain only alphabets"); System.exit(0); } }

String text1=""; int len=text.length(); for(int i=0;i<len;i++) { if(text.charAt(i) == 'j') { text1=text1+'i'; } else { text1=text1+text.charAt(i); } } System.out.println(" "); len=text1.length(); int kk=0; String kkf=""; for(int i=0; i<len; i++) { if(kk<len-2) { kk=kk+2; if(text1.charAt(i) == text1.charAt(i+1)) { text1=text1.substring(0,i+1)+'x'+text1.substring(i+1); kkf=text1; //add x between same alphabets in a group //replace all j with i

text1=kkf; } i++; } else { break; } } len=text1.length(); if(len%2!=0) { len++; text1=text1+"z"; } len=text1.length(); counter=0; int xfl=len/2; String xl[]=new String[xfl]; for(int i=0;i<len/2;i++) { xl[i]=text1.substring(counter,counter+2); counter=counter+2; } String text2=""; //add z if last alphabet is unpaired

for(int i=0;i<len/2;i++) { text2=text2+" "+xl[i]+" "; } text2=text2.trim(); System.out.println(text2); int f=len/2; String x[]=new String[f]; int count=0; for(int i=0;i<f;i++) { x[i]=text1.substring(count,count+2); count=count+2; } String Code=""; char one; char two; int part1[]=new int[2]; int part2[]=new int[2]; for(int i=0;i<x.length;i++) { one=x[i].charAt(0); two=x[i].charAt(1); for(int l=0;l<5;l++) {

for(int k=0;k<5;k++) { if(matrix[l][k] == one) { part1[0]=l; part1[1]=k; break; } if(matrix[l][k] == two) { part2[0]=l; part2[1]=k; break; } } } for(int l=0;l<5;l++) { for(int k=0;k<5;k++) { if(matrix[l][k] == two) { part2[0]=l; part2[1]=k; break; //check for special cases

} } } if(part1[0]==part2[0]) { if (part1[1]<4) part1[1]++; else part1[1]=0; if(part2[1]<4) part2[1]++; else part2[1]=0; } else if (part1[1]==part2[1]) { if (part1[0]<4) part1[0]++; else part1[0]=0; if(part2[0]<4) part2[0]++; else part2[0]=0; } //same column //same row

else { int temp=part1[1]; part1[1]=part2[1]; part2[1]=temp; } Code= Code + matrix[part1[0]][part1[1]] + matrix[part2[0]][part2[1]]; } System.out.println(" "); System.out.println("The cipher text is :"); System.out.println(" "); len=Code.length(); String code=""; counter=0; for(int i=0;i<len/2;i++) { x[i]=Code.substring(counter,counter+2); counter=counter+2; } for(int i=0;i<len/2;i++) { code=code+" "+x[i]+" "; } code=code.trim(); System.out.println(code);

} } Debugging:

You might also like