Question
Java Caeser Cipher A reporter needs a security application that will encrypt their phone numbers in their contact list to protect their sources. The client
Java Caeser Cipher
A reporter needs a security application that will encrypt their phone numbers in their contact list to protect their sources. The client program will provide a three digit encoding number that will be used to encode the separate parts of a phone number: area code, exchange and subscriber number. The first digit will be used to encode the 3 digit area code. The second digit will be used to encode the 3 digit exchange. The third digit will be used to encode the 4 digit subscriber number.
Use a simple encryption algorithm to make this easier. The Caesar cipher can be represented asC = (P + key) mod 10, whereCis the encrypted value andPis the value being encrypted. Thekeyis the encoding key derived from the security code that is provided by the client.
The corresponding deciphering formula to return the encoded phone number to its original state is:
P = (C - key) mod 10.
In Java, this might look like this (assuming variables previously defined as integers for p, c and key):
p = (c - key) % 10;
if(p p= p + 10;
The if condition is necessary in Java because of the way the modulus operator (%) handles negative numbers. When (C-key) produces a negative number, the Java modulus operator returns that negative number. To counteract that, add 10 to the result.
For more information and variations on this cipher, check out modulo encryption.(optional)
In Java, the%is the modulus operator, so you could use it in combination with the addition or subtraction operator for the variables, like this:
C =(P + key) % 10
P =(C - key) % 10 //use an if condition next to test for a negative result
if(P
P= P + 10;
Use this cipher for each individual digit in the phone number. For example, the client provides this security code: 135. The security code is broken down to three encoding keys: 1 (for the area code) 3 (for the exchange) 5 (for the subscriber number). We will use an example phone number, such as 707-123-4567.
- The area code will be encoded using the Caesar cipher as follows, using the first digit in the security code (1) as the encoding key on each of the numbers in the area code: 7, 0, 7:
- C = (P + code) mod 10.
- 8 = (7 + 1) % 10
- 1 = (0 + 1) % 10
- 8 = (7 + 1) % 10
- The exchange will be encoded using the Caesar cipher as follows, using the second digit in the security code (3) as the encoding key on each of the numbers in the exchange:1, 2, 3:
- C = (P + code) mod 10.
- 4 = (1 + 3) % 10
- 5 = (2 + 3) % 10
- 6 = (3 + 3) % 10
- The subscriber number will be encoded using the Caesar cipher as follows, using the third digit in the security code (5) as the encoding key on each of the numbers in the subscriber number: 4, 5, 6, 7:
- C = (P + code) mod 10.
- 9 = (4 + 5) % 10
- 0 = (5 + 5) % 10
- 1 = (6 + 5) % 10
- 2 = (7 + 5) % 10
The resulting phone number is therefore 818-456-9012.
- Create a separate class that contains the following methods (5 points)Note:This class should be separate and apart from the public class that holds the main method. You can either make a class that isnota public class or you could put it in a separate .java file:
- A method that will accepta parameteras an integer arrayfor the phone numberanda parameter for the security code(you choose the data type for the security code). Use one (or more) repetition structures to replace each number in the array with its encrypted version usingC = (P + key) mod 10with the appropriate digit in the security code as described above. Note: You should make the replacementin the original array. (5 points)
- Create a method that will accept an encrypted phone numberas an integer arrayand the decoding key. Use one (or more) repetition structures to return it to its plain text (decrypted) version usingP = (C - key) mod 10 and the conditional:
- if(P P= P + 10;
- Note: You should make the replacementin the original array. (5 points)
- Call the methods of this class in the main method with at least one sample phone number. Display the phone number in main before and after calling each method (5 points).
Important Note:You can get the phone number from the user at the console or with a GUI if you prefer to do that, but it is not required. You can put the phone number in the code when you create the array instead if you would rather do it that way. Regardless of how you get the phone number, it must be put in an array and the array must be passed to another method that accepts an array as a parameter. You shouldnotget the phone number and process it in the same method.
What I have so far
package hidingnumbers; import java.util.Arrays; public class HidingNumbers { public static void main(String[] args) { int[] area = {7,0,7}; int[] exchange = {1,2,3}; int[] subscriber = {4,5,6,7}; instructions(); System.out.println(\"Our security code to encrypt the phone number would be 1,3,5\"); System.out.print(\"A given phone number would be \"); System.out.print(Arrays.toString(area)); System.out.print(Arrays.toString(exchange)); System.out.println(Arrays.toString(subscriber)); myClass hiding = new myClass(); hiding.encrypt(area); hiding.encrypt(exchange); hiding.encrypt(subscriber); System.out.print(\"This would be the number after encryption\"); System.out.print(Arrays.toString(area)); System.out.print(Arrays.toString(exchange)); System.out.println(Arrays.toString(subscriber)); hiding.decrypt(area); hiding.decrypt(exchange); hiding.decrypt(subscriber); } public static void instructions() { System.out.print(\"This program will show an example of an encrypted \"); System.out.println(\"phone number to help the reporter\"); } }
class myClass {
public void encrypt(int x[]) { for(int counter=0; counter x[counter]+=5; } public void decrypt(int x[]) { } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started