Question

In: Computer Science

Desc: Encrypts or decrypts a file. Input: The user supplies the character '1' to encrypt, or...

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.

  1. 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.

  1. 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");

}

}

-----------------------------------------------------------------------------------------------------------------------

  1. 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.

  1. 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.

  1. 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);

}

-----------------------------------------------------------------------------------------------------------------------

  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

Solutions

Expert Solution

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();

        }


}

Related Solutions

Write a program that encrypts and decrypts the user input. Note – Your input should be...
Write a program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user For Encryption Process: Take the string and reverse the string. Encrypt the reverse string with each character replaced with distance value (x) given by...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user For Encryption Process: Take the string and reverse the string. Encrypt the reverse string with each character replaced with distance value (x)...
Write a program in javascript to encrypt and decrypt the user input using the caesar algorithm...
Write a program in javascript to encrypt and decrypt the user input using the caesar algorithm with the substitution algorithm. Specify the min and max of the message user can enter to encrypt. Specify the length of the key user can enter to encrypt and decrypt the message. document your design by words or diagrams.
How To Print Nice Padding Character in a String in C++? 1. User input the width...
How To Print Nice Padding Character in a String in C++? 1. User input the width = 14 2. User input the padding character = + Example output: ++Hello World++ Thanks in advance.
Using Python, write a simple application that takes user input of plaintext and key, and encrypt...
Using Python, write a simple application that takes user input of plaintext and key, and encrypt the plaintext with Vigenere Cipher. The application should then print out the plaintext and the ciphertext.
You are to write a short program asking the user to input a single character. Specifically,...
You are to write a short program asking the user to input a single character. Specifically, your prompt should be to ask the user to input a vowel - A, E, I , O or U. (We will not use Y ).       Your program should then determine if they did indeed type in a vowel (upper or lower case is acceptable for user input for the vowel). You should use data type char   for input! If they did, you print...
Question 1: Write a Java program that prompts the user to input a file name (existing...
Question 1: Write a Java program that prompts the user to input a file name (existing text file), then calculate and display the numbers of lines in that file. Also calculate and display the length of the longest line in that file. For example, if the input file has the following lines: Hello This is the longest line Bye The output should be: The file has 3 lines. The longest line is line 2 and it has 24 characters. Test...
Write an algorithm (flowchart OR Pseudocode) for the following problem Take input character from the user...
Write an algorithm (flowchart OR Pseudocode) for the following problem Take input character from the user unless he enters '$'. Thereafter display how may vowels were entered by the user. Also display the number of each vowel ('a', 'e', 'i', 'o' and 'u') separately. For example if the user enters B a b e c o o d i u g o a l $ Then we have the output below: #A=2 #E=1 #I=1 #O=3 #U=2
Write program#1 upload .java file. #1 Write a java program that prompts the user for input...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input using the Scanner class. First to enter their first name. Then prompts the user to enter their last name. Then prompts the user to enter their city. Then prompts the user to enter their state. Then prompts the user to enter their zip code. Concatenate first name and last name into full_name. Using String Class is optional. Use the String Class to replace zip...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input using the Scanner class. First to enter their first name. Then prompts the user to enter their last name. Then prompts the user to enter their city. Then prompts the user to enter their state. Then prompts the user to enter their zip code.   Concatenate first name and last name into full_name. Using String Class is optional. Use the String Class to replace zip...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT