Secret Channels A company uses public Internet to carry its phone service. The v
ID: 3845042 • Letter: S
Question
Secret Channels
A company uses public Internet to carry its phone service. The voice data will be encrypted before sending to Internet. The encryption algorithm is as follows: For each four-digit decimal integer, (1) Add 5 to each digit, divide the sum by 10 and use the remainder to replace the digit, (2) Swap 1st-digit with 4th-digit, (3) Swap 2nd-digit with 3rd-digit. For this assignment, write a program named Encrypted.java to perform the following tasks:
1. Implement a static method as specified below: public static int encrypt(int data) - this method takes voice data and return encrypted data by applying the encryption algorithm 2. Implement the main method, which reads data from the provided data file, then use the encrypt method to encrypt the voice data and print the encrypted voice data.
Input:
A series of lines. Each line is a positive four-digit decimal integer.
Output:
In each line, print the encrypted four-digit decimal integer.
Sample Input (sample data file is provided for your testing purpose):
0123
5890
9999
Sample Output
8765
5430
4444
Explanation / Answer
import java.io.*;
import java.lang.*;
class Encrypted
{
public static String encrypt(int data)
{
int a[] = new int[4];
int value = 0;
int i=0,t=3;
while(i != 4)
{
value = data % 10 ;
if((value + 5) > 9)
{
value = value - 10;
}
a[i] = (value + 5) * (int) (Math.pow(10,t)) ;
i++;
t--;
data = data / 10;
}
for(i=0;i<4;i++)
{
data = data + a[i];
}
String string_data = String.valueOf(data);
return string_data;
}
public static void main(String args[]) throws IOException
{
try
{
FileReader fr = new FileReader("voiceData.txt");
BufferedReader br = new BufferedReader(fr);
String s = null;
while((s = br.readLine()) != null) {
String n = encrypt(Integer.parseInt(s));
System.out.println(n);
}
}
catch (Exception e)
{
System.out.println(e);
}
//fr.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.