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:

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

EDIT: I've attached a working [BETA] program in hopes that may help.
Last edited by joshdgreen; 10-25-2010 at 07:34 AM.

2. 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. 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

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. How would you recommend doing it?

6. 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. 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. 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.

9. 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. 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. 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 :)

12. Originally Posted by JosAH
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 :)

13. Originally Posted by JosAH
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'];`

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:

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.

14. 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:

Java Code:
```joshabcdefghijklmnopqrstuvwxyz <-- encryption key
abcdefghijklmnopqrstuvwxyz.... <-- original alphabet

hello josh <-- original string
dahhk fkod <-- encrypted string```
kind regards,

Jos

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"```

16. I've attached a test program so you can see first hand.

17. 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

18. 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. :)

19. Originally Posted by joshdgreen
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. Thanks, I'll mess around with this for a bit.

Page 1 of 2 12 Last

#### Posting Permissions

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