# Cryptography Efficiency

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-25-2010, 07:27 AM
joshdgreen
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:

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.

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;         } }```

EDIT: I've attached a working [BETA] program in hopes that may help.
• 10-25-2010, 12:30 PM
Eranga
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
joshdgreen
Quote:

Originally Posted by Eranga
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:

The real password would end up being: abc
• 10-25-2010, 01:01 PM
Eranga

Quote:

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
joshdgreen
How would you recommend doing it?
• 10-26-2010, 04:42 AM
Eranga
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
Eranga
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
joshdgreen
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.
• 10-27-2010, 07:55 AM
Eranga
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
JosAH
You can condense that big ugly switch statement to:

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
• 10-27-2010, 08:31 PM
joshdgreen
Quote:

Originally Posted by Eranga
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 :)
• 10-27-2010, 08:32 PM
joshdgreen
Quote:

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

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 :)
• 10-29-2010, 05:55 AM
joshdgreen
Quote:

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

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:

Code:

`encryptedMessage[i] = encryptArray[c-'A'];`

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:

But I get: hjeek bkqh

UPDATED CODE:

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;         }        }```
• 10-29-2010, 06:43 AM
JosAH
Quote:

Originally Posted by joshdgreen
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:

But I get: hjeek bkqh

Your encryption string is incorrect; have a look:

Code:

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

Jos
• 10-29-2010, 07:01 AM
joshdgreen
I'm sorry I should have posted more. The info looks like this:

Code:

```joshabcdefgiklmnpqrtuvwxyz  <-- encryption key abcdefghijklmnopqrstuvwxyz  <-- original alphabet hello josh      <-- original string daiim fmrd      <-- encrypted string Decryption == not "hello josh" but "hjeek bkqh"```
• 10-29-2010, 07:04 AM
joshdgreen
I've attached a test program so you can see first hand.
• 10-29-2010, 07:24 AM
JosAH
Quote:

Originally Posted by joshdgreen
I've attached a test program so you can see first hand.

Where is/are the source code file(s)?

kind regards,

Jos
• 10-29-2010, 07:28 AM
joshdgreen
Quote:

Originally Posted by JosAH
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. :)
• 10-29-2010, 07:32 AM
JosAH
Quote:

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

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:

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:

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
• 10-29-2010, 07:43 AM
joshdgreen
Thanks, I'll mess around with this for a bit.
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last