Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default Cryptography Efficiency

    I've been working on a cryptography program lately and was wondering if anyone could look at the code below to see if there is a more efficient way to do the same task.

    The background behind the program is that a user is allowed to enter a password, and then a message. The program then encrypts the message and gives the user the encrypted text. The only way someone can decrypt that message is if they have that users same password.

    In the method below, the parameter message, is obviously the message we want encrypted. encryptedMessage is an empty character array that has the same length as the message. And encryptArray is a character array that holds the "new alphabet". Here's how a new alphabet was formed:

    Password: josh
    New Alphabet: joshabcdefghiklmnpqrtuvwxyz
    Corresponding letter for letter to the regular alphabet: a = j, b = o, etc...
    Each letter from the password is omitted from the regular alphabet.

    Java Code:
    package Application;
    
    public class Encrypt extends Crypto
    {	
    	
    	public Encrypt(Information word){super(word);}
    	
    	public char[] getFinalArray(String message, char[] encryptedMessage, char[] encryptArray)
    	{
    		for(int i = 0; i < encryptedMessage.length; i++)
    			switch(message.charAt(i))
    			{
    				case ' ': 	encryptedMessage[i] = ' '; break;
    				case 'A':	encryptedMessage[i] = encryptArray[0]; break;
    				case 'B':	encryptedMessage[i] = encryptArray[1]; break;
    				case 'C':	encryptedMessage[i] = encryptArray[2]; break;
    				case 'D':	encryptedMessage[i] = encryptArray[3]; break;
    				case 'E':	encryptedMessage[i] = encryptArray[4]; break;
    				case 'F':	encryptedMessage[i] = encryptArray[5]; break;
    				case 'G':	encryptedMessage[i] = encryptArray[6]; break;
    				case 'H':	encryptedMessage[i] = encryptArray[7]; break;
    				case 'I':	encryptedMessage[i] = encryptArray[8]; break;
    				case 'J':	encryptedMessage[i] = encryptArray[9]; break;
    				case 'K':	encryptedMessage[i] = encryptArray[10]; break;
    				case 'L':	encryptedMessage[i] = encryptArray[11]; break;
    				case 'M':	encryptedMessage[i] = encryptArray[12]; break;
    				case 'N':	encryptedMessage[i] = encryptArray[13]; break;
    				case 'O':	encryptedMessage[i] = encryptArray[14]; break;
    				case 'P':	encryptedMessage[i] = encryptArray[15]; break;
    				case 'Q':	encryptedMessage[i] = encryptArray[16]; break;
    				case 'R':	encryptedMessage[i] = encryptArray[17]; break;
    				case 'S':	encryptedMessage[i] = encryptArray[18]; break;
    				case 'T':	encryptedMessage[i] = encryptArray[19]; break;
    				case 'U':	encryptedMessage[i] = encryptArray[20]; break;
    				case 'V':	encryptedMessage[i] = encryptArray[21]; break;
    				case 'W':	encryptedMessage[i] = encryptArray[22]; break;
    				case 'X':	encryptedMessage[i] = encryptArray[23]; break;
    				case 'Y':	encryptedMessage[i] = encryptArray[24]; break;
    				case 'Z':	encryptedMessage[i] = encryptArray[25]; break;
    			}
    		return encryptedMessage;
    	}
    }

    If you need any more information or any more code snips just ask. And thanks in advance for taking a look.

    EDIT: I've attached a working [BETA] program in hopes that may help.
    Attached Files Attached Files
    Last edited by joshdgreen; 10-25-2010 at 08:34 AM.
    Sincerely, Joshua Green
    Please REP if I help :)

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    One thing, in the encrypted message you've the same sequence. It's easy to break with a brute-force attack.

    How about if the user enter a password of length 26 characters?

  3. #3
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Quote Originally Posted by Eranga View Post
    One thing, in the encrypted message you've the same sequence. It's easy to break with a brute-force attack.

    How about if the user enter a password of length 26 characters?

    What do you mean by the same sequence? Do you mean that it always does the encryption the exact same way?

    And any duplicate letters in the password get deleted. So if the user enters:

    Password: aabbcc
    The real password would end up being: abc
    Sincerely, Joshua Green
    Please REP if I help :)

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    I'm talking about this.

    Password: josh
    New Alphabet: joshabcdefghiklmnpqrtuvwxyz
    Corresponding letter for letter to the regular alphabet: a = j, b = o, etc...
    Each letter from the password is omitted from the regular alphabet.

  5. #5
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    How would you recommend doing it?
    Sincerely, Joshua Green
    Please REP if I help :)

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    There are many ways actually. Just looking at your crypto pattern, I think even with Caesar cipher you can build up a strong pattern.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    According to your pattern, at the very first part of the encrypted message could find the password. In almost all brute-force attacks start the navigation from the begging or at the end.

  8. #8
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    I would think my code is better than a Caesar Cipher isn't it? The cipher just shifts letters but mine takes them out of order completely. Or were you saying to use my algorithm and then apply a Caesar Cipher?

    Could you explain a little bit more on how the password could be found in this method? There are no variables at this point in the code that hold the password. And encryptedMessage[] holds all null values when passed in.
    Sincerely, Joshua Green
    Please REP if I help :)

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    I think we messup with this. I'm not saying that your encryption is weak. I'm talking about the new alphabet. I'm comparing with that.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,765
    Blog Entries
    7
    Rep Power
    21

    Default

    You can condense that big ugly switch statement to:

    Java Code:
    char c= message.charAt(i);
    if (c >= 'A' && c <= 'Z')
       encryptedMessage[i] = encryptArray[c-'A'];
    else if (c == ' ')
       encryptMessage[i]= c;
    It doesn't change the 'efficiency' much but it looks so much nicer ;-)

    kind regards,

    Jos

  11. #11
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Quote Originally Posted by Eranga View Post
    I think we messup with this. I'm not saying that your encryption is weak. I'm talking about the new alphabet. I'm comparing with that.

    Oh! I see what you are talking about now. *facepalm* Thanks :)
    Sincerely, Joshua Green
    Please REP if I help :)

  12. #12
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    You can condense that big ugly switch statement to:

    Java Code:
    char c= message.charAt(i);
    if (c >= 'A' && c <= 'Z')
       encryptedMessage[i] = encryptArray[c-'A'];
    else if (c == ' ')
       encryptMessage[i]= c;
    It doesn't change the 'efficiency' much but it looks so much nicer ;-)

    kind regards,

    Jos

    This helps a ton, I appreciate it. It's always nice to have a fresh set of eyes on a code that you've been staring at for awhile :)
    Sincerely, Joshua Green
    Please REP if I help :)

  13. #13
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    You can condense that big ugly switch statement to:

    Java Code:
    char c= message.charAt(i);
    if (c >= 'A' && c <= 'Z')
       encryptedMessage[i] = encryptArray[c-'A'];
    else if (c == ' ')
       encryptMessage[i]= c;
    It doesn't change the 'efficiency' much but it looks so much nicer ;-)

    kind regards,

    Jos

    When I tried to implement this code I ran into a few problems. When I debugged I would get to this line:

    Java Code:
    encryptedMessage[i] = encryptArray[c-'A'];

    Password: josh
    My Message: hello josh (which is converted to uppercase)
    The output: daiim fmrd

    When I try to decrypt, the 'h' comes out fine, but when the statement evaluates with 'a' the output is a 'j'. Here's the complete output:

    It should read: hello josh
    But I get: hjeek bkqh

    UPDATED CODE:

    Java Code:
    package Application;
    
    public class Decrypt extends Crypto
    {	
    	public Decrypt(Information word){super(word);}
    	
    	public char[] getFinalArray(String message, char[] encryptedMessage, char[] encryptArray)
    	{
    		for(int i = 0; i < encryptedMessage.length; i++)
    		{
    		     char c = message.charAt(i);
    		     if (c >= 'A' && c <= 'Z')
    		          encryptedMessage[i] = encryptArray[c - 'A'];
    		     else if (c == ' ')
    			  encryptedMessage[i] = c;
    		}
    					
    		return encryptedMessage;
    	}	
    }
    Last edited by joshdgreen; 10-29-2010 at 06:58 AM.
    Sincerely, Joshua Green
    Please REP if I help :)

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,765
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by joshdgreen View Post
    Password: josh
    My Message: hello josh (which is converted to uppercase)
    The output: daiim fmrd

    When I try to decrypt, the 'h' comes out fine, but when the statement evaluates with 'a' the output is a 'j'. Here's the complete output:

    It should read: hello josh
    But I get: hjeek bkqh
    Your encryption string is incorrect; have a look:

    Java Code:
    joshabcdefghijklmnopqrstuvwxyz <-- encryption key
    abcdefghijklmnopqrstuvwxyz.... <-- original alphabet
    
    hello josh <-- original string
    dahhk fkod <-- encrypted string
    kind regards,

    Jos

  15. #15
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    I'm sorry I should have posted more. The info looks like this:

    Java Code:
    joshabcdefgiklmnpqrtuvwxyz   <-- encryption key
    abcdefghijklmnopqrstuvwxyz   <-- original alphabet
    
    hello josh      <-- original string
    daiim fmrd      <-- encrypted string
    
    Decryption == not "hello josh" but "hjeek bkqh"
    Sincerely, Joshua Green
    Please REP if I help :)

  16. #16
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    I've attached a test program so you can see first hand.
    Attached Files Attached Files
    Sincerely, Joshua Green
    Please REP if I help :)

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,765
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by joshdgreen View Post
    I've attached a test program so you can see first hand.
    Where is/are the source code file(s)?

    kind regards,

    Jos

  18. #18
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    Where is/are the source code file(s)?

    kind regards,

    Jos

    I've attached all the files below. It's just a hobby project I'm working on, not an assignment or anything, so no rush. I appreciate everyone's help. :)
    Attached Files Attached Files
    Sincerely, Joshua Green
    Please REP if I help :)

  19. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,765
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by joshdgreen View Post
    I'm sorry I should have posted more. The info looks like this:

    Java Code:
    joshabcdefgiklmnpqrtuvwxyz   <-- encryption key
    abcdefghijklmnopqrstuvwxyz   <-- original alphabet
    
    hello josh      <-- original string
    daiim fmrd      <-- encrypted string
    
    Decryption == not "hello josh" but "hjeek bkqh"
    Your encryption key doesn't contain a sequence of consecutive characters. So you need both Strings (for en- and decrypting); encrypting goes like this:

    Java Code:
    char encrypt(char c) {
       if (c == ' ') return c;
       return enc.charAt(orig.indexOf(c));
    }
    (orig is the String "abcd ... xyz" and enc is the encryption key). Decrypting goes like this:

    Java Code:
    char decrypt(char c) {
       if (c == ' ') return c;
       return orig.charAt(enc.indexOf(c));
    }
    Note the similarity between the two little methods; you can transmogrify them into one method.

    kind regards,

    Jos

  20. #20
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Thanks, I'll mess around with this for a bit.
    Sincerely, Joshua Green
    Please REP if I help :)

Page 1 of 2 12 LastLast

Similar Threads

  1. Efficiency of code...
    By Inventor22 in forum New To Java
    Replies: 6
    Last Post: 09-26-2010, 11:24 AM
  2. URLConnection Efficiency
    By Lil_Aziz1 in forum New To Java
    Replies: 22
    Last Post: 08-19-2010, 07:27 PM
  3. An Efficiency Question
    By Revenna in forum Java 2D
    Replies: 0
    Last Post: 06-25-2010, 08:22 AM
  4. cryptography
    By swathi palla in forum AWT / Swing
    Replies: 2
    Last Post: 02-19-2009, 02:51 AM
  5. method efficiency
    By TheWave in forum Advanced Java
    Replies: 0
    Last Post: 02-13-2008, 05:11 AM

Posting Permissions

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