In: Computer Science
Desc: Encrypts or decrypts a file.
Input: The user supplies the character '1' to encrypt, or '2' to decrypt via the keyboard.
The user also supplies the name of the source file via the keyboard.
Output: If the user wants to encrypt, the text in input file is encrypted and the encrypted text is
stored in "encrypted.txt". The original file is not changed.
If the user wants to decrypt, the text in input file is decrypted and the decrypted text is
stored in "decrypted.txt". The original file is not changed.
-----------------------------------------------------------------------------------------------------------------------
Figure 18-1. Specifications for the Encryption case study.
Architectural design
-----------------------------------------------------------------------------------------------------------------------
print("1. Encrypt a file")
print("2. Decrypt a file")
choice=readChar()
switch (choice)
{
case '1': encrypt(); break
case '2': decrypt(); break
}
-----------------------------------------------------------------------------------------------------------------------
Figure 18-2. Pseudocode design of the main method for the Encryption case study.
Implementation
-----------------------------------------------------------------------------------------------------------------------
import java.util.Scanner;
class Encryption
{
public static void main(String[] args)
{
Scanner keyboard=new Scanner(System.in);
char choice='1';
System.out.println("1. Encrypt a file");
System.out.println("2. Decrypt a file");
choice=keyboard.nextLine().charAt(0);
switch (choice)
{
case '1': encrypt(); break;
case '2': decrypt(); break;
}
}
public static void encrypt()
{
System.out.println("Method encrypt: to be completed");
}
public static void decrypt()
{
System.out.println("Method decrypt: to be completed");
}
}
-----------------------------------------------------------------------------------------------------------------------
Detailed design
-----------------------------------------------------------------------------------------------------------------------
//Desc : Encrypts a file.
//Input: The user supplies the name of a disk file via the keyboard and the file must exist.
//Output: The text in the specified file encrypted and written to "encrypted.txt".
-----------------------------------------------------------------------------------------------------------------------
Figure 18-4. The specifications for encrypt.
-----------------------------------------------------------------------------------------------------------------------
print("Enter file name")
s=readLine()
f=open(s)
g=open("encrypted.txt")
while (f.hasChar())
{
ch = f.readChar()
ch=convert(ch)
g.print(ch)
}
f.close()
g.close()
-----------------------------------------------------------------------------------------------------------------------
Figure 18-5. The pseudocode design for encrypt.
Implementation again
-----------------------------------------------------------------------------------------------------------------------
public static void encrypt() throws FileNotFoundException
{
Scanner keyboard=new Scanner(System.in);
System.out.print("Enter file name: ");
String s=keyboard.nextLine();
Scanner input = new Scanner(new File(s));
input.useDelimiter(""); //line 14
PrintWriter f = new PrintWriter("encrypted.txt");
while (input.hasNext())
{
char ch = input.next().charAt(0);
ch=convert(ch);
f.print(ch);
}
input.close();
f.close();
}
-----------------------------------------------------------------------------------------------------------------------
Figure 18-6. The code for encrypt.
Testing again
public static char convert (char ch)
{
return 'a';
}
-----------------------------------------------------------------------------------------------------------------------
//Desc : Convert a character to another secret character.
//Return: The predecessor of ch in the Unicode set.
public static char convert (char ch)
{
return (char)(ch-1);
}
-----------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------
//Desc : Decrypts a file.
//Input: The user supplies the name of a disk file via the keyboard and the file must exist.
//Output: The text in the specified file decrypted and written to "decrypted.txt".
//Throw: FileNotFoundException if the input source file does not exist, or decrypted.txt cannot be created
public static void decrypt() throws FileNotFoundException
{
Scanner keyboard=new Scanner(System.in);
char ch='a';
System.out.print("Enter file name: ");
String s=keyboard.nextLine();
Scanner input = new Scanner(new File(s));
input.useDelimiter("");
PrintWriter f = new PrintWriter("decrypted.txt");
while (input.hasNext())
{
ch = input.next().charAt(0);
ch=inverseConvert(ch);
f.print(ch);
}
input.close();
f.close();
}
//Desc : Convert a character to another character.
//Return: The successor of ch in the Unicode set.
public static char inverseConvert(char ch)
{
return (char)(ch+1);
}
-----------------------------------------------------------------------------------------------------------------------
Modify the Encryption program so that it uses the following encryption algorithm:
Every letter (both uppercase and lowercase) converted to its successor except z and Z, which are converted to 'a' and 'A' respectively (i.e., a to b, b to c, …, y to z, z to a, A to B, B to C, …, Y to Z, Z to A)
Every digit converted to its predecessor except 0, which is converted to 9 (i.e., 9 to 8, 8 to 7, … 1 to 0, 0 to 9)
Everything else unchanged.
Note:
You must modify the specifications (API) of the methods involved as well.
Hand in:
Encryption.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
class Encryption
{
public static void main(String[] args) throws FileNotFoundException
{
Scanner keyboard=new Scanner(System.in);
char choice='1';
System.out.println("1. Encrypt a file");
System.out.println("2. Decrypt a file");
choice=keyboard.nextLine().charAt(0);
switch (choice)
{
case '1': encrypt(); break;
case '2': decrypt(); break;
}
}
/*public static void encrypt()
{
System.out.println("Method encrypt: to be completed");
System.out.println("Enter file name");
String s=readLine();
f=open(s);
g=open("encrypted.txt")
while (f.hasChar())
{
ch = f.readChar()
ch=convert(ch)
g.print(ch)
}
f.close()
g.close()
//}
*/
/*
public static void decrypt()
{
System.out.println("Method decrypt: to be completed");
}*/
//}
public static void encrypt() throws FileNotFoundException
{
Scanner keyboard=new Scanner(System.in);
System.out.print("Enter file name: ");
String s=keyboard.nextLine();
Scanner input = new Scanner(new File(s));
input.useDelimiter(""); //line 14
PrintWriter f = new PrintWriter("encrypted.txt");
while (input.hasNext())
{
char ch = input.next().charAt(0);
ch=convert(ch);
f.print(ch);
}
input.close();
f.close();
}
/*
public static char convert (char ch)
{
return 'a';
}*/
public static char convert (char ch)
{
if(ch=='z') return 'a';
else if(ch=='Z') return 'A';
else if(ch=='0') return '9';
else if(Character.isDigit(ch)) return (char)(ch-1);
else return (char)(ch + 1);
//return (char)(ch-1);
}
//Desc : Convert a character to another character.
//Return: The successor of ch in the Unicode set.
public static char inverseConvert(char ch)
{
if(ch == 'a') return 'z';
else if(ch == 'A') return 'Z';
else if(ch == '9') return '0';
else if(Character.isDigit(ch)) return (char)(ch+1);
return (char)(ch-1);
}
//Desc : Decrypts a file.
//Input: The user supplies the name of a disk file via the keyboard and the file must exist.
//Output: The text in the specified file decrypted and written to "decrypted.txt".
//Throw: FileNotFoundException if the input source file does not exist, or decrypted.txt cannot be created
public static void decrypt() throws FileNotFoundException
{
Scanner keyboard=new Scanner(System.in);
char ch='a';
System.out.print("Enter file name: ");
String s=keyboard.nextLine();
Scanner input = new Scanner(new File(s));
input.useDelimiter("");
PrintWriter f = new PrintWriter("decrypted.txt");
while (input.hasNext())
{
ch = input.next().charAt(0);
ch=inverseConvert(ch);
f.print(ch);
}
input.close();
f.close();
}
}