Results 1 to 6 of 6

Thread: Caesar Cipher?

  1. #1
    socboy6579 is offline Member
    Join Date
    Oct 2010
    Posts
    81
    Rep Power
    0

    Default Caesar Cipher?

    Does anyone have any sources or references to a Caesar Cipher prgoram with a shift of 3 letters?

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by socboy6579 View Post
    Does anyone have any sources or references to a Caesar Cipher prgoram with a shift of 3 letters?
    Is Google down in your part of the world? b.t.w. what has this question to do with 'Advanced Java'?

    kind regards,

    Jos

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by socboy6579 View Post
    Does anyone have any sources or references to a Caesar Cipher prgoram with a shift of 3 letters?
    You'd learn best by writing this yourself. It's not hard to do, though with a "Caesar cipher" the shift is usually half the max char code - min char code so that encryption is the same as decryption.

  4. #4
    socboy6579 is offline Member
    Join Date
    Oct 2010
    Posts
    81
    Rep Power
    0

    Default

    Java Code:
    import java.io.*; 
    
    public class CaesarCypher {
    	public static void main(String args[])
    	{
    		final int MOVE_DOWN = 3; 
    		String plainText = "Mr Spiegel"; 
    		char character; 
    
    		System.out.print("Encrypted sentence is: " );
    
    		for (int iteration = 0; iteration < plainText.length(); iteration++)
    		{
          			character = plainText.charAt(iteration); //get the letters
          			character = (char) (character + MOVE_DOWN); //perform the move down shift +3
          			System.out.print(character);
          			
    		}
    
    		System.out.println(); 
    		
    
    	}
    }
    I have part of the code for my cypher to move down 3. But the only thing i am stuck on is how would i keep the spaces in the cyper. so they that i input "Michael Hall" and the output of that is "Encrypted sentence is: Plfkdho#Kdoo" with the # for the space. How would i keep an input with a space to keep the space in the output. along with a dash(-)

  5. #5
    theChameleon is offline Member
    Join Date
    Oct 2010
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by socboy6579 View Post
    Java Code:
    import java.io.*; 
    
    public class CaesarCypher {
    	public static void main(String args[])
    	{
    		final int MOVE_DOWN = 3; 
    		String plainText = "Mr Spiegel"; 
    		char character; 
    
    		System.out.print("Encrypted sentence is: " );
    
    		for (int iteration = 0; iteration < plainText.length(); iteration++)
    		{
          			character = plainText.charAt(iteration); //get the letters
          			character = (char) (character + MOVE_DOWN); //perform the move down shift +3
          			System.out.print(character);
          			
    		}
    
    		System.out.println(); 
    		
    
    	}
    }
    I have part of the code for my cypher to move down 3. But the only thing i am stuck on is how would i keep the spaces in the cyper. so they that i input "Michael Hall" and the output of that is "Encrypted sentence is: Plfkdho#Kdoo" with the # for the space. How would i keep an input with a space to keep the space in the output. along with a dash(-)
    Java Code:
    if ((character >= 'a' && character <= 'z') || (character >='A' && character <= 'Z')){
    	//do whatever u need to do here
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    The caesar cypher is nothing more than a rotational shift of character value. I would find an ASCII table and include a range of all characters that I want included in the cypher including the space character. I'd find the size of this range, divide it in half and use this number to shift each char. In other words add the value to each char, then mod the resulting value by the top char so that the cypher shift wraps around. Then the cypher itself will automatically take care of spaces, capitals, lower case, everything.

    For example, I'd set the START char of my cypher as the ' ' or space char and the END char as te end curly brace or '}' (because it works well), calculate the RANGE and the MID point of the RANGE and then create two static methods, shiftChar and shiftString, in pseudocode:
    Java Code:
    shiftChar is public static, takes a char parameter, c, and returns a char result
       int i is (c - START) // shift chars value so that the start char is at 0
       add MID to i // MID is RANGE / 2, and this shifts all our characters over -- this is the cypher
       mod i by RANGE // RANGE is nothing but END - START + 1, this will ensure that any chars shifted beyond the range will wrap back into the range
       add START back to i // shift char back 
       return i cast as a char
    end method
    
    shiftString is public static, takes a String parameter and returns a String result
       create StringBuilder
       for each char in the parameter String, call shiftChar and place in StringBuilder
       return the toString result of the StringBuilder.
    end method
    Last edited by Fubarable; 10-30-2010 at 12:40 AM.

Similar Threads

  1. symmetric cipher help
    By George Lucas in forum New To Java
    Replies: 2
    Last Post: 12-27-2011, 10:21 PM
  2. vigenere cipher
    By swaart in forum New To Java
    Replies: 7
    Last Post: 11-22-2011, 08:20 PM
  3. How to cipher a string without using Cipher class?
    By arnab321 in forum New To Java
    Replies: 1
    Last Post: 09-09-2009, 12:19 AM
  4. cipher.. Won't work
    By jgonzalez14 in forum New To Java
    Replies: 1
    Last Post: 11-18-2008, 02:22 PM
  5. Caesar and encoding with block spaces
    By Franneldort in forum New To Java
    Replies: 13
    Last Post: 10-30-2008, 05: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
  •