In: Computer Science
This is a java assignment inspired by Caesar algorithm with some minor modifications. You are required to take in three inputs; a string, which is the message that you want to encrypt, an integer number that we call it salt, which is going to salt your plain message to help create a better encrypted message and an int to be used for displacement of the letters.
Your algorithm is going to encode the message letter by letter. If the position of the letter in your message is odd, then it should add the value of this letter to the displacement value plus salt. If the letter is in even position, then it should be added to the displacement subtracted from salt. For example, assume displacement = 3 and salt = 5, then letter ‘A’ will be replaced by ‘I’, if it is in odd position and by ‘C’ if it is in even position.
The message that you read can contain any character that is typeable (i.e. you can see the typable characters on your keyboard). Your program finally should output the encrypted message.
Sample input and corresponding output:
Input:
Let's meet somewhere cool. How about Tuesday at 9:30 in Chemistry 0400?
5
3
Output:
Jmr/q(kmc|_{muc•fmpm_kmwj6_Pm•_i`ws|_\smql_?_ir(7B18_ql(Apcu
g{rzw(.<.8=
JAVA JAVA
Given below is the code for the question. Please do rate it if it helped. Thank you.
Encrypt.java
------
import java.util.Scanner;
public class Encrypt {
public static String encrypt(String msg, int
displacement, int salt) {
String s = "";
for(int i = 0; i < msg.length();
i++) {
char c =
msg.charAt(i);
if(i % 2 == 1)
// odd position
{
c = (char)(c + displacement + salt);
}
else {
c = (char)(c + displacement - salt);
}
s += c;
}
return s;
}
public static void main(String[] args) {
Scanner scnr = new
Scanner(System.in);
String msg;
int salt, displacement;
System.out.println("Enter a message
to be encrypted");
msg = scnr.nextLine();
System.out.print("Enter salt:
");
salt = scnr.nextInt();
System.out.print("Enter
displacement: ");
displacement =
scnr.nextInt();
System.out.println("The encrypted
message is ");
System.out.println(encrypt(msg,
displacement, salt));
}
}
