Question

In: Computer Science

Write a Java program that will encode plain text into cipher text and decode cipher text...

Write a Java program that will encode plain text into cipher text and decode cipher text into plain text. Create following methods and add them to your class:

• a method named encode(String p, int key) that takes a plain text p and encodes it to a cipher text by adding the key value to each alphabet character of plain text p using the ASCII chart. The method should return the encoded String. For example, if p = "attack at dawn" and key is 5, it should be encrypted as "fyyfhpefyeifas" because ‘a’+5 = ‘f’, ‘t’+5 = ‘y’, ‘c’+5 = ‘h’, ‘k’+5 = ‘p’, ‘ ‘+5 = ‘e’, ‘d’+5 = ‘i’, ‘w’+5 = ‘a’, and ‘n’+5 = ‘s’.

• a method named decode(String c, int key) that takes a cipher text c and decodes it to a plain text by subtracting the key value from each alphabet character of c. The method should return the encoded String.

Assume that the only characters allowed in Strings p and c are lower-case alphabet letters and spaces. You don’t have to validate input for correct characters, but you should make sure you only pass Strings with all lowercase letters for p and c to your methods.

Provide the following menu using a do..while loop in the main method to the user. Options 1 and 2 in the menu should print the returned encoded or decoded text for the user.

1. Convert plain text to encoded cipher text

2. Convert cipher text to decoded plain text

3. Exit

Solutions

Expert Solution

Java Program:

import java.util.Scanner;

/* You can change class name what you want */
public class Main 
{
        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                System.out.println("Encryption <===> Decryption\n");
                while(true){
                        System.out.println("1. Convert plain text to encoded cipher text");
                        System.out.println("2. Convert cipher text to decoded plain text");
                        System.out.println("3. Exit");
                        
                        System.out.print("Enter your choice : ");
                        int choice = sc.nextInt(); //accept user choice

                        switch(choice){
                        case 1:
                            System.out.print("Enter Plain text : ");
                            sc.nextLine();  // skips the input line
                            String p = sc.nextLine().toLowerCase();
                            System.out.print("Enter Key : ");
                            int key = sc.nextInt();
                            encode(p, key);
                            break;
                        case 2:
                            System.out.print("Enter Cipher text : ");
                            sc.nextLine();  // skips the input line
                            String c = sc.nextLine().toLowerCase();
                            System.out.print("Enter Key : ");
                            int key1 = sc.nextInt();
                            decode(c, key1);
                            break;
                        case 3:
                            System.out.println("Thank You !!!");
                            System.exit(0);
                        default:
                            System.out.println("Incorrect input!!! Please re-enter choice from our menu");
                        }
                }
        }
        
        public static void encode(String p, int key)
        {
            String cipher = "";
            char letter;
            
            for(int i=0; i < p.length(); i++) 
        {
            letter = p.charAt(i); //shift one letter at a time
            
            // if letter lies in between a and z 
            if(letter >= 'a' && letter <= 'z') 
            {
                // shifting letter
                letter = (char) (letter + key);
                
                // if shift letter greater than 'z'
                if(letter > 'z') {
                    // reshift the letter to the starting position 
                    letter = (char) (letter+'a'-'z'-1);
                }
                cipher = cipher + letter;
            }
            else {
                cipher = cipher + letter;
            }
        
        }
        System.out.println("Encoded Cipher Text : " + cipher);
        }
        
        public static void decode(String c, int key1)
        {
            String plain = "";
            char letter;
            
            for(int i=0; i < c.length(); i++) 
        {
            letter = c.charAt(i); //shift one letter at a time
            
            // if letter lies in between a and z 
            if(letter >= 'a' && letter <= 'z') 
            {
                // shifting letter
                letter = (char) (letter - key1);
                
                // if shift letter less than 'a'
                if(letter < 'a') {
                    // reshift the letter to the starting position 
                    letter = (char) (letter-'a'+'z'+1);
                }
                plain = plain + letter;
            }
            
            else{
                plain = plain + letter;
            }
        
        }
        System.out.println("Decoded Plain Text : " + plain);
        }
}

Output:

Thumbs Up Please !!!


Related Solutions

Write a Python program to implement Vignere Cipher. Take user input to get plain text and...
Write a Python program to implement Vignere Cipher. Take user input to get plain text and key. TRY TO MAKE IT AS EASY AS YOU CAN.
!) Vigenere cipher Encode the text manybooksoninformationsecurity using the Vigenere cipher with the keyword fun
!) Vigenere cipher Encode the text manybooksoninformationsecurity using the Vigenere cipher with the keyword fun
Write the program in java Write a program that does basic encrypting of text. You will...
Write the program in java Write a program that does basic encrypting of text. You will ask the user the filename of a text file which contains a few sentences of text. You will read this text file one character at a time, and change each letter to another one and write out to an output file. The conversion should be done a -> b, b->c , … z->a, A->B, B->C, …. Z->A. This means just shift each letter by...
Write Java program Lab52.java which reads in a line of text from the user. The text...
Write Java program Lab52.java which reads in a line of text from the user. The text should be passed into the method: public static String[] divideText(String input) The "divideText" method returns an array of 2 Strings, one with the even number characters in the original string and one with the odd number characters from the original string. The program should then print out the returned strings.
An encryption-decryption system consists of three elements: encode, transmit, and decode. A faulty encode occurs in...
An encryption-decryption system consists of three elements: encode, transmit, and decode. A faulty encode occurs in 0.7% of the messages processed, transmission errors occur in 1% of the messages, and a decode error occurs in 0.1% of the messages. Assume the errors are independent. Round your answers to four decimal places (e.g. 98.7654). (a) What is the probability of a completely defect-free message? (b) What is the probability of a message that has either an encode or a decode error?
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
Write an Java/C program code to perform known-plaintext attack on Permutation Cipher. This program can be...
Write an Java/C program code to perform known-plaintext attack on Permutation Cipher. This program can be used to determine the length of the permutation m and the key permutation.
Write a Java program that uses the RC4 cipher algorithm to encrypt the following message using...
Write a Java program that uses the RC4 cipher algorithm to encrypt the following message using the word CODES as the key:   Cryptography is a method of protecting information and communications through the use of codes so that only those for whom the information is intended can read and process it. Instead of using stream length 256, we will use length 26. When encrypting, let A = 0 to Z = 25. Ignore spaces and punctuations and put the message...
Write a Java program that will test lines of text to see if they are palindromes....
Write a Java program that will test lines of text to see if they are palindromes. The program should prompt the user for the name of a file to process, then open and read the file of possible palindromes (one per line of text). The program should then print the total number of lines read, and total number of palindromes.
Python class Select a free ebook and download the plain text file (utf8). Write a program...
Python class Select a free ebook and download the plain text file (utf8). Write a program that does the following: Read in your ebook (any book, any language) Break the book into words (look at .split() for strings) Make a dictionary of all words and how often they occur: for instance if the word'the' happened 2000 time and the word'a' happened 1940 times word_dict= {'the' : 2000, 'a' :1940} Print the dictionary of word frequencies to a file (freqs.txt) with...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT