By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,806 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,806 IT Pros & Developers. It's quick & easy.

Caesar Cipher letter extraction

P: 10
Hey, I have spent all of my afternoon writing this program that uses a Caesar Cipher for encryption. I have the entire program written and it works fine. this is the problem area:

New text string is the above string converted to lower case, "the quick brown fox jumps over the lazy dog"
Extract the letter 'D' (first letter) from the key. Extract the letter 'a' (first letter) from the ALPHABET.
Replace all 'a's with 'D's. Getting "the quick brown fox jumps over the lDzy dog"
Extract the letter 'E' (2nd letter) from the key. Extract the letter 'b' (2nd letter) from the ALPHABET.
Replace all 'b's with 'E's. Getting "the quick Erown fox jumps over the lDzy dog"
Extract the letter 'F' (3rd letter) from the key. Extract the letter 'c' (3rd letter) from the ALPHABET.
Replace all 'c's with 'F's. Getting "the quiFk Erown fox jumps over the lDzy dog"

I can extract the letters individually no problem. Where I have trouble is where I'm asked to multiple letters at a time. I spent a good portion of the time that I have been working on this project trying to figure it out. Then I eventually got tired on being unsuccessful and moved on to the decryption part. If someone could give a hand and point me in the right direction it would be appreciated.
Here is the code:
Expand|Select|Wrap|Line Numbers
  1. public class CeasarCode {
  2.  
  3.     /**
  4.      * This program takes a message, and turns it into a coded message using what is known as the Caeser Cipher. 
  5.      */
  6.     public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";    
  7.         //This method the ALPHABET string
  8.         public static void main(String[] args){    
  9.             String key = "DEFGHIJKLMNOPQRSTUVWXYZABC";    
  10.             String message= "the quick brown fox jumps over the lazy dog";    
  11.             String encrypted = encrypt(message, key); 
  12.             String decrypted = decrypt(message, key);
  13.             System.out.println("Message: "+message);
  14.             System.out.println("Encryption: "+encrypted);  
  15.             System.out.println("Decryption: "+decrypted.toLowerCase());
  16.         } //The above method creates the variables "key" and "message".
  17.           //It also prints the coded message returned from the encrypt() method
  18.           //and the decoded message returned from the decrypt() method.
  19.  
  20.         public static String encrypt(String message, String key){    
  21.             String encryptedString = message.toLowerCase();    
  22.             for (int i = 0; i < 26; i++){    
  23.                 char keyChar = key.charAt(i);    
  24.                 char alphaChar = ALPHABET.charAt(i);    
  25.  
  26.                 encryptedString = encryptedString.replace(alphaChar,keyChar);    
  27.             }    
  28.             return encryptedString;    
  29.         }//This method takes the strings "key" and "message", and encrypts them by replacing the letters of-
  30.          //the alphabet with the corresponding letters from "key" using a for loop
  31.          public static String decrypt(String message, String key) {
  32.              String decryptedString = message.toUpperCase();
  33.              for (int i = 0; i < 26; i++){    
  34.                 char alphaChar = key.charAt(i);
  35.                 char keyChar = ALPHABET.charAt(i);
  36.              decryptedString=decryptedString.replace(keyChar, alphaChar);
  37.              }
  38.              return decryptedString;
  39.          }//This method takes the message that was encrypted in the previous method and 
  40.           //decrypts it by essentially reversing the code written above
  41. }
  42.  
Output:
Message: the quick brown fox jumps over the lazy dog
Encryption: WKH TXLFN EURZQ IRA MXPSV RYHU WKH ODCB GRJ
Decryption: the quick brown fox jumps over the lazy dog
Mar 19 '12 #1
Share this Question
Share on Google+
7 Replies


Rabbit
Expert Mod 10K+
P: 12,422
I don't know what you mean when you say "asked to multiple letters at a time"
Mar 19 '12 #2

P: 10
Sorry about the typo. I'm supposed to extract multiple letters at once.
For example:
Replace all 'c's with 'F's. Getting "the quiFk Erown fox jumps over the lDzy dog"

The letters in bold show letters that were extracted, and replaced with their equivalent in the key(found in my main method).
Does that make any sense?
Mar 20 '12 #3

Rabbit
Expert Mod 10K+
P: 12,422
Not really, no. What does "replace c with F" have to do with the other letters?
Mar 20 '12 #4

P: 10
Basically, instead of encrypting the message, it's just having me encrypt the individual letters. For the other letters 'E' is the equivalent of 'b' according to the key, and 'D' the equivalent of 'a'.
Maybe this will help you understand:
Note: this text contains only one of each letter. If it were " abcabcabc" the result string would become "DbcDbcDbc", then "DEcDEcDEc", then "DEFDEFDEF"

As far as the whole point of the exercise... I think it's just to be a pain in the neck. I mean I've gotten the program to do everything else it's supposed to do.
Mar 20 '12 #5

Rabbit
Expert Mod 10K+
P: 12,422
And you're saying it's not doing that? What is it doing instead?
Mar 20 '12 #6

P: 10
I figured it out. In my method encrypt() I could only extract one letter and replace it with the corresponding letter from my key. So what I did was create 2 more methods named encryptI() and encryptII().
This was from the encrypt() method:
"String encrypted = encrypt(message, key);"
So with my 2 other methods I did the same thing as with the first except for one change in each:
"String encryptedI = encryptI(encrypted,key);" --> for encryptI
and
"String encryptedII = encryptII(encryptedI, key);" --> for encryptII
Then I did:
"System.out.println(encryptedII)"
and got the desired output:
"Encryption: the quiFk Erown fox jumps over the lDzy dog"

I appreciate your help. You asking me to explain what I wanted helped me organize my thoughts.
Mar 20 '12 #7

Rabbit
Expert Mod 10K+
P: 12,422
I'm glad you got it figured out. Good luck.
Mar 20 '12 #8

Post your reply

Sign in to post your reply or Sign up for a free account.