package playfair;
import java.awt.Point;
import java.util.Scanner;
public class PlayFair {
public static char [][] a=new char[5][5];
public static String alpha="abcdefghijklmnopqrstuvwxyz";
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Enter the key ");
Scanner in =new Scanner(System.in);
String key=in.nextLine();
setMatrix(key);
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
System.out.println("Enter the message to Encryption(plaintext):");
String plaintext=in.nextLine();
encryption(plaintext);
System.out.println("Enter the message to Decryption");
String ciphertext=in.nextLine();
decryption(ciphertext);
}
public static boolean isRepeat(char c){
for(int i=0;i<5;i++){
for (int j=0;j<5;j++){
if(a[i][j]==c||(a[i][j]=='i'&&c=='j')||(a[i][j]=='j'&&c=='i'))
return true;
}
}
return false;
}
public static void setMatrix(String str){
int k=0; int p=0,q=0;
//setting the keyword in matrix
loop:{
for(int i=0;i<5;i++){
for( int j=0;j<5;){
char ch=str.charAt(k++);
if(!isRepeat(ch)){
a[i][j++]=ch;
}
if(k==str.length()){
p=i;
q=j;
break loop;
}
}
}
}//looping break
//setting remaining letter in matrix
k=0;
loop:
{
for(int i=p;i<5;i++){
for(int j=q ;j<5;){
char ch=alpha.charAt(k++);
if(!isRepeat(ch)){
a[i][j++]=ch;
}
if(k==alpha.length())
break loop;
}
q=0;
}
}//loop breaking
}
public static void encryption(String str){
StringBuilder sb=new StringBuilder(str);
for(int i=0;i<sb.length();i+=2){
if(i==sb.length()-1)
sb.append(sb.length()%2==1?'x':"");
else if (sb.charAt(i)==sb.charAt(i+1))
sb.insert(i+1,"x");
}
encode(sb);
}
//function that encode the string
public static void encode(StringBuilder sb){
for (int i=0;i<sb.length();i+=2){
char key1=sb.charAt(i);
char key2=sb.charAt(i+1);
Point key1pos=position(key1);
Point key2pos=position(key2);
//testing if row1 is equal to row2
int row1=key1pos.x;
int row2=key2pos.x;
int col1=key1pos.y;
int col2=key2pos.y;
if(row1==row2){
col1=(key1pos.y+1)%5;
col2=(key2pos.y+1)%5;
}
else if( col1==col2){
row1=(key1pos.x+1)%5;
row2=(key2pos.x+1)%5;
}
else{
int temp=col1;
col1=col2;
col2=temp;
}
sb.setCharAt(i,a[row1][col1]);
sb.setCharAt(i+1,a[row2][col2]);
}
System.out.println(sb);
}
public static void decryption(String str){
StringBuilder sb=new StringBuilder(str);
for (int i=0;i<sb.length();i+=2){
char key1=sb.charAt(i);
char key2=sb.charAt(i+1);
Point key1pos=position(key1);
Point key2pos=position(key2);
//testing if row1 is equal to row2
int row1=key1pos.x;
int row2=key2pos.x;
int col1=key1pos.y;
int col2=key2pos.y;
if(row1==row2){
col1=(key1pos.y-1);
col2=(key2pos.y-1);
if(col1==-1) col1=4;
if(col2==-1) col2=4;
}
else if( col1==col2){
row1=(key1pos.x-1);
row2=(key2pos.x-1);
if(row1==-1) row1=4;
if(row2==-1) row2=4;
}
else{
int temp=col1;
col1=col2;
col2=temp;
}
sb.setCharAt(i,a[row1][col1]);
sb.setCharAt(i+1,a[row2][col2]);
}
System.out.println(sb);
}
// function to find the position of character in matrix
public static Point position(char c){
Point p=new Point();
for(int i=0;i<5;i++){
for (int j=0;j<5;j++){
if(a[i][j]==c||(a[i][j]=='i'&&c=='j')||(a[i][j]=='j'&&c=='i'))
{
p.x=i;
p.y=j;
return p;
}
}
}
return null;
}
}
No comments:
Post a Comment