Question

Write a java program that takes the following phrases and encrypts them using a substitution cipher....

Write a java program that takes the following phrases and encrypts them using

a substitution cipher. The program should ask for a key and a phrase and then proceed to encrypt

that phrase using the given shared key. Make sure that you have the key for the cipher and

the output of encrypted phrases.

Encrypt the phrase:

He who fights with monsters should look to it that he himself does not

become a monster. And if you gaze long into an abyss , the abyss also

gazes into you.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class EncryptPhrase {

    public static String encode(String code, int key) {
        String result = "";
        char ch;
        for (int i = 0; i < code.length(); ++i) {
            ch = code.charAt(i);
            if (Character.isLowerCase(ch)) {
                ch = (char) ('a' + ((ch - 'a' + key + 26) % 26));
            } else if (Character.isUpperCase(ch)) {
                ch = (char) ('A' + ((ch - 'A' + key + 26) % 26));
            }
            result += ch;
        }
        return result;
    }

    public static String decode(String code, int key)
    {
        String result = "";
        char ch;
        for(int i = 0; i < code.length(); ++i) {
            ch = code.charAt(i);
            if(Character.isLowerCase(ch)) {
                ch = (char) ('a' + ((ch - 'a' - key + 26) % 26));
            } else if(Character.isUpperCase(ch)) {
                ch = (char) ('A' + ((ch - 'A' - key + 26) % 26));
            }
            result += ch;
        }
        return result;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a key: ");
        int key = Integer.parseInt(in.nextLine());
        System.out.print("Enter a single line of phrase: ");
        String phrase = in.nextLine();
        String cipher = encode(phrase, key);
        System.out.println("Cipher text is\n" + cipher + "\n\n");
        System.out.println("Plain text is\n" + decode(cipher, key));
        in.close();
    }
}
Add a comment
Know the answer?
Add Answer to:
Write a java program that takes the following phrases and encrypts them using a substitution cipher....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Python program that takes the following phrases and encrypts them using a substitution cipher....

    Write a Python program that takes the following phrases and encrypts them using a substitution cipher. It should ask for a key and a phrase and then proceed to encrypt that phrase using the given shared key. Make sure that you have the key for the cipher that you use and the output of encrypted phrases. 1. He who fights with monsters should look to it that he himself does not become a monster . And if you gaze long...

  • Using a substitution cipher that skips by 7 characters, encrypt the message COMPUTERSCIENCE4ALL. For the input...

    Using a substitution cipher that skips by 7 characters, encrypt the message COMPUTERSCIENCE4ALL. For the input alphabet use 26 uppercase letters followed by 10 digits numbered 0 through 9. I have encrypted an English phrase using a substitution cipher that has an input alphabet of 26 uppercase letters. My cipher has skipped by N letters. You may assume that the most common letters in the English language are E T A O I N Sentence: LJWRPDNBBCQNARPQCJWBNA

  • Can i get Playfair Cipher for python 3 that encrypts a message and decrypts it, could...

    Can i get Playfair Cipher for python 3 that encrypts a message and decrypts it, could you possibly make it as simple as you can without losing functionality. please include comments, that would help me better understand Example of PlayFair Cipher: https://en.wikipedia.org/wiki/Playfair_cipher The Playfair cipher uses a 5 by 5 table containing a key word or phrase. Memorization of the keyword and 4 simple rules was all that was required to create the 5 by 5 table and use the...

  • JAVA Problem: With the recent news about data breaches and different organizations having their clients’ information...

    JAVA Problem: With the recent news about data breaches and different organizations having their clients’ information being exposed, it is becoming more and more important to find ways to protect our sensitive data. In this program we will develop a simple tool that helps users generate strong passwords, encrypt and decrypt data using some cyphering techniques. You will need to create two classes. The first class is your driver for the application and contains the main method. Name this class...

  • Substitution Cipher Ke 1. Decipher the following ciphertext using the substitution cipher key shown above: (4 points) Ciphertext: DOVMYWOJAYJMYWZBAOXOADY!U I Plaintext: The following information was...

    Substitution Cipher Ke 1. Decipher the following ciphertext using the substitution cipher key shown above: (4 points) Ciphertext: DOVMYWOJAYJMYWZBAOXOADY!U I Plaintext: The following information was extracted from The BLACK Chamber's Pigpen Cipher page. The content is available at https:/simonsingh net The_Black Chamber'pigpen html. "The Pigpen Cipher was used by Freemasons in the l8th Century to keep their records private. The cipher does not substitute one letter for another; rather it substitutes each letter for a symbol The aiphabet is written...

  • Write a program that implements an elementary bit stream cipher. An elementary level bit stream cipher...

    Write a program that implements an elementary bit stream cipher. An elementary level bit stream cipher is an encryption algorithm that encrypts 1 byte of plain text at a time. This one uses a given 4-bit bit pattern as the key. The size of the encrypted message that we want to be able to send has a maximum length of 200 characters. You must: 1. prompt the user to input the clear text to be encrypted. You must use printf()...

  • C Program In this assignment you'll write a program that encrypts the alphabetic letters in a...

    C Program In this assignment you'll write a program that encrypts the alphabetic letters in a file using the Vigenère cipher. Your program will take two command line parameters containing the names of the file storing the encryption key and the file to be encrypted. The program must generate output to the console (terminal) screen as specified below. Command Line Parameters Your program must compile and run from the command line. The program executable must be named “vigenere” (all lower...

  • Help write down below program with C++ language!!! Please... The Cipher Program Requirements An interactive program...

    Help write down below program with C++ language!!! Please... The Cipher Program Requirements An interactive program is required that allows a user to encode text using any of three possible ciphers. The three ciphers you are to offer are: Caesar, Playfair and Columnar Transposition. • The program needs to loop, repeating to ask the user if they wish to play with Caesar, Playfair or Columnar Transposition until the user wishes to stop the program. •For encoding, the program needs to...

  • Assignment 7: Caesar Cipher Assignment 7 You will create a Caesar cipher which allows the user...

    Assignment 7: Caesar Cipher Assignment 7 You will create a Caesar cipher which allows the user to specify the key and the text to be encrypted. A Caesar cipher is a simple substitution cipher wherein each letter in the message is shifted a certain number of spaces down the alphabet -- this number is called the key. a b c d e f g h i j k l m n o p q r s t u v w...

  • Language: C Write an encoder and a decoder for a modified "book cipher." A book cipher...

    Language: C Write an encoder and a decoder for a modified "book cipher." A book cipher uses a document or book as the cipher key, and the cipher itself uses numbers that reference the words within the text. For example, one of the Beale ciphers used an edition of The Declaration of Independence as the cipher key. The cipher you will write will use a pair of numbers corresponding to each letter in the text. The first number denotes the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT