Help with simple encryption method


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 13 of 13

Thread: Help with simple encryption method

  1. #1
    Join Date
    Feb 2004
    Posts
    12

    Help with simple encryption method

    Hi i need to make a simple encryption method.

    Brief:

    Write a class called Encryption which allows you to encrypt a message using a secret code word and to decrypt an encrypted message.
    // You will need to provide the following methods: A constructor which takes an int parameter and saves it as the encryption key (referred to as k in the description below).
    //A method called encrypt which encrypts a message, taking a String parameter and returning a String result. The result is produced by converting the parameter to upper-case, and replacing each letter by the letter k places further on in the alphabet. Other characters (punctuation, spaces etc.) should be left unchanged.
    //A method called decrypt which decrypts a message, taking a String parameter and returning a String result. The result is produced by converting the parameter to upper-case, and replacing each letter by the letter k places earlier in the alphabet. Other characters (punctuation, spaces etc.) should be left unchanged.

    Heres what i have so far

    Code:
    public class Encryption
    {String s;
    	public Encryption(int k)
    	{
    
    		
    	}
    public String encrypt(String si)
    {
    si = si.toUpperCase();
    
    
    
    
    
    
    return si;
    }
    }
    There are bits of code that i know i will use in it.
    //char c = s.charAt(n); // get the n-th character of s (n = 0 gets the first character)


    //You can convert a letter A to Z into a number 0 to 25 by subtracting 'A':

    // int n = c - 'A'; // convert character c into a number n (0 to 25)
    (((i atempted to put this in for every char but i got null pointer if you put less that 25 characters in)))

    //You can convert a number between 0 to 25 to a letter by adding 'A':

    // char c = (char)(n + 'A'); // convert number n (0 to 25) to a character c


    //You can find the first position of a character in a string using the indexOf method from class String, where the characters are numbered starting from zero:

    // String s = "ABCDE";
    // int n = s.indexOf('D'); // get the position of the first 'D' in s (n = 3 in this case)

    I have made the letters go capital, now i need to split them up into idividual char, then transfer to an int, then add k, then convert back, i just need a little help on how i should put it all together.

    Thanks

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    Well since this is an academic excercise people aren't likely to tell you exactly how to do it (which i'm sure isn't what you were after anyway), so if you write down exactly what you've got so far (as in codewise as well as ideas), and any ideas you have for finishing the excercise, i'm sure people will be more than happy to point you in the right direction.

    First point to note is that the String objects are immutable (ie you can't change them), so you're going to have to make a new String object that holds the encrypted string. One of your methods that you listed will be very helpful in getting the actual characters in the string, and you're going to need to use that to get each of the characters in turn and decide what needs to be done to it.

  3. #3
    Join Date
    Jan 2004
    Posts
    22
    Here's a hint-esque type thing for the encypt one:
    • Convert to upper case
    • Initialize a temporary String, called encypted or something to blank
    • Iterate through each character of si (assuming that's what you're calling the argument)
    • Get it as an integer, subtracting 'A'. This leaves you with 0-25.
    • Add k. Now, you might be over 25, which is bad. If it is over 25, subtract 26.
    • Add 'A' back on and convert to a character
    • Add that character on the encrypted String. Use String.valueOf((char)theLettersNumber) to add the number onto the end.
    • End you iterating section. Return the encrypted String

  4. #4
    Join Date
    Feb 2004
    Posts
    12
    Cheers, i didnt want the exact answer as i need to learn how to do this myself.

  5. #5
    Join Date
    Feb 2004
    Posts
    541
    if you have any other problems let us know

  6. #6
    Join Date
    Feb 2004
    Posts
    808
    this may provide some interesting enlightenment:

    http://www.codeguru.com/forum/search...der=descending

    read all the threads started by kitsune84
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  7. #7
    Join Date
    Feb 2004
    Posts
    12
    Hi again, this is what i have so far. Now i need to add the k but i am unsure how to do so, also is there any errors in what i have done so far, i expect yes.
    Code:
    public class Encryption
    {String s;
    	public Encryption(int k)
    	{
    
    		
    	}
    public String encrypt(String si)
    {
    si = si.toUpperCase();
    int i = 0;
    for (int index = 0; index< si.length(); index++);
    char c = si.charAt(i);
    int n = c - 'A';
    
    return si;
    }
    }

  8. #8
    Join Date
    Feb 2004
    Posts
    541
    well your constructor accepts an interger as a parameter (that is going to be the key I assume), but doesn't do anything with it at all. Also, you have a string class variable, but the method for encryption takes a string as a parameter and uses that to do the encryption on. I think it would make more sense to change the string class variable to an integer (for the key), and then have the constructor set the key using the parameter you supply when you make a new object.
    Code:
    public class Encryption
    {
        int key;
    
        public Encryption(int k)
        {
            key = k;
        }
    
        //rest of the class
    }
    For the rest of the class bit you can have an encryption method and a decryption method, and they will work on any string you supply to them (as long as they were encrypted by this encryption object of course).

  9. #9
    Join Date
    Feb 2004
    Posts
    12
    Ok, one last question, heres my full code so far.
    Code:
    public class Encryption
    {
        int key;
    
        public Encryption(int k)
        {
            key = k;
        }
    
        //rest of the class
    
    public String encrypt(String si)
    {
    
    si = si.toUpperCase();
    
    String result = "";
    
    for (int index = 0; index< si.length(); index++)
    {
    
    char c = si.charAt(index);
    
    
    
    
    
    int n = c - 'A' + key;
    char a = (char)(n + 'A');
    
    result = result + a;
    }
    
    return result ;
    
    }
    public String decrypt(String bi)
    {
    
    
    
    bi = bi.toUpperCase();
    
    String results = "";
    
    for (int index = 0; index< bi.length(); index++)
    {
    
    char g = bi.charAt(index);
     
    int n = g - 'A' - key;
    char q = (char)(n + 'A');
    results = results + q;
    
    }
    return results ;
    
    }}
    My prob is that in the breif it says leave punctuation as it is but this code changes it, also say if key = 3 and char was z it changes it to something like ^ instead of going back round and becoming c. Any helpfull coding would be nice, thanks.

  10. #10
    Join Date
    Feb 2004
    Posts
    541
    well to make sure you only change letters and not punctuation you need to test each character before you change it. Remember, each character is associated with a numerical value, so you can do something like
    Code:
    if(theChar >= 'a')
    {
        //do something;
    }
    What you need to do is check that the character is between A and Z. If it is then you encrypt it, if it isn't then you don't.

    The reason adding 3 to 'z' doesn't produce 'c' is because 'z' is represented by the integer 122. The integer 125 represents a "}", and there are many other strange characters that are beyond 'z' too. What you need to do is use the modulo operator (represented by a %). This operator returns the remainder of a division, so if you do 10 / 8 you get 2 as the answer. You can use this operator to make sure that when you add 'k' to the character it stays within a certain range.

  11. #11
    Join Date
    Feb 2004
    Posts
    808
    V6.. i know it might seem boring to read other people's posts and think about how to apply the knowledge in them to your own situation, but there's been a hearty discussion about shift ciphering over on codeguru. i posted a liink in this thread a while ago - you might do well to read it
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  12. #12
    Join Date
    Feb 2004
    Posts
    12
    Thanks, i saw it once but now the link is not working.

  13. #13
    Join Date
    Feb 2004
    Posts
    808
    ho ho, yea.. it would appear that to stop people like me linking to the serach engine, the results url has an id that expires after a time...

    do this. go here:

    http://www.codeguru.com/forum/search.php

    search for word:
    CIPHER

    by user:
    CJARD

    read the kitsune84 threads
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center