In: Computer Science
Write a program names EncryptDecrypt.java that has the following menu choices: Print menu and allow user to choose options. The program must have a file dialogue box for text file. Output should be based on user choices.
Your encryption program should work like a filter, reading the contents of one file modifying the data into a code, and then writing the coded contents out to a second file. Do not hard code the path location! Use a simple one from the shift category encryption technique. Do not use a serialized file use a text file.
Screenshot
Program
/**
* Program to read and write file and encrypt decrypt data
* Here user get options for different operations
* Read user specified data encode or decode as per the
requirements
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class EncryptDecrypt {
public static void main(String[] args) {
//Variable for data
String
data="",encrypt="",decrypt="";
//User options
int opt=menu();
//Loop until exit
while(opt!=9) {
//Read a file
specified by user and store into data
if(opt==1)
{
data=readFile();
}
//Print data
value
else if(opt==2)
{
printData(data);
}
//Encrypt data
value and display encrypted on console
else if(opt==3)
{
encrypt=encryptData(data);
System.out.println("\nEncrypted Data: ");
printData(encrypt);
}
//Write
encrypted data into user specified file
else if(opt==4)
{
writeEncrypt(encrypt);
System.out.println("\nSuccessfully wrote
encrypted data!!");
}
//Clear
data
else if(opt==5)
{
data=null;
encrypt=null;
decrypt=null;
System.out.println("\nClear memory of data
value!!!");
}
//Read a file
data for decryption
else if(opt==6)
{
data=readFile();
}
//Decrypt
else if(opt==7)
{
decrypt=decryptData(data);
System.out.println("\nDecryption
completed");
}
//Display
decrypted information
else if(opt==8)
{
System.out.println("\nDecrypted data: ");
printData(decrypt);
}
opt=menu();
}
System.out.println("\nExiting
Program............");
}
//Get menu
//Show options and prompt for input
//Error check and return opt
public static int menu() {
Scanner sc=new
Scanner(System.in);
System.out.println("USER
OPTIONS:");
System.out.println("\r\n" +
"1. Read in a file\r\n" +
"2. Print the file to the console\r\n" +
"3. Encrypt the file and write it to the
console\r\n" +
"4. Write out the encrypted file to a text
file\r\n" +
"5. Clear the data in memory\r\n" +
"6. Read in an encrypted file\r\n"
+
"7. Decrypt the file\r\n" +
"8. Write out the decrypted file to
the console\r\n" +
"9. End\r\n" +
"");
System.out.println("Enter your
choice: ");
int opt=sc.nextInt();
while(opt<1 || opt>9) {
System.out.println("Error!!!Option should be 1-9.Pease
re-enter");
System.out.println("Enter your choice: ");
opt=sc.nextInt();
}
sc.nextLine();
return opt;
}
//Read data from file and return string of data
public static String readFile() {
Scanner sc=new
Scanner(System.in);
StringBuffer data=new
StringBuffer();
System.out.println("\nEnter file
name to read: ");
String
filename=sc.nextLine();
try {
Scanner sc1=new
Scanner(new File(filename));
while(sc1.hasNextLine()) {
data.append(sc1.nextLine()+"\n");
}
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
return data.toString();
}
//Display passed data on console
public static void printData(String str) {
System.out.println("\n"+str);
}
//Encrypt data using simple shift add method
//Check each character , if alphebet add with shift
value
//Here i take fixed shift value
public static String encryptData(String data) {
char ch;
int shift =5;
String encrypt =
"";
for(int i=0; i <
data.length();i++)
{
ch = data.charAt(i);
if(ch >= 'a' && ch <= 'z')
{
ch = (char) (ch + shift);
if(ch> 'z') {
ch = (char) (ch+'a'-'z'-1);
}
encrypt = encrypt + ch;
}
else if(ch >= 'A' && ch <= 'Z') {
ch = (char) (ch + shift);
if(ch > 'Z') {
ch = (char) (ch+'A'-'Z'-1);
}
encrypt = encrypt + ch;
}
else {
encrypt = encrypt +ch;
}
}
return encrypt;
}
//Write encrypted data into user specified file
public static void writeEncrypt(String data) {
Scanner sc=new
Scanner(System.in);
System.out.println("\nEnter file
name to write encrypted data: ");
String
filename=sc.nextLine();
try {
FileWriter
fw=new FileWriter(filename);
fw.write(data);
fw.close();
} catch (IOException e) {
// TODO
Auto-generated catch block
e.printStackTrace();
}
}
//Decrypt data using simple shift sub method
//Check each character , if alphebet subtract with
shift value
//Here i take fixed shift value
public static String decryptData(String data) {
char ch;
int shift =5;
String decrypt =
"";
for(int i=0; i <
data.length();i++)
{
ch = data.charAt(i);
if(ch >= 'a' && ch <= 'z')
{
ch = (char) (ch - shift);
if(ch < 'a') {
ch = (char) (ch-'a'+'z'+1);
}
decrypt= decrypt + ch;
}
else if(ch >= 'A' && ch <= 'Z')
{
ch = (char) (ch - shift);
if (ch < 'A') {
ch = (char) (ch-'A'+'Z'+1);
}
decrypt= decrypt
+ch;
}
else
{
decrypt = decrypt
+ch;
}
}
return decrypt;
}
}
---------------------------------------------------
Output
USER OPTIONS:
1. Read in a file
2. Print the file to the console
3. Encrypt the file and write it to the console
4. Write out the encrypted file to a text file
5. Clear the data in memory
6. Read in an encrypted file
7. Decrypt the file
8. Write out the decrypted file to the console
9. End
Enter your choice:
1
Enter file name to read:
datafile.txt
USER OPTIONS:
1. Read in a file
2. Print the file to the console
3. Encrypt the file and write it to the console
4. Write out the encrypted file to a text file
5. Clear the data in memory
6. Read in an encrypted file
7. Decrypt the file
8. Write out the decrypted file to the console
9. End
Enter your choice:
2
Your encryption program should work like a filter, reading the
contents of one file modifying
the data into a code, and then writing the coded contents out to a
second file.
USER OPTIONS:
1. Read in a file
2. Print the file to the console
3. Encrypt the file and write it to the console
4. Write out the encrypted file to a text file
5. Clear the data in memory
6. Read in an encrypted file
7. Decrypt the file
8. Write out the decrypted file to the console
9. End
Enter your choice:
3
Encrypted Data:
Dtzw jshwduynts uwtlwfr xmtzqi btwp qnpj f knqyjw, wjfinsl ymj
htsyjsyx tk tsj knqj rtinkdnsl
ymj ifyf nsyt f htij, fsi ymjs bwnynsl ymj htiji htsyjsyx tzy yt f
xjhtsi knqj.
USER OPTIONS:
1. Read in a file
2. Print the file to the console
3. Encrypt the file and write it to the console
4. Write out the encrypted file to a text file
5. Clear the data in memory
6. Read in an encrypted file
7. Decrypt the file
8. Write out the decrypted file to the console
9. End
Enter your choice:
4
Enter file name to write encrypted data:
encryptedfile.txt
Successfully wrote encrypted data!!
USER OPTIONS:
1. Read in a file
2. Print the file to the console
3. Encrypt the file and write it to the console
4. Write out the encrypted file to a text file
5. Clear the data in memory
6. Read in an encrypted file
7. Decrypt the file
8. Write out the decrypted file to the console
9. End
Enter your choice:
5
Clear memory of data value!!!
USER OPTIONS:
1. Read in a file
2. Print the file to the console
3. Encrypt the file and write it to the console
4. Write out the encrypted file to a text file
5. Clear the data in memory
6. Read in an encrypted file
7. Decrypt the file
8. Write out the decrypted file to the console
9. End
Enter your choice:
2
null
USER OPTIONS:
1. Read in a file
2. Print the file to the console
3. Encrypt the file and write it to the console
4. Write out the encrypted file to a text file
5. Clear the data in memory
6. Read in an encrypted file
7. Decrypt the file
8. Write out the decrypted file to the console
9. End
Enter your choice:
6
Enter file name to read:
encryptedfile.txt
USER OPTIONS:
1. Read in a file
2. Print the file to the console
3. Encrypt the file and write it to the console
4. Write out the encrypted file to a text file
5. Clear the data in memory
6. Read in an encrypted file
7. Decrypt the file
8. Write out the decrypted file to the console
9. End
Enter your choice:
7
Decryption completed
USER OPTIONS:
1. Read in a file
2. Print the file to the console
3. Encrypt the file and write it to the console
4. Write out the encrypted file to a text file
5. Clear the data in memory
6. Read in an encrypted file
7. Decrypt the file
8. Write out the decrypted file to the console
9. End
Enter your choice:
8
Decrypted data:
Your encryption program should work like a filter, reading the
contents of one file modifying
the data into a code, and then writing the coded contents out to a
second file.
USER OPTIONS:
1. Read in a file
2. Print the file to the console
3. Encrypt the file and write it to the console
4. Write out the encrypted file to a text file
5. Clear the data in memory
6. Read in an encrypted file
7. Decrypt the file
8. Write out the decrypted file to the console
9. End
Enter your choice:
9
Exiting Program............
------------------------------------------------------
Note:-
I assume you are expecting this way