Results 1 to 5 of 5
Thread: The Caesar Shift
- 02-22-2013, 10:07 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 3
- Rep Power
- 0
The Caesar Shift
Hi everyone. I'm taking a course online for java and the assignment today has me stumped. I have to write a program to encode/decode messages using the Caesar shift. Basically, the program asks for the user's message and whether or not they want to encode it for decode it. The program then asks for the shift key value and then goes from there. I have the encryption part down. I have basically made a new alphabet based on the shift key value, and then used that to encode the message by matching up the index values. However, the way that I thought I could decrypt it didn't work whatsoever. You can see the remnants of that in the appropriate method. But here is the code:
Java Code:public class CaesarShiftEncryption { private final String alphabet = "abcdefghijklmnopqrstuvwxyz"; private String newAlphabet = ""; private String userMessage; private int userShiftKey; public CaesarShiftEncryption(String message, int shiftKey) { userMessage = message; userShiftKey = shiftKey; } public void newAlphabetEncrypt() { int alphabetTemp; for(int i = 0; i < alphabet.length(); i++) { alphabetTemp = i + userShiftKey; alphabetTemp = alphabetTemp % 26; newAlphabet += alphabet.charAt(alphabetTemp); } } public void encryptMessage() { int letter; String encode = ""; char[] messageLetters = new char[userMessage.length()]; for(int i = 0; i < userMessage.length(); i++) { messageLetters[i] = userMessage.charAt(i); } for(int i = 0; i < userMessage.length(); i++) { letter = alphabet.indexOf(messageLetters[i]); encode += newAlphabet.substring(letter, letter + 1); } System.out.println("Your encoded message: " + encode); } public void newAlphabetDecrypt() { int alphabetTemp; for(int i = alphabet.length(); i > 0; i--) { alphabetTemp = i - userShiftKey; alphabetTemp = alphabetTemp % 26; newAlphabet += alphabet.charAt(alphabetTemp); } } public void decryptMessage() { int letter; String encode = ""; char[] messageLetters = new char[userMessage.length()]; for(int i = 0; i < userMessage.length(); i++) { messageLetters[i] = userMessage.charAt(i); } for(int i = 0; i < userMessage.length(); i++) { letter = alphabet.indexOf(messageLetters[i]); encode += newAlphabet.substring(letter, letter + 1); } System.out.println("Your encoded message: " + encode); } }
- 02-23-2013, 12:38 AM #2
Senior Member
- Join Date
- Jan 2009
- Location
- CA, USA
- Posts
- 271
- Rep Power
- 13
Re: The Caesar Shift
Essentially you just need to do the encrypt shift in reverse. That is, however far you shift the values when you encrypt it (we'll call it x), you would shift them -x to decipher it.
Here's a quick example"
Java Code:public class Caesar { public static void main(String[] args) { // Start with some message String msg = "some message to be encrypted"; System.out.println("Message: " + msg); System.out.println(); // Shift the characters however many spaces in one direction to encrypt it String encrypted = shift(msg, 5); System.out.println("Encrypted: " + encrypted); System.out.println(); // Simply shift the characters in the opposite direction to decipher it String decrypted = shift(encrypted, -5); System.out.println("Deciphered: " + decrypted); } public static String shift(String str, int shift) { // Convert String to a char array char[] arr = str.toCharArray(); for(int i = 0; i < arr.length; i++) { // increment the character by the shift arr[i] += shift; } // return the new String return String.valueOf(arr); } }
Message: some message to be encrypted
Encrypted: xtrj%rjxxflj%yt%gj%jshw~uyji
Deciphered: some message to be encryptedLast edited by AndrewM16921; 02-23-2013 at 12:41 AM.
- 02-23-2013, 01:36 AM #3
Member
- Join Date
- Feb 2013
- Posts
- 3
- Rep Power
- 0
Re: The Caesar Shift
Wow! You really shortened the code! I see what you mean though. I should have just converted it to the character array like you, then subtracted the values. I did overthink this lol. :D Thanks for your help!
- 02-23-2013, 02:06 AM #4
Senior Member
- Join Date
- Jan 2009
- Location
- CA, USA
- Posts
- 271
- Rep Power
- 13
Re: The Caesar Shift
Glad I could help ^_^
- 02-25-2013, 01:10 AM #5
Member
- Join Date
- Feb 2013
- Posts
- 3
- Rep Power
- 0
Re: The Caesar Shift
I don't mean to reopen this thread but I have a question. What if the user wanted to implement his own custom alphabet? (I'm going to use this for a little game I've been working on) The way I was going to complete this before involved creating the alphabet in an array, then converting that array into the ASCII code values, and then using the shift on those values. For example:
The alphabet is in a char array.
I then create an int array to hold the ASCII code values for the alphabet.
So I do something like this:
Java Code:for(int i = 0; i < alphabet.length; i++) { alphabetASCII[i] = alphabet[i] + ShiftKey; if(alphabetASCII[i] > 122) { alphabetASCII[i] = alphabetASCII[i] - 26; } }
Thanks!
Similar Threads
-
Caesar Encrypt and Decrypt
By manfredo in forum New To JavaReplies: 11Last Post: 01-22-2011, 11:40 PM -
Caesar Cipher?
By socboy6579 in forum Advanced JavaReplies: 5Last Post: 10-29-2010, 11:59 PM -
where exactly bit shift operators are useful?
By sandeepsai39 in forum New To JavaReplies: 6Last Post: 09-08-2010, 12:58 PM -
Anyway to fix the lines so they dun shift?
By PeterFeng in forum New To JavaReplies: 0Last Post: 01-14-2009, 11:26 AM -
Caesar and encoding with block spaces
By Franneldort in forum New To JavaReplies: 13Last Post: 10-30-2008, 05:48 PM
Bookmarks