Writing an encryption/decryption method in Java, Please help! Introduction: We w
ID: 3863649 • Letter: W
Question
Writing an encryption/decryption method in Java, Please help!
Introduction: We will simulate that you are trying to hide some secret information from the rival companies. You are planning on encrypting the messages you send to the other party using some secret key that you and the receiver know. You will be prompting the user to enter a phrase in English. Your program will randomly generate a secret key and then convert this English phrase to the super secret virtually unbreakable encrypted code based on that secret key. The encrypted code and the secret key will be sent to the receiver who will decrypt that code and understand the secret information! Generating the random secret key: Your secret key should have the following format X1X 1x3x4-zy 2, where x1, x2, x3, and x4 are randomly generated alphabet characters can be upper or lower case 1 and y2 are randomly generated special characters, and z is a randomly generated integer between 2 and 5, inclusive. Encryption: A message will be encrypted as follows: 1. All punctuation marks will be removed from the English phrase 2. The phrase will be converted to lower case 3. The English phrase should be split up to words and each word encrypted separately (but all using the same secret key as follows: 3.1: If the first character of the word is a consonant, move it to the end of the word and then append x1x2. If it is a vowel, do not move the first character but append y1x3x4 to the end 3.2: Insert the special character y2 to the encrypted code at positions that are multiples of z. So if z is 2, insert y2 at the positions 2, 4, 6, 8, all the way to the end of the encrypted code (don't replace the existing characters with y2 at these positions, just insert) Decryption: The receiver should receive the encrypted code and the secret keyExplanation / Answer
PROGRAM CODE:
Message.java
package cipher;
import java.util.Random;
public class Message {
private Random rand;
private static String SPECIAL_CHARACTERS = ";\/`~!@#$%^&*()-_+=][}{'":.,<>?";
private static String ALPHABETS = "abcdefghijklmnopqrstuvwxyz";
private static String VOWELS = "aeiou";
private char x1, x2, x3, x4, y1, y2;
private int z;
public String key;
public String text;
protected void generateSecretKey()
{
rand = new Random();
x1 = ALPHABETS.charAt(rand.nextInt(26));
x2 = ALPHABETS.charAt(rand.nextInt(26));
x3 = ALPHABETS.charAt(rand.nextInt(26));
x4 = ALPHABETS.charAt(rand.nextInt(26));
y1 = SPECIAL_CHARACTERS.charAt(rand.nextInt(31));
y2 = SPECIAL_CHARACTERS.charAt(rand.nextInt(31));
z = rand.nextInt(4) + 2;
key = String.valueOf(x1)+String.valueOf(x2)+"-"+y1+x3+x4+"-"+z+y2;
}
protected void readText(String msg)
{
text = msg;
}
protected String encryptText()
{
String characters = "";
String encryptedMessage = "";
//removing the punctuation marks
for(int i=0; i<text.length(); i++)
{
if(Character.isLetter(text.charAt(i)) || Character.isDigit(text.charAt(i)) || text.charAt(i) == ' ' || text.charAt(i) == ' ')
if(text.charAt(i) != ' ')
characters += text.charAt(i);
else characters +=" ";
}
//converting to lowercase
characters = characters.toLowerCase();
String words[] = characters.split(" ");
for(int x=0; x<words.length; x++)
{
if(VOWELS.contains(String.valueOf(words[x].charAt(0))))
{
encryptedMessage += words[x] + y1 + x3 + x4 + " ";
}
else
encryptedMessage += words[x].substring(1) + words[x].charAt(0) + x1 + x2 + " ";
}
characters = "";
System.out.println("After the steps 1, 2 and 3.1:");
System.out.println(encryptedMessage);
for(int i=0; i<encryptedMessage.length(); i++)
{
if((i+1)%z == 0)
characters += y2;
characters += encryptedMessage.charAt(i);
}
System.out.println(" After step 3.2 (adding the special character):");
System.out.println(characters);
return characters;
}
protected boolean verifySecretKey(String key)
{
if(key.length() == 9 && key.contains("-"))
{
try
{
x1 = key.charAt(0);
x2 = key.charAt(1);
x3 = key.charAt(4);
x4 = key.charAt(5);
y1 = key.charAt(3);
y2 = key.charAt(8);
z = key.charAt(7);
}catch (Exception e) {
return false;
}
}
else return false;
return true;
}
protected String decryptCode(String encryptedMessage)
{
String decryptedMessage = "";
if(!verifySecretKey(key))
return null;
while(encryptedMessage.contains(String.valueOf(y2)))
encryptedMessage = encryptedMessage.replace(String.valueOf(y2), "");
String words[] = encryptedMessage.split(" ");
for(int i=0; i<words.length; i++)
{
if(words[i].endsWith(String.valueOf(x1)+String.valueOf(x2)))
{
words[i] = words[i].substring(0, words[i].length()-2);
decryptedMessage += words[i].charAt(words[i].length()-1);
decryptedMessage += words[i].substring(0, words[i].length()-1) + " ";
}
else
{
words[i] = words[i].substring(0, words[i].length()-3);
decryptedMessage += words[i] + " ";
}
}
return decryptedMessage;
}
}
Sender.java
package cipher;
import java.util.Scanner;
public class Sender extends Message{
private String message;
private String encryptedMessage;
public Sender() {
message = "";
encryptedMessage = "";
}
public void getMessageFromUser()
{
System.out.print("English phrase: ");
Scanner reader = new Scanner(System.in);
message = reader.nextLine();
}
public void encrypt()
{
generateSecretKey();
System.out.println("Sample secret key (randomly generated): " + key);
readText(message);
System.out.println(" Encrypting the English phrase: ");
encryptedMessage = encryptText();
}
public String getEncryptedMessage()
{
return encryptedMessage;
}
public String getKey()
{
return key;
}
}
Receiver.java
package cipher;
public class Receiver extends Message{
private String encryptedMessage;
private String decryptedMessage;
public Receiver(String msg, String keyFromEncryption) {
encryptedMessage = msg;
key = keyFromEncryption;
}
public void decryptMessage()
{
System.out.println(" Decrypting code: ");
decryptedMessage = decryptCode(encryptedMessage);
System.out.println(decryptedMessage);
}
}
EncryptorDecryptorTester.java
package cipher;
public class EncryptorDecryptorTester {
public static void main(String[] args) {
Sender sender = new Sender();
sender.getMessageFromUser();
sender.encrypt();
String message = sender.getEncryptedMessage();
String key = sender.getKey();
Receiver receiver = new Receiver(message, key);
receiver.decryptMessage();
}
}
OUTPUT:
English phrase: Hi! How are you?
Sample secret key (randomly generated): jq-!fa-4#
Encrypting the English phrase:
After the steps 1, 2 and 3.1:
ihjq owhjq are!fa ouyjq
After step 3.2 (adding the special character):
ihj#q ow#hjq #are!#fa o#uyjq#
Decrypting code:
hi how are you
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.