Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By AndrewM16921

Thread: The Caesar Shift

  1. #1
    BillyMayes is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default 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);
    	}
    }
    How would I get it to decode a message? I feel like it's something simple, but everything I try doesn't work. I feel like I'm overthinking it, but I'm not sure. Thanks!

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    261
    Rep Power
    6

    Default 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);
    	}
    }
    Prints:

    Message: some message to be encrypted
    Encrypted: xtrj%rjxxflj%yt%gj%jshw~uyji
    Deciphered: some message to be encrypted
    Last edited by AndrewM16921; 02-22-2013 at 11:41 PM.
    BillyMayes likes this.

  3. #3
    BillyMayes is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default 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!

  4. #4
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    261
    Rep Power
    6

    Default Re: The Caesar Shift

    Glad I could help ^_^

  5. #5
    BillyMayes is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default 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;
    			}
    		}
    So now the alphabet is stored into an int array containing the appropriate ASCII code values. So element 0 is 97 and so on and so forth. However, the shift key has been applied so element 0 might start at 112, 113, etc. What if I wanted to use that array to encode a message? How would I use an actual alphabet like I was planning to before, to complete this? If it's possible I would love to know, because then I would be able to implement custom alphabets to encode with, instead of the default a-z.

    Thanks!

Similar Threads

  1. Caesar Encrypt and Decrypt
    By manfredo in forum New To Java
    Replies: 11
    Last Post: 01-22-2011, 10:40 PM
  2. Caesar Cipher?
    By socboy6579 in forum Advanced Java
    Replies: 5
    Last Post: 10-29-2010, 10:59 PM
  3. where exactly bit shift operators are useful?
    By sandeepsai39 in forum New To Java
    Replies: 6
    Last Post: 09-08-2010, 11:58 AM
  4. Anyway to fix the lines so they dun shift?
    By PeterFeng in forum New To Java
    Replies: 0
    Last Post: 01-14-2009, 10:26 AM
  5. Caesar and encoding with block spaces
    By Franneldort in forum New To Java
    Replies: 13
    Last Post: 10-30-2008, 04:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •