Results 1 to 12 of 12
  1. #1
    manfredo is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default Caesar Encrypt and Decrypt

    hey guys,

    i am absolutely a noob when it comes to programming. at the moment i have got this:

    Java Code:
    public class CaesarClass {
       String alphabet ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
       String plaintext;
       int key;
    
       public String encrypt() {
           StringBuffer buf =  new StringBuffer();
           for(int i =0; i<plaintext.length(); i++) {
           char c = plaintext.charAt(i);
           int index = alphabet.indexOf(c);
    
          buf.append(alphabet.charAt((index + key + 26) % 26));
           }
          return buf.toString();
       }
    
       public CaesarClass(String s, int k ) {
           plaintext = s;
           key = k;
       }
    
    }
    the problem is that only capital letters work. but how do i include low case letters?
    could you help me? ;)

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

    Default

    You could change the input String to upper case via a method of the String class. Otherwise you're going to have to put a little more thought into your logic, perhaps have two alphabet variables, one for upper case and one for lower case and an if block to distinguish which one to use, depending on if the input char is upper case or lower case.

  3. #3
    manfredo is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    as i said i havent got a clue what i am doing there :(
    could you please correct and add the missing things to my code?
    i would really appreciate that. Its nice that you try to help me by teaching me how it works but i cant programme! :((

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

    Default

    Quote Originally Posted by manfredo View Post
    as i said i havent got a clue what i am doing there :(
    could you please correct and add the missing things to my code?
    No, that's not how it works here.


    i would really appreciate that. Its nice that you try to help me by teaching me how it works but i cant programme! :((
    We'll try to help you, but you must first be completely clear about what confuses you. This code has some sophisticated stuff in it, in particular the use of mods to shift the char values, and your questions are somewhat basic which leads me to ask, is this your code that you've written, or have you borrowed it from online?

  5. #5
    manfredo is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    i havent borrowed it from someone else. in school we are a group of 2 who are working on this programme. he is good at it and wrote this for me but at the moment he cant finish it because he feels sick..

  6. #6
    manfredo is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    well i know how it would work but i dont know how to write it.
    So if the plaintext has small letters use the lower case alphabet and if it has capital ones use the capital ABC.
    but how do i write that?

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

    Default

    Character class has a method isUpperCase that takes a character parameter and returns a boolean true if the character passed in is upper case, and false if lower case. You can use this in an if block to decide which String to use as the alphabet.

  8. #8
    manfredo is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    sorry man, but i cant do that . i will fail then :(

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

    Default

    Quote Originally Posted by manfredo View Post
    sorry man, but i cant do that . i will fail then :(
    What do you mean "you can't do that"? Is it not allowed for you to have an if block in this program?

    Or do you mean you don't understand how to create if blocks. If that's the case, then all you have to do is go to the tutorial and read up on them. You can find the Oracle Java tutorial here:

    Java Tutorial Really Big Index
    If / else tutorial

    Please be clear on a point though, that no one here is going to write your code for you. We will try to help you learn, but you are responsible for writing your code, and if you can't do that, then yes you will fail, but if you put in the effort, then no, you won't.
    Last edited by Fubarable; 01-22-2011 at 10:38 PM.

  10. #10
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Look at this book and its examples. a simple caesar encrypt and decrypt is shown in example Listing4801.java. good luck.

  11. #11
    manfredo is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    public String encrypt() {
    StringBuffer buf = new StringBuffer();
    for(int i =0; i<plaintext.length(); i++) {
    if (small letter ) { then use this alphabet }
    char c = plaintext.charAt(i);
    int index = alphabet.indexOf(c);

    buf.append(alphabet.charAt((index + key + 26) % 26));
    }
    return buf.toString();
    }

    you can see that i am an amateur.

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

    Default

    Quote Originally Posted by manfredo View Post
    public String encrypt() {
    StringBuffer buf = new StringBuffer();
    for(int i =0; i<plaintext.length(); i++) {
    if (small letter ) { then use this alphabet }
    char c = plaintext.charAt(i);
    int index = alphabet.indexOf(c);

    buf.append(alphabet.charAt((index + key + 26) % 26));
    }
    return buf.toString();
    }

    you can see that i am an amateur.
    Please read the tutorials I linked to above. You can't guess at programming, and you need to do some catch up studying it seems.

    Also, your story about your "partner" is full of holes, but that's another story entirely that I won't dwell on here.

    Good luck.

Similar Threads

  1. Caesar Cipher?
    By socboy6579 in forum Advanced Java
    Replies: 5
    Last Post: 10-29-2010, 10:59 PM
  2. Need Help for encrypt/decrypt.
    By superdhebz in forum New To Java
    Replies: 5
    Last Post: 09-17-2010, 06:26 PM
  3. How to decrypt SSL?
    By Sergio in forum Advanced Java
    Replies: 1
    Last Post: 08-21-2010, 05:03 AM
  4. Encrypt and decrypt SMS
    By ZeCute in forum CLDC and MIDP
    Replies: 8
    Last Post: 06-13-2010, 05:01 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
  •