加密/解密程序无法正常运行 [英] Encryption/decryption program not working properly
问题描述
我正在尝试构建一个程序,该程序将ascii值从32到126的字符转换为9个空格以对其进行加密.然后我想解密它.它可以成功解密大多数值,但最后一组则不能.这是我的代码如下:
I am trying to build a program that takes the characters with ascii values from 32 to 126 and shifting them over by 9 spaces to encrypt them. Then I want to decrypt it. It successfully decrypts most of the values but the last bunch do not. Here's my code below:
public static void main(String[] args) {
final int SIZE = 95; // total # of ascii vals being used
int[] availableChars = new int[SIZE];
int[] ePhrase = new int[SIZE];
int[] dePhrase = new int[SIZE];
for(int i = 0; i < SIZE; i++){
availableChars[i] = (i + 32);
}
for(int i = 0; i < SIZE; i++){
ePhrase[i] = encrypt(availableChars[i] - 32);
}
for(int i = 0; i < SIZE; i++){
dePhrase[i] = decrypt(ePhrase[i] - 32);
}
System.out.print("\n");
for(int i = 0; i < SIZE; i++){
System.out.print(i + " ");
}
System.out.print("\n");
for(int i = 0; i < SIZE; i++){
System.out.print((char)availableChars[i] + " ");
if(i >= 10){
System.out.print(" ");
}
}
System.out.print("\n");
for(int i = 0; i < SIZE; i++){
System.out.print((char)ePhrase[i] + " ");
if(i >= 10){
System.out.print(" ");
}
}
System.out.print("\n");
for(int i = 0; i < SIZE; i++){
System.out.print((char)dePhrase[i] + " ");
if(i >= 10){
System.out.print(" ");
}
}
}
public static int encrypt(int val){
return ((val + 9) % 94) + 32;
}
public static int decrypt(int val){
return ((val - 9) % 94) + 32;
}
推荐答案
除了两个小问题,您的算法还可以.第一个缺陷是模运算必须考虑字母的所有字符,即95,而不是Willis在其回答中已经解释的94.
Apart from two minor issues your algorithm is OK. The first flaw is that the modulo-operation must consider all characters of the alphabet and that is 95 instead of 94 as already explained by Willis in his answer.
第二个问题是您使用了错误模运算符.在解密期间,(val - 9)
项可能变为负数.但是,对于负数,模运算的数学定义不同于许多编程语言(包括Java)中的定义.在凯撒密码算法中,数学模运算符是引用的,因此必须在实现中使用.另请参阅:使用原始128 ASCII表使用仿射密码对字符串进行加密/解密
The second problem is that you use a wrong modulo-operator. During decryption the term (val - 9)
can become negative. However, for negative numbers the mathematical definition of the modulo-operation differs from the definition in many programming languages (including Java). In the Caesar-cipher-algorithm the mathematical modulo-operator is referred to and thus, has to be used in the implementation. See also: Encrypting/Decrypting a string using a Affine cipher using the original 128 ASCII table
可以使用Java模运算符%
定义数学模运算符,如下所示:
The mathematical modulo-operator can be defined in terms of the Java modulo-operator %
as follows:
private static int mod(int a, int b) {
return ((a % b) + b) % b;
}
或者,可以使用方法int Math.floorMod(int a, int b)
.另请参见: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Math.html#floorMod(int,int)
Alternatively, the method int Math.floorMod(int a, int b)
can be used. See also: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Math.html#floorMod(int,int)
要使用此mod
运算符(以及适当的除数95
)替换为encrypt
-方法
To use this mod
-operator (and also the proper divisor 95
) replace in your encrypt
-method
return ((val + 9) % 94) + 32;
使用
return mod(val + 9, 95) + 32;
以及您的decrypt
方法
return ((val - 9) % 94) + 32;
使用
return mod(val - 9, 95) + 32;
然后输出变为:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ 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 [ \ ] ^ _ ` 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 { | } ~
) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ 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 [ \ ] ^ _ ` 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 { | } ~ ! " # $ % & ' (
! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ 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 [ \ ] ^ _ ` 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 { | } ~
现在,纯文本和解密文本匹配,并且在加密文本中,纯文本的所有字符都移位了9.
Now, plain and decrypted text match and all characters of the plain text are shifted by 9 in the encrypted text.
这篇关于加密/解密程序无法正常运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!