Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: creating a ceaser cipher

  1. #1
    Member
    Join Date
    Oct 2010
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default creating a ceaser cipher

    Hi Guys,

    As part ofr a little project im working on i need to create a program whcih reads in a text file and encrypts it using a ceaser cipher.

    "The Caesar Cipher is one of the oldest and simplest methods of substitution which was used by Julius Caesar, who added 3 to each of his letter numbers to conceal the real message. For example, he would add 3 to the first letter A to make D. The alphabet was treated as a circle, so when he got to X, he moved 3 letters to Z and then to Y would come back around making it A. Using the Caesar cipher, from "Testing", we get whvwlqj. The Caesar cipher may be summarised in a table, shown below (Murky, 2004)."

    So basically i have a program which reads in the text file into a string.

    say for example its


    what i am trying to do is basically read in the string and shift each character along 3 ( a will become c) . i have tried this by converting it to ascii and +3 but i gte loads of random characters . does anyone know a simple way of doing this


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,307
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: creating a ceaser cipher

    Can you please post the code you have so far? It makes it easier to see how far along you are. Simply adding 3 to a byte ascii value doesn't make it circular, and doesn't take into account lower case and upper case.

  3. #3
    Member
    Join Date
    May 2010
    Posts
    39
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default Re: creating a ceaser cipher

    I put it together as:
    	public static String encrypt(String s) {
    		for(char letter : s.toCharArray())  {
    	                s += (char) ((letter - 32 + 3) % 96 + 32);
                    }
    		return s.substring(s.length() / 2);
    	}
    Last edited by Time; November 14th, 2010 at 09:29 AM.

  4. #4
    Member
    Join Date
    Oct 2010
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: creating a ceaser cipher

    I dont really need to post the code because im going to start again.

    I did try and change the plaintext character to its ascii number + 3 and then convert it back however i can only use charcters in the range of 33-126 (http://www.cs.utk.edu/~pham/ascii_table.jpg) I understand

    So for example i have

    text: Hello123~
    ascii: H=72, e=101, l=108, l=108, o=111, 1=49, 2=50, 3=51, ~=176

    if i was to for example add 1 to each ascii number the ~ will be come 177 which when converted back to its charcter will be the DEL caharcter. So i need some wat to wrap it around so if it goes over 176 it goes back to 33 and up .

    Also i have had problems when i convert the test to a Char array the spaces become '#' characters

Similar Threads

  1. creating a package
    By x3rubiachica3x in forum Object Oriented Programming
    Replies: 6
    Last Post: September 26th, 2010, 11:10 PM
  2. Creating new instance
    By vluong in forum Object Oriented Programming
    Replies: 2
    Last Post: November 28th, 2009, 10:35 PM
  3. creating a gui
    By rsala004 in forum AWT / Java Swing
    Replies: 2
    Last Post: July 21st, 2009, 02:17 AM
  4. TCP chat attaching cipher is not running
    By Koren3 in forum Java Networking
    Replies: 4
    Last Post: May 19th, 2009, 02:52 AM
  5. [SOLVED] Interesting error cipher while sending message
    By Koren3 in forum Java Networking
    Replies: 0
    Last Post: April 29th, 2009, 09:54 AM