473,320 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Caesar Cipher letter extraction

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
7 3640
Rabbit
12,516 Expert Mod 8TB
I don't know what you mean when you say "asked to multiple letters at a time"
Mar 19 '12 #2
rsduck
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
12,516 Expert Mod 8TB
Not really, no. What does "replace c with F" have to do with the other letters?
Mar 20 '12 #4
rsduck
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
12,516 Expert Mod 8TB
And you're saying it's not doing that? What is it doing instead?
Mar 20 '12 #6
rsduck
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
12,516 Expert Mod 8TB
I'm glad you got it figured out. Good luck.
Mar 20 '12 #8

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

Similar topics

4
by: Carl Harris | last post by:
I am trying to write some code to: 1.Prompt a user for filenames 2.Open the files 3.Convert my plain text into a cipher text array/string bear in mind I am a novice! I have wriiten some code...
8
by: markus | last post by:
I have an application that I think was developed using some form of Borland C with a folder for BIG images with the extension .fre. When I try to view them they cannot be read even by changing the...
1
by: beetle17 | last post by:
Plaintext: a  n i c e  d a y Key: -3 Ciphertext: X  k f Z b  a X v Cipher will accept commands from the user and perform the operations required by the commands. There are three different...
4
by: wagn31 | last post by:
i need to use a cipher but I have to used the assigned code in the ciphering i know how to do it, but i am not sure how to add my own dictionary. Here is what i have so far:
2
thatos
by: thatos | last post by:
How can a progrsm caesar cipher using python
1
by: happysuki | last post by:
Julius Caesar used this cipher with a cyclic shift of three to encrypt his message. In this case a is replaced by d, m by p and z by c. Hence ‘Julius Caesar’ will become ‘Mxolxv Fdhvdu’. Write a...
2
by: barks33 | last post by:
Hi all, im writing a caesar cipher and ive done most of the program (see below) def decrypt(ciphertext, shift): decrytped_text = " " for letter in ciphertext: if...
1
by: Ashley Demers | last post by:
I am taking a second semester programming course in C and have to write this program that I have to write a program that encrypts a message using a Caesar cipher. The user will enter the message to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.