Question

Please help me write this Java program. I had posted this question before, but got an...

Please help me write this Java program. I had posted this question before, but got an answer that was totally wrong. We are using the latest version of Java8. Thank You!

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

Write a Java program that can successfully DECRYPT a string inputted by the user which has been encrypted using a Caesar Cipher with a unknown shift value(key).

You can use brute force to do a for loop through all the 26 shift values, however, your program should only return the CORRECT decrypted string and not all the other incorrectly decoded strings.  

Your program should be case sensitive and should include the foreign characters (such as commas, puncuation marks, etc.) as they were located in the encrypted text. You are to only decrypt the letters of the alphabet (a-z), all other characters should stay as they are. Your program should work for all shift values up to and including 26, and then print the correctly decoded string.

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

An Example run would be:

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

Enter Encoded Text:
Czxafepc Sntpynp, td lmzgp lww, esp rcplepde!

Decoded Text is:
Computer Science, is above all, the greatest!

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

Another Example run would be:

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

Enter Encoded Text:
Iw yv yru repkyzex tfewzuvekzrc kf jrp, yv nifkv zk ze tzgyvi, kyrk zj, sp jf tyrexzex kyv fiuvi fw kyv cvkkvij fw kyv rcgyrsvk, kyrk efk r nfiu tflcu sv druv flk.

Decoded Text is:
If he had anything confidential to say, he wrote it in cipher, that is, by so changing the order of the letters of the alphabet, that not a word could be made out.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you need any clarification, please give me comment...

class Cipher {

public static void main(String args[]){

String str = "Czxafepc Sntpynp, td lmzgp lww, esp rcplepde!";

String dec_str;

for(int i=0; i<26; i++){

dec_str = decode(str, i);

System.out.println("Key: "+i+", Decode Str is: "+dec_str);

}

System.out.println("\n\n");

str = "Iw yv yru repkyzex tfewzuvekzrc kf jrp, yv nifkv zk ze tzgyvi, kyrk zj, sp jf tyrexzex kyv fiuvi fw kyv cvkkvij fw kyv rcgyrsvk, kyrk efk r nfiu tflcu sv druv flk.";

for(int i=0; i<26; i++){

dec_str = decode(str, i);

System.out.println("Key: "+i+", Decode Str is: "+dec_str);

}

}

public static String decode(String str, int shift) {

int i, len = str.length();

String dec_str = "";

for (i = 0; i < len; i++) {

if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') {

if (str.charAt(i) - shift < 'a') {

dec_str += (char)('z' - (('a' - (str.charAt(i) - shift)) % 26) + 1);

} else

dec_str += (char)(str.charAt(i) - shift);

}

else

dec_str += str.charAt(i);

}

return dec_str;

}

}

Shift key is: 11

Add a comment
Know the answer?
Add Answer to:
Please help me write this Java program. I had posted this question before, but got an...
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 the programming C please, not C++. The main function should be to find the offset...

    Write the programming C please, not C++. The main function should be to find the offset value of the ciper text "wPL2KLK9PWWZ7K3ST24KZYKfPMKJ4SKLYOKRP4KFKP842LK0ZTY43 " and decrypt it. In cryptography, a Caesar Cipher 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. For example, with a left shift of 3, D would be...

  • In java write a command-line program that helps to decrypt a message that has been encrypted...

    In java write a command-line program that helps to decrypt a message that has been encrypted using a Caesar cipher1. Using this method, a string may contain letters, numbers, and other ASCII characters, but only the letters (upper- and lower-case) are encrypted – a constant number, the shift, is added to the ASCII value of each letter and when letters are shifted beyond ‘z’ or ‘Z’ they are wrapped around (e.g. “Crazy?” becomes “Etcba?” when shifted by 2). When your...

  • Develop a Java application that uses a type of encrypted alphabet, called a random monoalphabetic cipher,...

    Develop a Java application that uses a type of encrypted alphabet, called a random monoalphabetic cipher, to encrypt and decrypt a message. Your encryption key and message are to be read in from two different files and then the encrypted message will be output to third file. Then the encrypted message is read in and decrypted to a fourth file. A monoalphabetic cipher starts with an encryption word, removes the redundant letters from the word, and assigns what’s left to...

  • Cryptography, the study of secret writing, has been around for a very long time, from simplistic...

    Cryptography, the study of secret writing, has been around for a very long time, from simplistic techniques to sophisticated mathematical techniques. No matter what the form however, there are some underlying things that must be done – encrypt the message and decrypt the encoded message. One of the earliest and simplest methods ever used to encrypt and decrypt messages is called the Caesar cipher method, used by Julius Caesar during the Gallic war. According to this method, letters of the...

  • Security is an important feature of information systems. Often, text is encrypted before being sent, and...

    Security is an important feature of information systems. Often, text is encrypted before being sent, and then decrypted upon receipt. We want to build a class (or several classes) encapsulating the concept of encryption. You will need to test that class with a client program where the main method is located. For this project, encrypting consists of translating each character into another character. For instance, if we consider the English alphabet, including characters a through z, each character is randomly...

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

  • JAVA PROJECT Part 1 - Normalize Text The first thing we will do is normalize the...

    JAVA PROJECT Part 1 - Normalize Text The first thing we will do is normalize the input message so that it’s easier to work with. Write a method called normalizeText which does the following: Removes all the spaces from your text Remove any punctuation (. , : ; ’ ” ! ? ( ) ) Turn all lower-case letters into upper-case letters Return the result. The call normalizeText(“This is some \“really\” great. (Text)!?”) should return “THISISSOMEREALLYGREATTEXT” Part 2 - Obfuscation...

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

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

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

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