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)...
Create a Columnar Transposition Cipher in Python that encrypts/decrypts the word "Crypt".
Create a Columnar Transposition Cipher in Python that encrypts/decrypts the word "Crypt".
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.
Programing Language: Java The Problem: You are writing a program that encrypts or decrypts messages using...
Programing Language: Java The Problem: You are writing a program that encrypts or decrypts messages using a simple substitution cipher. Your program will use two constant strings. One will represent the code for encryption: going from the original message (called the plaintext) to the encrypted version of the message. The other will be “abcdefghijklmnopqrstuvwxyz” (the lowercase alphabet. Your program will ask the user whether they want to 1) encrypt a message, 2) decrypt a message, or 3) quit. If they...
You are writing a program that encrypts or decrypts messages using a simple substitution cipher. Your...
You are writing a program that encrypts or decrypts messages using a simple substitution cipher. Your program will use two constant strings. One will represent the code for encryption: going from the original message (called the plaintext) to the encrypted version of the message. The other will be “abcdefghijklmnopqrstuvwxyz” (the lowercase alphabet. Your program will ask the user whether they want to 1) encrypt a message, 2) decrypt a message, or 3) quit. If they choose encrypt or decrypt, you...
In C++ Write a program that contains a function, encrypt(Cypher) that encrypts the below text and...
In C++ Write a program that contains a function, encrypt(Cypher) that encrypts the below text and a second function, decrypt(Cypher),  that decrypts the encrypted message back to normal.  Cypher is the string which contain the plain or cypher texts.  Demonstrate that the encrypted message that you created is correctly decrypted.  For this problem you need to input “All Gaul is …” into a string Cypher.   Julius Caesar was one of the earliest persons to employ cryptology in history.  All his correspondence from his campaigns to...
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
Task 2.5: Write a script that will ask the user for to input a file name...
Task 2.5: Write a script that will ask the user for to input a file name and then create the file and echo to the screen that the file name inputted had been created 1. Open a new file script creafile.sh using vi editor # vi creafile.sh 2. Type the following lines #!/bin/bash echo ‘enter a file name: ‘ read FILENAME touch $FILENAME echo “$FILENAME has been created” 3. Add the execute permission 4. Run the script #./creafile.sh 5. Enter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT