COURSE NAME: INFORMATION SECURITY Classic Ciphers: Substitution Encryption with Playfair Cipher Bellows, a java code of

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

COURSE NAME: INFORMATION SECURITY Classic Ciphers: Substitution Encryption with Playfair Cipher Bellows, a java code of

Post by answerhappygod »

COURSE NAME: INFORMATION SECURITY
Course Name Information Security Classic Ciphers Substitution Encryption With Playfair Cipher Bellows A Java Code Of 1
Course Name Information Security Classic Ciphers Substitution Encryption With Playfair Cipher Bellows A Java Code Of 1 (95.83 KiB) Viewed 38 times
Course Name Information Security Classic Ciphers Substitution Encryption With Playfair Cipher Bellows A Java Code Of 2
Course Name Information Security Classic Ciphers Substitution Encryption With Playfair Cipher Bellows A Java Code Of 2 (93.95 KiB) Viewed 38 times
Course Name Information Security Classic Ciphers Substitution Encryption With Playfair Cipher Bellows A Java Code Of 3
Course Name Information Security Classic Ciphers Substitution Encryption With Playfair Cipher Bellows A Java Code Of 3 (94.1 KiB) Viewed 38 times
Course Name Information Security Classic Ciphers Substitution Encryption With Playfair Cipher Bellows A Java Code Of 4
Course Name Information Security Classic Ciphers Substitution Encryption With Playfair Cipher Bellows A Java Code Of 4 (92.94 KiB) Viewed 38 times
Course Name Information Security Classic Ciphers Substitution Encryption With Playfair Cipher Bellows A Java Code Of 5
Course Name Information Security Classic Ciphers Substitution Encryption With Playfair Cipher Bellows A Java Code Of 5 (99.45 KiB) Viewed 38 times
Course Name Information Security Classic Ciphers Substitution Encryption With Playfair Cipher Bellows A Java Code Of 6
Course Name Information Security Classic Ciphers Substitution Encryption With Playfair Cipher Bellows A Java Code Of 6 (99.58 KiB) Viewed 38 times
Classic Ciphers: Substitution Encryption with Playfair Cipher
Bellows, a java code of the Playfair Encryption and decryption:
package Playfair;
import java.util.Scanner;
public class Playfair
{ private String KeyWord=new String();
private String Key=new String();
private char matrix_arr[][]= new char[5][5];
public void setKey(String k)
{
String K_adjust=new String();
boolean flag = false;
K_adjust = K_adjust + k.charAt(0);
for(int i=1; i<k.length();i++)
{
for(int j=0;j<K_adjust.length(); j++)
{
if(k.charAt(i)==K_adjust.charAt(j))
{
flag = true;
}
}
if(flag == false)
K_adjust = K_adjust + k.charAt(i);
flag = false;
}
KeyWord=K_adjust;
}
public void KeyGen()
{
boolean flag=true;
char current;
Key=KeyWord;
for ( int i=0 ; i<26 ; i++)
{
current=(char)(i+97);
if(current=='j')
continue;
for(int j=0 ; j< KeyWord.length() ; j++ )
{
if (current == KeyWord.charAt(j))
{
flag=false;
break;
}
} if(flag)
Key=Key+current;
flag=true;
}
System.out.println(Key);
matrix ();
}
private void matrix ()
{
int counter=0;
for (int i=0 ; i<5 ;i++)
{
for (int j=0 ; j<5 ; j++)
{
matrix_arr[j]=Key.charAt(counter);
System.out.print(matrix_arr[j]);
counter++;
}
System.out.println("\n");
}
}
private String format(String old_text)
{
int i = 0;
int j = 0;
int len = 0;
String text = new String();
len = old_text.length();
for (int tmp = 0; tmp < len; tmp++)
{
if (old_text.charAt(tmp) == 'j')
{
text = text + 'i';
}
else
text = text+old_text.charAt(tmp);
}
len = text.length();
for (i = 0; i < len; i = i + 2)
{
if (text.charAt(i+1) == text.charAt(i))
{
text = text.substring(0, i+1) + 'x' + text.substring(i+1);
}
else
{}
}
return text;
}
private String [] Divid2Pairs (String new_string)
{
String Original = format(new_string);
int size= Original.length();
if(size%2!=0)
{
size++;
Original = Original+'x';
}
String x[]= new String[size/2];
int counter=0;
for ( int i=0 ; i<size/2 ;i++)
{
x=Original.substring(counter, counter+2);
System.out.println(x);
counter=counter+2;
}
return x;
}
public int[] GetDiminsions(char letter)
{
int []key=new int[2];
if ( letter == 'j')
letter='i';
for (int i=0 ; i<5 ;i++)
{
for (int j=0 ; j<5 ; j++)
{
if(matrix_arr[j] == letter)
{
key[0]=i;
key[1]=j;
break;}
}
}
return key;
}
public String Encript(String Source)
{
System.out.println("Encription Start");
String src_arr[]=Divid2Pairs(Source);
String Code=new String();
char one;
char two;
int part1[]=new int[2];
int part2[]=new int[2];
for (int i=0 ; i< src_arr.length ;i++ )
{
one = src_arr.charAt(0);
two = src_arr.charAt(1);
part1 = GetDiminsions(one);
part2 = GetDiminsions(two);
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;
}
else
{
int temp=part1[1];
part1[1]=part2[1];
part2[1]=temp;
}
Code= Code + matrix_arr[part1[0]][part1[1]] + matrix_arr[part2[0]][part2[1]];
}
System.out.println(Code);
return Code;
}
public String Decript (String Code) {
System.out.println("Decription Start");
String Original=new String();
String src_arr[]=Divid2Pairs(Code);
char one;
char two;
int part1[]=new int[2];
int part2[]=new int[2];
for (int i=0 ; i< src_arr.length ;i++ )
{
one = src_arr.charAt(0);
two = src_arr.charAt(1);
part1 = GetDiminsions(one);
part2 = GetDiminsions
(two);
if(part1[0]==part2[0])
{
if (part1[1]>0)
part1[1]--;
else
part1[1]=4;
if(part2[1]>0)
part2[1]--;
else
part2[1]=4;
}
else if (part1[1]==part2[1])
{
if (part1[0]>0)
part1[0]--;
else
part1[0]=4;
if(part2[0]>0)
part2[0]--;
else
part2[0]=4;
}
else
{
int temp=part1[1];
part1[1]=part2[1];
part2[1]=temp ;
} Original =Original + matrix_arr[part1[0]][part1[1]] +
matrix_arr[part2[0]][part2[1]];
}
System.out.println(Original);
return Original;
}
public static void main(String[] args)
{
Playfair x=new Playfair();
Scanner sc = new Scanner(System.in);
System.out.println("Enter a keyword:");
String keyword = sc.next();
x.setKey(keyword);
x.KeyGen();
System.out.println("To Encrypt enter 1 \nTo Decript enter 2\nTesting both enter anything else:");
int choosen_value = sc.nextInt();
if(choosen_value==1)
{
System.out.println("Enter word to encrypt:");
String key_input = sc.next();
String Encripted= x.Encript(key_input);
}
else if(choosen_value==2)
{
System.out.println("Enter word to decrypt:");
String decripted = sc.next();
x.Decript(decripted);
}
else
{
System.out.println("Enter word to encrypt & decrypt:");
String key_input = sc.next();
String Encripted= x.Encript(key_input);
x.Decript(Encripted);
} }}
Classic Ciphers: Substitution Encryption with Playfair Cipher Bellows, a java code of the Playfair Encryption and decryption: package Playfair; import java.util.Scanner; public class Playfair { private String Keyword=new String(); private String Key=new String(); private char matrix_arr[][]= new char[5][5]; public void setkey (String k) { String K_adjust=new String(); boolean flag = false; K_adjust = K_adjust + k.charAt(0); for(int i=1; i<k.length();i++) for(int j=0; j<K_adjust.length(); j++) { if(k.charAt(i)==k_adjust.charAt(j)) { flag = true; } } if(flag false) K_adjust = K_adjust + k.charAt(i); flag = false; } Keyword=K_adjust; } public void KeyGen() { boolean flag=true; char current; Key=keyword; for (int i=0; i<26 ; i++) { current=(char) (i+97); if(current=='j') continue; for(int j=0; j< Keyword.length(); j++) { if (current == Keyword.charAt(j)) { flag=false; break; } } if(flag) Key=key+current; flag=true; Page 2 of 7

} System.out.println(Key); matrix (); private void matrix () { int counter=0; for (int i=0; i<5 ;i++) { for (int j=0; j<5; j++) { matrix_arr[j]=key.charAt(counter); System.out.print(matrix_arr[i][j]); counter++; } System.out.println("\n"); private String format(String old_text) { int i = 0; int i = 0; int len = 0; String text = new String(); len = old_text.length(); for (int tmp = 0; tmp < len; tmp++) { if (old_text.charAt(tmp) 'j') { text = text + 'i'; } else text = text+old_text.charAt(tmp); } len = text.length(); for (i = 0; i < len; i = i + 2) { if (text.charAt(i+1) == text.charAt(i)) { text = text.substring(0, i+1) + 'x' + text.substring(i+1); } else O } return text; } private String [] Divid2Pairs (String new_string) string Original = format(new_string); int size= Original.length(); if(size%2!=0) { size++; Original = Original+'x'; } String [ ] = new String[size/2]; Page 3 of 7

int counter=0; for (int i=0; i<size/2 ;i++) x[i]=Original.substring(counter, counter+2); System.out.println(x[i]); counter=counter+2; } return x; 3 public int[] GetDiminsions (char letter) { int []key=new int[2]; if ( letter == 'j') letter='i'; for (int i=0; i<5 ;i++) { for (int j=0; j<5; j++) if(matrix_arr[i][j] letter) key[@]=i; key[1]=j; break;) } return key; } public String Encript(String Source) System.out.println("Encription Start"); String src_arr[]=Divid2Pairs (Source); String Code=new String(); char one; char two; int parti[]=new int[2]; int part2[ ]=new int[2]; for (int i=0; i< src_arr. length ;i++) { one = src_arr[i].charAt(0); two = src_arr[i].charAt(1); parti = GetDiminsions (one); part2 = GetDiminsions(two); 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]) Page 4 of 7

