anon
anon

Reputation:

Trying to write a decryption function in Java

I'm finished with the plugin finally, and started working on another project. That project is a simple bit of software to encrypt a string given an encryption key and the string itself. So I wrote that, and it seems to be working fine, but I can't figure out how to decrypt it. I used to have an array with the encrypted alphabet, but I figured out a way to do without that in the encryption function and there should be a way to do without it in the decryption as well.

My encryption function:

public static String e(String toEncrypt, int encKey) {
    encKey %= ALPHABET.length;
    toEncrypt = toEncrypt.toLowerCase();
    char[] TEChar = toEncrypt.toCharArray();
    for (int i = 0; i < toEncrypt.length(); i++) {
        for (int j = 0; j < ALPHABET.length; j++) {
            if (TEChar[i] == '`') {
                TEChar[i] = '_';
            }
            else if (TEChar[i] == ALPHABET[j]) {
                TEChar[i] = ALPHABET[(j + encKey) % ALPHABET.length];
                break;
            }
        }
    }
    toEncrypt = String.valueOf(TEChar) + "`" + encKey;
    return toEncrypt;
}

ALPHABET (the array, and I realize that it contains more than just the alphabet.):

 char[] ALPHABET = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ' ', '1',
'2', '3', '4', '5', '6', '7', '8', '9', '0', ',', '.', ';', ':', '[', ']', '{',
'}', '?'};

Rather than making a new array just to have the offset characters, I did that with a simple addition and use of the remainder operator. I tried using subtraction and the same thing, but it didn't work as I would need it to wrap around to 26. Somehow I get the feeling that something glaringly obvious is the solution.

Upvotes: 1

Views: 733

Answers (1)

Kamil
Kamil

Reputation: 431

Try this:

public static String e(String toEncrypt, int encKey, boolean addEncKey) {       
    encKey %= ALPHABET.length;
    toEncrypt = toEncrypt.toLowerCase();
    char[] TEChar = toEncrypt.toCharArray();
    for (int i = 0; i < toEncrypt.length(); i++) {
        for (int j = 0; j < ALPHABET.length; j++) {
            if (TEChar[i] == '`') {
                TEChar[i] = '_';
            } else if (TEChar[i] == ALPHABET[j]) {
                TEChar[i] = ALPHABET[(j + encKey + ALPHABET.length)
                        % ALPHABET.length];
                break;
            }
        }
    }
    if (addEncKey) {
        toEncrypt = String.valueOf(TEChar) + "`" + encKey;
    } else {
        toEncrypt = String.valueOf(TEChar);
    }
    return toEncrypt;
}

This how I tested it:

public static void main(String[] args) {
        String encoded = e("ABDSDfz}", 2, true);
        System.out.println(encoded);
        String decoded = e(
encoded.substring(0, encoded.indexOf("`")),
                -Integer.parseInt(encoded.substring(encoded.indexOf("`") + 1)),
                false);
        System.out.println(decoded);

    }

As you can see I moved some logic to method invocation (you can easily write a decryption function that wrapes around encryption like this:

public static String decrypt(String encoded) {
return e(encoded.substring(0, encoded.indexOf("`")),
                    -Integer.parseInt(encoded.substring(encoded.indexOf("`") + 1)),
                    false);
}

The decrypion and encryption works basically in the same way but decryption uses opposite encKey (for example instead of 22 it will use -22), this done in invocation. You add encKey after '`' sign - I didn't wanted that in decoded message so I cut it out when parsing encoded string to method and added booleam parameter addEncKey so I can disable adding addEncKey. I also changed in the code this line: TEChar[i] = ALPHABET[(j + encKey + ALPHABET.length) % ALPHABET.length]; This is an easy way to deal with negative indexes that can happen when encKey is negative.

Upvotes: 1

Related Questions