package substitutionencryption;
import java.util.Scanner;
public class SubstitutionDemo
{
public static void main(String[] args)
{
Scanner bb = new Scanner(System.in);
System.out.println("Please enter the key.");
String key = bb.nextLine();
System.out.println("If you would like to encrypt, type [E]."
+ " If you would like to decrypt, type [D]");
String answer = bb.nextLine();
char choice = answer.charAt(0);
if (choice == 'e' || choice == 'E'){
System.out.println("Please enter the plain text.");
String plaintext = bb.nextLine();
encryption(key, plaintext);
}
else
System.out.println("Please enter the cipher text.");
String ciphertext = bb.nextLine();
decryption(key, ciphertext);
}
public static void encryption(String ps, String pl){
char [] letters = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
String password = ps;
String plaintext = pl;
password = password + "abcdefghijklmnopqrstuvwxyz";
int kk = password.length();
char [] userEntered = password.toCharArray();
char [] userFixed = new char [kk];
char [] plainx = plaintext.toCharArray();
int l = 0;
for(int i = 0; i < userEntered.length; i++){
char x = userEntered[i];
if (Character.isLetter(x)) {
boolean Insert = true;
for(int j = 0; j < userEntered.length; j++){
if (userFixed[j] == x)
Insert=false;
}
if (Insert) {
userFixed[l] = x;
l++;
}
}
}
for(int i = 0; i < plainx.length; i++){
for(int x = 0; x < letters.length; x++){
if (plainx[i] == letters[x])
System.out.print(userFixed[x]);
}
}
}
public static void decryption(String la, String pn) {
char [] letters = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
String password = la;
String cipher = pn;
password = password + "abcdefghijklmnopqrstuvwxyz";
int kk = password.length();
char [] userEntered = password.toCharArray();
char [] userFixed = new char [kk];
char [] cipherx = cipher.toCharArray();
int l = 0;
for(int i = 0; i < userEntered.length; i++){
char x = userEntered[i];
if (Character.isLetter(x)) {
boolean Insert = true;
for(int j = 0; j < userEntered.length; j++){
if (userFixed[j] == x)
Insert=false;
}
if (Insert) {
userFixed[l] = x;
l++;
}
}
}
for(int i = 0; i < cipherx.length; i++){
for(int x = 0; x < userFixed.length; x++){
if (cipherx[i] == userFixed[x])
System.out.print(letters[x]);
}
}
}
}