Encryption Program Changing Indexes of Strings

2019-07-27 08:12发布

public class Encryption {
private static final int[] encrypt = {2, 9, 3, 4, 6, 8, 1, 0};
private static final int[] decrypt = new int[8];
private static final int minLength = 10;

String encrypt (String password)    {
    if(password.length()<minLength) {
        return password;

    }   else {
        char[] arrayEncrypted = password.toCharArray();
        for (int i = 0; i < encrypt.length; i++)    {
            arrayEncrypted[i] = (char) (arrayEncrypted[i]);

            }
            return String.valueOf(arrayEncrypted);

    }
}

String decrypt (String password)    {
    if (password.length()<minLength)    {
        return password;

    }   else {
        char[] arrayDecrypted = password.toCharArray();
        for (int i = 0; i < arrayDecrypted.length; i++) {
            arrayDecrypted[i] = (char) (arrayDecrypted[i]);
        }
        return String.valueOf(arrayDecrypted);
    }
}

boolean isValidLength (String password) {
    if (password.length()<minLength)    {
        return true;

    }   else    {
        return false;
    }
}

int getMinLength(){
    return minLength;
}

}

This program is suppose to use a classic permutation function to encrypt data. The encryption function to be used in the encryption class is only(2, 9, 3, 4, 6, 8, 1, 0).

Each character in the password is moved to a new index according to the permutation. Since 2 is immediately followed by 9 in the permutation, the character at index 2 of the password must be reassigned to index 9. Similarly, the character in index 9 must be reassigned to index 3 and so on. The last reassignment completes the cycle by placing the character originally at index 0 into index 2.

Just checking because my program is very off, and I need some help to put it back on track. Below is a picture of what it is suppose to look like when compiled.

Compiled

1条回答
小情绪 Triste *
2楼-- · 2019-07-27 08:19

Look carefully at this line.

arrayEncrypted[i] = (char) (arrayEncrypted[i]);

Unless I'm totally misinterpreting it, what you're doing there is... actually, absolutely nothing. You're taking the character found at position i in the array, and placing it at position i. What you want to do is something like

encryptionResult[i] = (arrayEncrypted[encrypt[i]]);

as you're trying to rearrange the characters.

查看更多
登录 后发表回答