Write a java program which implements two out of the followi
Write a java program which implements two out of the following three classical cryptosystems which we covered in class: a) Affine Cipher b) Substitution Cipher c) Vigenere Cipher Your program should consist of at least five functions: a) Two functions named encrypt, one for each of the two algorithms which accepts a lowercase alphabetical plaintext string and key as input and outputs a corresponding ciphertext string. b) Two functions named decrypt, one for each of the two algorithms which accepts a lowercase alphabetical ciphertext string and a key as input and outputs a corresponding plaintext string. c) A main function which will offer users the option to select a cryptosystem, mode (encryption or decryption), and specify plaintext/ciphertext and key inputs using either the command line or a simple GUI. The output of each cryptosystem function should be display to stdout. Note that the format of your key will vary depending on which ciphers you choose to implement. For this problem you should submit your source code and a document containing: a) Your compilation instructions. b) Your execution instructions. c) The output from a sample run in which you use your program to encrypt and decrypt a message using each of your cryptosystems which shows the input, output, and keys used in each.
Solution
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author miracle
*/
import java.util.*;
public class Cryptograph
{
public static String encrypt1(String Msg, int a1, int b1)
{
String CTxt = \"\";
int a = a1;
int b = b1;
for (int i = 0; i < Msg.length(); i++)
{
CTxt = CTxt + (char) ((((a * Msg.charAt(i)) + b) % 26) + 65);
}
return CTxt;
}
public static String decrypt1(String CTxt, int a2, int b2 )
{
String Msg = \"\";
int a = a2;
int b = b2;
int a_inv = 0;
int flag = 0;
for (int i = 0; i < 26; i++)
{
flag = (a * i) % 26;
if (flag == 1)
{
a_inv = i;
System.out.println(i);
}
}
for (int i = 0; i < CTxt.length(); i++)
{
Msg = Msg + (char) (((a_inv * ((CTxt.charAt(i) - b)) % 26)) + 65);
}
String d=Msg.toLowerCase();
return d;
}
public static String encrypt2(String text, final String key)
{
String res = \"\";
text = text.toUpperCase();
for (int i = 0, j = 0; i < text.length(); i++)
{
char c = text.charAt(i);
if (c < \'A\' || c > \'Z\')
continue;
res += (char) ((c + key.charAt(j) - 2 * \'A\') % 26 + \'A\');
j = ++j % key.length();
}
return res;
}
public static String decrypt2(String text, final String key)
{
String res = \"\";
text = text.toUpperCase();
for (int i = 0, j = 0; i < text.length(); i++)
{
char c = text.charAt(i);
if (c < \'A\' || c > \'Z\')
continue;
res += (char) ((c - key.charAt(j) + 26) % 26 + \'A\');
j = ++j % key.length();
}
return res;
}
public static void main(String[] args)
{
System.out.println(\"Enter your choice \ \");
System.out.println(\"1.Affine Cipher \ \");
System.out.println(\"2.Vignere Cipher\");
Scanner sc = new Scanner(System.in);
int choice= sc.nextInt();
switch(choice){
case 1:
System.out.println(\"1.Encrypt \ \");
System.out.println( \"2.Decrypt\");
int nc= sc.nextInt();
switch(nc){
case 1:
System.out.println(\"Enter the key values of alphabets a and b\");
int a1=sc.nextInt();
int b1=sc.nextInt();
System.out.println(\"Enter the String to be encrypted\");
String af = sc.next();
String af1=af.toUpperCase();
System.out.println(\"Encrypted Message is : \"
+ encrypt1(af1,a1,b1));break;
case 2:
System.out.println(\"enter a the values of a and b\");
int a2=sc.nextInt();
int b2=sc.nextInt();
System.out.println(\"Enter the String to be decrypted\");
String df= sc.next();
String df1= df.toUpperCase();
System.out.println(\"Decrypted Message is: \"
+ decrypt1(df1,a2,b2));break;
}break;
case 2:
System.out.println(\"1.Encrypt /n 2.Decrypt\");
int nc1= sc.nextInt();
switch(nc1){
case 1:
System.out.println(\"Enter the Key\");
String ekey= sc.next();
String key= ekey.toUpperCase();
System.out.println(\"Enter the String to be encrypted in lower case\");
String ve = sc.next();
String encryptedMsg = encrypt2(ve, key);
System.out.println(\"Encrypted message: \" + encryptedMsg);
case 2:
System.out.println(\"Enter the Key\");
String dkey= sc.next();
String key1= dkey.toUpperCase();
System.out.println(\"Enter the String to be decrypted in lower case\");
String vd = sc.next();
String decryptedMsg = decrypt2(vd, key1);
System.out.println(\"Decrypted message: \" + decryptedMsg);
}
}
}



