Results 1 to 20 of 23
Thread: Cryptography Efficiency
- 10-25-2010, 07:27 AM #1
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.Last edited by joshdgreen; 10-25-2010 at 07:34 AM.
Sincerely, Joshua Green
Please REP if I help :)
- 10-25-2010, 12:30 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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?
- 10-25-2010, 12:50 PM #3
Sincerely, Joshua Green
Please REP if I help :)
- 10-25-2010, 01:01 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 10-25-2010, 10:08 PM #5
How would you recommend doing it?
Sincerely, Joshua Green
Please REP if I help :)
- 10-26-2010, 04:42 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
There are many ways actually. Just looking at your crypto pattern, I think even with Caesar cipher you can build up a strong pattern.
- 10-26-2010, 04:44 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 10-26-2010, 05:11 AM #8
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 :)
- 10-27-2010, 07:55 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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-27-2010, 08:26 AM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
You can condense that big ugly switch statement to:
It doesn't change the 'efficiency' much but it looks so much nicer ;-)Java Code:char c= message.charAt(i); if (c >= 'A' && c <= 'Z') encryptedMessage[i] = encryptArray[c-'A']; else if (c == ' ') encryptMessage[i]= c;
kind regards,
Jos
- 10-27-2010, 08:31 PM #11
- 10-27-2010, 08:32 PM #12
- 10-29-2010, 05:55 AM #13
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 05:58 AM.
Sincerely, Joshua Green
Please REP if I help :)
- 10-29-2010, 06:43 AM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
- 10-29-2010, 07:01 AM #15
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 :)
- 10-29-2010, 07:04 AM #16
I've attached a test program so you can see first hand.
Sincerely, Joshua Green
Please REP if I help :)
- 10-29-2010, 07:24 AM #17
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
- 10-29-2010, 07:28 AM #18
- 10-29-2010, 07:32 AM #19
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Your encryption key doesn't contain a sequence of consecutive characters. So you need both Strings (for en- and decrypting); encrypting goes like this:
(orig is the String "abcd ... xyz" and enc is the encryption key). Decrypting goes like this:Java Code:char encrypt(char c) { if (c == ' ') return c; return enc.charAt(orig.indexOf(c)); }
Note the similarity between the two little methods; you can transmogrify them into one method.Java Code:char decrypt(char c) { if (c == ' ') return c; return orig.charAt(enc.indexOf(c)); }
kind regards,
Jos
- 10-29-2010, 07:43 AM #20
Similar Threads
-
Efficiency of code...
By Inventor22 in forum New To JavaReplies: 6Last Post: 09-26-2010, 10:24 AM -
URLConnection Efficiency
By Lil_Aziz1 in forum New To JavaReplies: 22Last Post: 08-19-2010, 06:27 PM -
An Efficiency Question
By Revenna in forum Java 2DReplies: 0Last Post: 06-25-2010, 07:22 AM -
cryptography
By swathi palla in forum AWT / SwingReplies: 2Last Post: 02-19-2009, 01:51 AM -
method efficiency
By TheWave in forum Advanced JavaReplies: 0Last Post: 02-13-2008, 04:11 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks