Question

Using the website Repl.it Write a Java program that can perform the Caesar cipher for English...

Using the website Repl.it

Write a Java program that can perform the Caesar cipher for English messages that include both upper and lowercase alphabetic characters. The Caesar cipher replaces each plaintext letter with a different one, by a fixed number of places down the alphabet. The program should be able to shift a specific number of places based on the user's input number.

For example

Plaintext: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.

Ciphertext: QEBNRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD

Also enable ROT13. Algorithm ROT13 is a common example of a cipher that was used quite frequently on numerous sites. With ROT13, you are shifting each character 13 places in the alphabet, the same shift will return the text to its original position.

And if you have time...write a program to decipher both. For example, change QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD back to THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

import java.util.Scanner;

public class CaesarCipher {
// Encrypts text using a shift od s
public static StringBuffer caesarCipher(String plaintext, int shift) {
StringBuffer result= new StringBuffer(); //the string that will be returned
char ch;
for (int i=0; i<plaintext.length(); i++) {
if (Character.isUpperCase(plaintext.charAt(i))) { // if the character is an uppercase character
if(((((int)plaintext.charAt(i) - shift - 65) % 26) + 65) < 65) {// if the new character is lower than A
ch = (char)((((int)plaintext.charAt(i) - shift - 65) % 26) + 65 + 26) ;// we add 26 more to it
}
else {
ch = (char)((((int)plaintext.charAt(i) - shift - 65) % 26) + 65);//else we add the base 65 to teh character
}
//System.out.println(" Character = " + plaintext.charAt(i) + "=" + ((int)plaintext.charAt(i) - shift - 65) % 26);
result.append(ch);
}
else if(Character.isLowerCase(plaintext.charAt(i))) {// if the character is an uppercase character
if(((((int)plaintext.charAt(i) - shift - 65) % 26) + 97) < 97) { // if the new character gooes lower than a
ch = (char)((((int)plaintext.charAt(i) - shift - 97) % 26) + 97 + 26);// we add 26 to it
}
else {
ch = (char)((((int)plaintext.charAt(i) - shift - 97) % 26) + 97);
}
result.append(ch);
}
else { // if the character is an special character, number or spaces
  
result.append(plaintext.charAt(i));
}
}
return result;
}
  
// Driver code
public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

String plaintext = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG";// plain text
System.out.println("Please enter the cipher shift:");// prompt to get the shift input
int shift = scan.nextInt(); // getting the shift
System.out.println("Text : " + plaintext); // writing the plain text
System.out.println("Shift : " + shift); // writing the shift
System.out.println("Cipher: " + caesarCipher(plaintext, shift)); // writing the ciphered text by calling caesarCipher function
}
}

OUTPUT:

HERE IS A CODE ALONG WITH DECRYPTION:

import java.util.Scanner;

public class CaesarCipher {
    // Encrypts text using a shift od s
    public static StringBuffer caesarCipher(String plaintext, int shift) {
        StringBuffer result= new StringBuffer(); //the string that will be returned
        char ch;
        for (int i=0; i<plaintext.length(); i++) {
            if (Character.isUpperCase(plaintext.charAt(i))) { // if the character is an uppercase character
                if(((((int)plaintext.charAt(i) - shift - 65) % 26) + 65) < 65) {// if the new character is lower than A
                    ch = (char)((((int)plaintext.charAt(i) - shift - 65) % 26) + 65 + 26) ;// we add 26 more to it
                }
                else {
                    ch = (char)((((int)plaintext.charAt(i) - shift - 65) % 26) + 65);//else we add the base 65 to teh character
                }
                //System.out.println(" Character = " + plaintext.charAt(i) + "=" + ((int)plaintext.charAt(i) - shift - 65) % 26);
                result.append(ch);
            }
            else if(Character.isLowerCase(plaintext.charAt(i))) {// if the character is an uppercase character
             if(((((int)plaintext.charAt(i) - shift - 65) % 26) + 97) < 97) { // if the new character gooes lower than a
                ch = (char)((((int)plaintext.charAt(i) - shift - 97) % 26) + 97 + 26);// we add 26 to it
             }
             else {
                ch = (char)((((int)plaintext.charAt(i) - shift - 97) % 26) + 97);
             }
                result.append(ch);
            }
            else { // if the character is an special character, number or spaces
          
                result.append(plaintext.charAt(i));
            }
        }
        return result;
    }
    // decryption function
    public static StringBuffer decryptCaesar(String ciphertext, int shift) {
        StringBuffer result= new StringBuffer(); //the string that will be returned
        char ch;
        for (int i=0; i<ciphertext.length(); i++) {
            if (Character.isUpperCase(ciphertext.charAt(i))) { // if the character is an uppercase character
                if(((((int)ciphertext.charAt(i) + shift - 65) % 26) + 65) > 90) {// if the new character is greater than A
                    ch = (char)((((int)ciphertext.charAt(i) + shift - 65) % 26) + 65 - 26) ;// we subtract 26 from it
                }
                else {
                    ch = (char)((((int)ciphertext.charAt(i) + shift - 65) % 26) + 65);//else we add the base 65 to the character
                }
             
                result.append(ch);
            }
            else if(Character.isLowerCase(ciphertext.charAt(i))) {// if the character is an uppercase character
             if(((((int)ciphertext.charAt(i) + shift - 65) % 26) + 97) > 122) { // if the new character gooes higher than a
                ch = (char)((((int)ciphertext.charAt(i) + shift - 97) % 26) + 97 - 26);// we subtract 26 from it
             }
             else {
                ch = (char)((((int)ciphertext.charAt(i) + shift - 97) % 26) + 97);// else we only add 97 to it
             }
                result.append(ch);
            }
            else { // if the character is an special character, number or spaces
          
                result.append(ciphertext.charAt(i));
            }
        }
        return result;
    }

