[英]Algorithm will not encrypt/decrypt properly
I tested the below code with all ASCII values from 64 - 90 inclusive (All uppercase letters) and adjusted accordingly so instead of: 我用下面的代码测试了所有ASCII值,从64-90(含)(所有大写字母),并进行了相应的调整,而不是:
for(int i = 0 ; i < c.length(); i++){
info[i] = ((int)c.charAt(i) - 32);
}
I would replace the 32 with 64 (so the ASCII value of A would save in the array as 0). 我将32替换为64(因此A的ASCII值将在数组中另存为0)。 Furthermore, in my encryption and decryption functions I would replace 95 with 26 (26 letters). 此外,在加密和解密功能中,我将95替换为26(26个字母)。
However, if I apply this to all values between 32-126 inclusive (95 characters) and adjust the values accordingly, the values become incorrect and I don't know why. 但是,如果我将此值应用于32-126之间(包括95个字符)的所有值并相应地调整值,则这些值将变得不正确,并且我不知道为什么。 Here is my whole main function below (note that the formula used in encryption and decryption is just an example one I used and I plan on changing the values later on): 这是我下面的整个主要功能(请注意,加密和解密中使用的公式只是我使用的一个示例,我打算稍后更改值):
public static void main(String[] args) {
String c = "sd344rf"; // could be any set of characters within the range
int[] e = new int[c.length()]; // encrypted set
int[] d = new int[c.length()]; // decrypted set
int[] info = new int[c.length()];
for(int i = 0 ; i < c.length(); i++){
info[i] = ((int)c.charAt(i) - 32);
}
for(int i = 0; i < c.length(); i++){
e[i] = encryption(info[i]);
}
for(int i = 0; i < c.length(); i++){
d[i] = decryption(e[i]);
}
display(info);
System.out.println();
display(e);
System.out.println();
display(d);
}
public static int encryption(int x){
return mod(3*x + 9,95);
}
public static int decryption(int x){
return mod(9*x - 3,95);
}
public static void display(int[] arr){
for(int i = 0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
}
}
Obviously you are trying to implement an affine cipher. 显然,您正在尝试实现仿射密码。 For an affine cipher the encryption is 对于仿射密码,加密为
y = mod(n * x + s, m)
and the decryption 和解密
x = mod(ni * (y - s), m)
with 与
x: Value of the character to encrypt
y: Value of the encrypted character
m: Number of characters in the underlying alphabet
n, s: Key of the encryption
n
and s
must be chosen so that they are between 0
and m - 1
, inclusive. n
和s
必须选择为介于0
和m - 1
(含)之间。 In addition, n
has to be chosen so that n
and m
are coprime. 此外, n
必须被选择,使得n
和m
互质。 ni
is the modular multiplicative inverse of n
modulo m
and is determined by n*ni mod m = 1
. ni
是n
模m
的模乘逆,由n*ni mod m = 1
。
This is in more detail explained at https://en.wikipedia.org/wiki/Affine_cipher . 有关详细信息, 请参见https://en.wikipedia.org/wiki/Affine_cipher 。
If the values u, v
associated with the characters don't start at 0
the values have to be shifted by an offset equal to the value of the first character (provided that there are no gaps) and the formulas become 如果与字符关联的值u, v
并非从0
开始,则必须将值偏移等于第一个字符的值的偏移量(假设没有间隙),并且公式变为
x = u - offset
y = v - offset
v = mod(n * (u - offset) + s, m) + offset
u = mod(ni * ((v - offset) - s), m) + offset
Thus, you've to replace in the main
-method 因此,您必须替换main
方法
info[i] = ((int)c.charAt(i) - 32);
with 与
info[i] = (int)c.charAt(i);
The encryption
-method becomes: encryption
方法变为:
public static int encryption(int u) {
return mod(n * (u - offset) + s, m) + offset;
}
and the decryption
-method 和decryption
方法
public static int decryption(int v) {
return mod(ni * ((v - offset) - s), m) + offset;
}
with the fields 与田野
private static int m = <Number of the characters in the alphabet>;
private static int n = <Key (factor)>; // n between 0 and m-1 and moreover, n and m have te be coprime
private static int s = <Key (summand)>; // s between 0 and m-1
private static int offset = <Value of the first character of the alphabet>;
private static int ni = <Modular multiplicative inverse of n modulo m>;
Moreover, for the mod
-operation the following method is used (see Encryption/decryption program not working properly ): 此外,对于mod
操作,使用以下方法(请参阅加密/解密程序无法正常工作 ):
private static int mod(int a, int b) {
return ((a % b) + b) % b;
}
Example 1: Uppercase letters A - Z: 示例1:大写字母A-Z:
private static int m = 'Z' - 'A' + 1; // 26
private static int n = 3; // Choose e.g. n = 3: n = 3 < 26 - 1 = 25 and moreover, 3 and 26 are coprime
private static int s = 9; // Choose e.g. s = 9: s = 9 < 26 - 1 = 25
private static int offset = 'A'; // 65
private static int ni = 9; // 3*9 mod 26 = 1
Test: 测试:
String c = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Output (with characters instead of their values): 输出(用字符代替它们的值):
Plain text: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Encrypted text: JMPSVYBEHKNQTWZCFILORUXADG
Decrypted text: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Example 2: All characters between 32 (Space) and 126 (~), inclusive: 示例2:32(空格)到126(〜)之间的所有字符,包括:
private static int m = '~' - ' ' + 1; // 95
private static int n = 3; // Choose e.g. n = 3: n = 3 < 95 - 1 = 94 and moreover, 3 and 95 are coprime
private static int s = 9; // Choose e.g. s = 9: s = 9 < 95 - 1 = 94
private static int offset = ' '; // 32
private static int ni = 32; // 3*32 mod 95 = 1
Test: 测试:
String c = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
Output (with characters instead of their values): 输出(用字符代替它们的值):
Plain text: !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
Encrypted text: ),/258;>ADGJMPSVY\_behknqtwz}!$'*-0369<?BEHKNQTWZ]`cfilorux{~"%(+.147:=@CFILORUX[^adgjmpsvy| #&
Decrypted text: !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.