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
- public class CeasarCode {
- /**
- * This program takes a message, and turns it into a coded message using what is known as the Caeser Cipher.
- */
- public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
- //This method the ALPHABET string
- public static void main(String[] args){
- String key = "DEFGHIJKLMNOPQRSTUVWXYZABC";
- String message= "the quick brown fox jumps over the lazy dog";
- String encrypted = encrypt(message, key);
- String decrypted = decrypt(message, key);
- System.out.println("Message: "+message);
- System.out.println("Encryption: "+encrypted);
- System.out.println("Decryption: "+decrypted.toLowerCase());
- } //The above method creates the variables "key" and "message".
- //It also prints the coded message returned from the encrypt() method
- //and the decoded message returned from the decrypt() method.
- public static String encrypt(String message, String key){
- String encryptedString = message.toLowerCase();
- for (int i = 0; i < 26; i++){
- char keyChar = key.charAt(i);
- char alphaChar = ALPHABET.charAt(i);
- encryptedString = encryptedString.replace(alphaChar,keyChar);
- }
- return encryptedString;
- }//This method takes the strings "key" and "message", and encrypts them by replacing the letters of-
- //the alphabet with the corresponding letters from "key" using a for loop
- public static String decrypt(String message, String key) {
- String decryptedString = message.toUpperCase();
- for (int i = 0; i < 26; i++){
- char alphaChar = key.charAt(i);
- char keyChar = ALPHABET.charAt(i);
- decryptedString=decryptedString.replace(keyChar, alphaChar);
- }
- return decryptedString;
- }//This method takes the message that was encrypted in the previous method and
- //decrypts it by essentially reversing the code written above
- }
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