if (part1[@]<4) part1[0]++; else part1[0]=0; if(part2[@]<4) part2[@]++; else part2[@]=0; } else { int temp=part1[1]; part1[1]=part2[1]; part2[1]=temp; } Code= Code + matrix_arr[part1[@]][part1[1]] + matrix_arr (part2[@]][part2[1]]; } System.out.println(Code); return Code; } public String Decript (String Code) { System.out.println("Decription Start"); string Original=new string(); String src_arr[]=Divid2Pairs(Code); char one; char two; int parti[]=new int[2]; int part2[]=new int[2]; for (int i=0; i< src_arr.length; i++) { one = src_arr[i].charAt(); two = src_arr[i].charAt(1); part1 = GetDiminsions (one); part2 = GetDiminsions (two); if(part1[@]==part2[@]) { if (part1[1]>0) part1[1]--; else part1[1]=4; if(part2[1]>0) part2[1]--; else part2[1]=4; } else if (part1[1]==part2[1]) { if (part1[@]>0) part1[0]--; else part1[@]=4; if(part2[@]>0) Page 5 of 7

part2[0]; else part2[@]=4; } else { int temp=part1[1]; part1[1]=part2[1]; part2[1]=temp; } Original =Original + matrix_arr(part1[@]][part1[1]] + matrix_arr(part2[@]](part2[1]]; } System.out.println(Original); return Original; } public static void main(String[] args) { Playfair x=new Playfair(); Scanner sc = new Scanner(System.in); System.out.println("Enter a keyword:"); String keyword = sc.next(); X.setKey(keyword); X.KeyGen(); System.out.println("To Encrypt enter 1 \nTo Decript enter 2\nTesting both enter anything else:"); int choosen_value = sc.nextInt(); if (choosen_value==1) { System.out.println("Enter word to encrypt:"); String key_input = sc.next(); String Encripted= x.Encript(key_input); else if (choosen_value==2) { System.out.println("Enter word to decrypt:"); String decripted = sc.next(); X.Decript(decripted); } else { System.out.println("Enter word to encrypt & decrypt: "); String key_input = sc.next(); String Encripted= x. Encript(key_input); X.Decript(Encripted); }}} Page 6 of 7

Run the Playfair program Encrypt the plaintext "holiday” with the keyword exam 1) The program present an error; propose a solution for encryption the plaintext “holiday” 2) Write the new plaintext 3) Write the found ciphertext 4) We propose to use the playfair 6*6 matrix instead of playfair 5*5 matrix. Make the essential changes in the program for put into consideration the new matrix size: > private char matrix_arr [11]newchar[5][5]; for (int i=0; i<26 ;i++) for (int j=0; j<5; j++) for (int i=0; i<5;i++) 5) Write the new Ciphertext Page 7 of 7
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply