Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Imagine you are an agent and for some secret government agency. You work togethe

ID: 3804271 • Letter: I

Question

Imagine you are an agent and for some secret government agency. You work together with a colleague who is a double agent and has infiltrated some underground organization. You need to get a message to him, but you cannot send him a clear text message, since someone might intercept your message and uncover his true identity. You have to encrypt the message, and send it to him. For this purpose, you're going to do the following: First you will read the encryption prime number limit and the secret message from the text file that is given to you by the Agency. The text file is called: SecretMessage.txt The first line contains an integer (the prime number limit). The second line contains the secret message you have to encrypt. Then, you will write a function called getEncryptionPrime. This function takes an integer as parameter, and returns to you the highest prime number between zero and the prime number limit (for example, if the prime number limit is 100, this function will return 97. Now you will encrypt the message For this purpose, write a function called encrypt (string original string, int prime) This function looks at every character of the string it receives (which contains the secret message), and add to the characters integer value the calculated prime number. Put the resulting characters together in a new String to assemble the encrypted message, and output the encrypted message to the console like: "The encrypted message is: " Let the function return the encrypted string. Finally, write a function called decrypt Message that takes a string and an integer as function parameters. This function is responsible to decrypt the message back to its readable content. Call this function from your main function, give it the encrypted message and the prime number to reverse the encryption, and output the original message from this function to the console like: The decrypted message is; " Example output (not the actual message): The encrypted message is: The decrypted message is: The meeting takes place at midnight.

Explanation / Answer

*Strore the name of the text file with temp.txt

*Calculate the largest prime number of primeL before sending it as secret key

Here goes the required code here

Code:

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;

public class HelloWorld{

private static SecretKeySpec secretKey;
private static byte[] key;

public static void setKey(String myKey)
{
MessageDigest sha = null;
try {
key = myKey.getBytes("UTF-8");
sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key, 16);
secretKey = new SecretKeySpec(key, "AES");
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}

public static String encrypt(String strToEncrypt, String secret)
{
try
{
setKey(secret);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
}
catch (Exception e)
{
System.out.println("Error while encrypting: " + e.toString());
}
return null;
}

public static String decrypt(String strToDecrypt, String secret)
{
try
{
setKey(secret);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
}
catch (Exception e)
{
System.out.println("Error while decrypting: " + e.toString());
}
return null;
}


public static void main(String []args){
String fileName = "temp.txt";
String originalString=new String();
int primeL;
try{
FileReader fileReader =new FileReader(fileName);

BufferedReader bufferedReader = new BufferedReader(fileReader);
String primeLine=bufferedReader.readLine();
primeL=Integer.parseInt(primeLine);
originalString =bufferedReader.readLine();
}
catch(IOException e){
  
}
  
HelloWorld hw=new HelloWorld();
String encryptS = hw.encrypt(originalString, primeL) ;
String decryptS = hw.decrypt(encryptS, primeL) ;

System.out.println("The encrypted message is: "+encryptS);
System.out.println("The decrypted message is: "+decryptS);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote