首先,正如 mypetlion 所提到的,模运算 a % b 等于 a 除以 b 的余数。例如。 11 % 4 = 3, 8 % 15 = 8。准确地说,是值 k 满足 b * n + k = a, b * (n + 1) > a, k
至于为什么模数应该是 128 而不是 126,7 位 ascii 码从 0 到 127,总共 128 个值。另外我对 +32 的用途有点困惑,因为你已经 +k 了。
您也可以完全省略 if 块,因为对于范围 [0, 128) 中的任何 c,c % 128 = c。
所以我会写如下代码:
public static String encrypt(String s, int k) {
char[] arr = s.toCharArray();
for(int i = 0; i < arr.length; i++){
arr[i] = (char) ((arr[i] + k) % 128);
}
return String.valueOf(arr);
}
public static String decrypt(String s, int k) {
char[] arr = s.toCharArray();
for(int i = 0; i < arr.length; i++){
arr[i] = (char) ((arr[i] + 128 - k % 128) % 128);
// + 128 - k % 128 because I don't want do deal with negative numbers.
}
return String.valueOf(arr);
}
我相信我的代码可以正常工作(对于所有正的 k 值),但我无法解释为什么你的代码会产生一些疯狂的高 ascii 值。当我在我的 IDE 上运行你的代码时,它没有这样做,我也无法从你的代码中看到它为什么会这样。
最后,这种密码方法可以加密和解密,但请注意,ascii 确实包含许多不适合打印的控制字符。因此,如果您想将密码词汇限制为仅限字母和标点符号,则需要进行一些字符映射来限制要加密和加密的字符。这将要复杂得多,更不用说您必须考虑诸如 Windows 中的 CR+LF 和 Unix 中的 LF 之类的问题。下面是一个简单的例子,只对字母进行加解密。
public static int asciiToCustom(char ascii) {
// Maps 65-90 & 97-122 to 0-51.
int customCode;
if(ascii >= 65 && ascii <= 90){
customCode = ascii - 65;
}
else if(ascii >= 97 && ascii <= 122){
customCode = ascii - 71;
}
else{
throw new RuntimeException("not a letter!");
}
return customCode;
}
public static char customToAscii(int custom) {
// Maps 0-51 to 65-90 & 97-122.
int ascii;
if(custom >= 0 && custom <= 25){
ascii = custom + 65;
}
else if(custom >= 26 && custom <= 51){
ascii = custom + 71;
}
else{
throw new RuntimeException("not a valid custom code!");
}
return (char) ascii;
}
public static String encrypt(String s, int k) {
char[] arr = s.toCharArray();
for(int i = 0; i < arr.length; i++){
if(Character.isLetter(arr[i])){
arr[i] = customToAscii((asciiToCustom(arr[i]) + k) % 52);
}
}
return String.valueOf(arr);
}
public static String decrypt(String s, int k) {
char[] arr = s.toCharArray();
for(int i = 0; i < arr.length; i++){
if(Character.isLetter(arr[i])){
arr[i] = customToAscii((asciiToCustom(arr[i]) + 52 - k % 52) % 52);
// + 52 - k % 52 because I don't want do deal with negative numbers.
}
}
return String.valueOf(arr);
}