加密程序更改字符串的索引 [英] Encryption Program Changing Indexes of Strings
问题描述
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;
}
}
该程序假设使用经典的置换功能来加密数据.加密类中要使用的加密函数为only(2,9,3,4,6,6,8,1,0).
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).
密码中的每个字符都会根据排列顺序移动到新索引.由于排列后立即在2后面紧跟9,因此必须将密码索引2处的字符重新分配给索引9.类似地,索引9中的字符必须重新分配给索引3,依此类推.最后一次重新分配通过将原来位于索引0处的字符放置到索引2中来完成循环.
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.
推荐答案
仔细查看此行.
arrayEncrypted[i] = (char) (arrayEncrypted[i]);
除非我完全误解了它,否则您在这里所做的...实际上,完全没有.您将获取在数组中位置i
处找到的字符,并将其放置在位置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]]);
在尝试重新排列字符时.
这篇关于加密程序更改字符串的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!