问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/10573760/encryption-program-changing-indexes-of-strings