import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
/**
* Class represents encrypt.
*/
public class Encrypt {
private static final Scanner SCANNER = new Scanner(System.in);
public static void main(String[] args) throws IOException {
char[] plaintext = new char[26];
int i = 0;
// create plaintext
for (char ch = ‘a’; ch <= ‘z’; ch++) {
plaintext[i] = ch;
i++;
}
System.out.println(“Enter the keyword:”);
String keyword = SCANNER.nextLine().toLowerCase();
// get ciphertext
char[] ciphertext = createCiphertext(plaintext, keyword);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter the message (end with a blank line): “);
String message = “”;
String curr;
while (!(curr = bufferedReader.readLine()).equals(“”)) {
message += curr;
}
// get encrypted message
String encrypted = encrypt(plaintext, ciphertext, message);
System.out.println(encrypted);
}
/**
* Creates ciphertext.
* @param plaintext plaintext
* @param keyword keyword
* @return ciphertext
*/
private static char[] createCiphertext(char[] plaintext, String keyword) {
char[] ciphertext = new char[26];
int count = 0;
// copy chars from keyword ignoring existing chars
for (int j = 0; j < keyword.length(); j++) {
if (!exists(ciphertext, keyword.charAt(j), j)) {
ciphertext[count] = keyword.charAt(j);
count++;
}
}
// copy left chars from plaintext
for (int k = 0; k < plaintext.length; k++) {
if (!exists(ciphertext, plaintext[k], count)) {
ciphertext[count] = plaintext[k];
count++;
}
}
return ciphertext;
}
/**
* Checks if given char presents in array
* @param arr array
* @param ch ch
* @param j index
* @return true, if char presents, false – otherwise
*/
private static boolean exists(char[] arr, char ch, int j) {
boolean result = false;
for (int k = 0; k < j; k++) {
if (arr[k] == ch) {
result = true;
break;
}
}
return result;
}
/**
* Encrypts message.
* @param plaintext plaintext
* @param ciphertext ciphertext
* @param message message
* @return message
*/
private static String encrypt(char[] plaintext, char[] ciphertext, String message) {
String result = “”;
for (int i = 0; i < message.length(); i++) {
boolean uppercase = false;
char ch = message.charAt(i);
if (ch >= ‘A’ && ch <= ‘Z’) {
ch = Character.toLowerCase(ch);
uppercase = true;
}
int index = getIndex(ch, plaintext);
if (index != -1) {
char convertedCh = ciphertext[index];
if (uppercase) {
convertedCh = Character.toUpperCase(convertedCh);
}
result += convertedCh;
} else {
result += ch;
}
}
return result;
}
/**
* Gets index of given character in plaintext.
* @param ch ch
* @param plaintext plaintext
* @return index of given character in plaintext
*/
private static int getIndex(char ch, char[] plaintext) {
int index = -1;
for (int i = 0; i < plaintext.length; i++) {
if (plaintext[i] == ch) {
index = i;
break;
}
}
return index;
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
/**
* Class represents decrypt.
*/
public class Decrypt {
private static final Scanner SCANNER = new Scanner(System.in);
public static void main(String[] args) throws IOException {
char[] plaintext = new char[26];
int i = 0;
// create plaintext
for (char ch = ‘a’; ch <= ‘z’; ch++) {
plaintext[i] = ch;
i++;
}
System.out.println(“Enter the keyword:”);
String keyword = SCANNER.nextLine().toLowerCase();
// get ciphertext
char[] ciphertext = createCiphertext(plaintext, keyword);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter the message (end with a blank line): “);
String message = “”;
String curr;
while (!(curr = bufferedReader.readLine()).equals(“”)) {
message += curr;
}
// get decrypted message
String decrypted = decrypt(plaintext, ciphertext, message);
System.out.println(decrypted);
}
/**
* Creates ciphertext.
*
* @param plaintext plaintext
* @param keyword keyword
* @return ciphertext
*/
private static char[] createCiphertext(char[] plaintext, String keyword) {
char[] ciphertext = new char[26];
int count = 0;
// copy chars from keyword ignoring existing chars
for (int j = 0; j < keyword.length(); j++) {
if (!exists(ciphertext, keyword.charAt(j), j)) {
ciphertext[count] = keyword.charAt(j);
count++;
}
}
// copy left chars from plaintext
for (int k = 0; k < plaintext.length; k++) {
if (!exists(ciphertext, plaintext[k], count)) {
ciphertext[count] = plaintext[k];
count++;
}
}
return ciphertext;
}
/**
* Checks if given char presents in array
*
* @param arr array
* @param ch ch
* @param j index
* @return true, if char presents, false – otherwise
*/
private static boolean exists(char[] arr, char ch, int j) {
boolean result = false;
for (int k = 0; k < j; k++) {
if (arr[k] == ch) {
result = true;
break;
}
}
return result;
}
/**
* Decrypts message.
*
* @param plaintext plaintext
* @param ciphertext ciphertext
* @param message message
* @return message
*/
private static String decrypt(char[] plaintext, char[] ciphertext, String message) {
String result = “”;
for (int i = 0; i < message.length(); i++) {
boolean uppercase = false;
char ch = message.charAt(i);
if (ch >= ‘A’ && ch <= ‘Z’) {
ch = Character.toLowerCase(ch);
uppercase = true;
}
int index = getIndex(ch, ciphertext);
if (index != -1) {
char convertedCh = plaintext[index];
if (uppercase) {
convertedCh = Character.toUpperCase(convertedCh);
}
result += convertedCh;
} else {
result += ch;
}
}
return result;
}
/**
* Gets index of given character in plaintext.
*
* @param ch ch
* @param plaintext plaintext
* @return index of given character in plaintext
*/
private static int getIndex(char ch, char[] plaintext) {
int index = -1;
for (int i = 0; i < plaintext.length; i++) {
if (plaintext[i] == ch) {
index = i;
break;
}
}
return index;
}
}