    // Driver code
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        String plaintext = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG";// plain text
        System.out.println("Please enter the cipher shift:");// prompt to get the shift input
        int shift = scan.nextInt(); // getting the shift
        System.out.println("Text : " + plaintext); // writing the plain text
        System.out.println("Shift : " + shift); // writing the shift
        String encrypted = caesarCipher(plaintext, shift).toString();// getting the ciphertext in a variable
        System.out.println("Cipher: " + encrypted); // writing the ciphered text by calling caesarCipher function

        System.out.println("Decrypyted Cipher:" + decryptCaesar(encrypted,shift));
    }
}


OUTPUT:

As the question asks, you can simply edit the value of shift = 13 for implementing the same logic for Rot13. The shift size woulf be fixed,the rest of the things will be the same.

Add a comment
Know the answer?
Add Answer to:
Using the website Repl.it Write a Java program that can perform the Caesar cipher for English...
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
  • using the website repl.it (must be done in Javascript) PGM #1 Write a Java program that can perform the Caesar cipher fo...

    using the website repl.it (must be done in Javascript) PGM #1 Write a Java program that can perform the Caesar cipher for English messages that include both upper and lowercase alphabetic characters. The Caesar cipher replaces each plaintext letter with a different one, by a fixed number of places down the alphabet. The cipher illustrated here uses a left shift of three, so that (for example) each occurrence of E in the plaintext becomes B in the ciphertext. For example...

  • USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is...

    USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is one of the simplest encryption techniques in the field of cryptography. It is a cipher in which each letter in a plain text message is replaced by a letter some fixed number of positions up the alphabet (i.e., by right shifting the alphabetic characters in the plain text message). For example, with a right shift of 2, ’A’ is replaced by ’C’, ’B’ is...

  • Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple...

    Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple encryption methods. It works by shifting all letters in the original message (plaintext) by a certain fixed amount (the amounts represents the encryption key). The resulting encoded text is called ciphertext. Example Key (Shift): 3 Plaintext: Abc Ciphertext: Def Task Your goal is to implement a Caesar cipher program that receives the key and an encrypted paragraph (with uppercase and lowercase letters, punctuations, and...

  • In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or...

    In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. Given an arbitrary cipher text file, you need to write a C++ program to find out the value of the shift, and decrypt the...

  • Python 3:Write a program that inputs a text file. The program should print the unique words...

    Python 3:Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order.   Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order An example file along with the correct output is shown below: example.txt the quick brown fox jumps over the lazy dog Enter the input file name: example.txt brown dog fox jumps lazy over quick the

  • Simple Python Program Working with strings Write a Python program to read in a number of...

    Simple Python Program Working with strings Write a Python program to read in a number of strings from the user. Stop when the user enters “Done”. Then check if each of the strings contains all the letters of the alphabet. Print the results in the form of a dictionary, where they keys are the strings and the values are the Truth Value for the string, which you just calculated. Sample Run: Enter the strings: taco cat The quick brown fox...

  • Write a program using regular expression to find the starting and ending locations of ‘fox’ in...

    Write a program using regular expression to find the starting and ending locations of ‘fox’ in the text 'The quick brown fox jumps over the lazy dog.' Then print out its locations as follows: Found fox from (the starting location) to (the ending location).

  • Computer Science C++ Help, here's the question that needs to be answered (TASK D): Task D....

    Computer Science C++ Help, here's the question that needs to be answered (TASK D): Task D. Decryption Implement two decryption functions corresponding to the above ciphers. When decrypting ciphertext, ensure that the produced decrypted string is equal to the original plaintext: decryptCaesar(ciphertext, rshift) == plaintext decryptVigenere(ciphertext, keyword) == plaintext Write a program decryption.cpp that uses the above functions to demonstrate encryption and decryption for both ciphers. It should first ask the user to input plaintext, then ask for a right...

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

  • ‘C’ programming language question Write a MENU DRIVEN program to A) Count the number of vowels...

    ‘C’ programming language question Write a MENU DRIVEN program to A) Count the number of vowels in the string B) Count the number of consonants in the string C) Convert the string to uppercase D) Convert the string to lowercase E) Display the current string X) Exit the program The program should start with a user prompt to enter a string, and let them type it in. Then the menu would be displayed. User may enter option in small case...

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