算法将无法正确加密/解密

时间:2019-02-20 22:23:19

标签: java encryption

我用下面的代码测试了所有ASCII值,范围从64-90(包括所有大写字母),并进行了相应的调整,而不是:

for(int i = 0 ; i < c.length(); i++){
      info[i] = ((int)c.charAt(i) - 32);
  }

我将32替换为64(因此A的ASCII值将在数组中另存为0)。此外,在加密和解密功能中,我将95替换为26(26个字母)。

但是,如果我将此值应用于32-126之间(包括95个字符)的所有值并相应地调整值,则这些值将变得不正确,并且我不知道为什么。这是我下面的整个主要功能(请注意,加密和解密中使用的公式只是我使用的一个示例,我打算稍后更改值):

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] + " ");
    }
}
}

1 个答案:

答案 0 :(得分:1)

很显然,您正在尝试实现仿射密码。对于仿射密码,加密是

y = mod(n * x + s, m)

和解密

x = mod(ni * (y - s), m)

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

ns必须选择为介于0m - 1之间(包括两者)。另外,必须选择n以便nm是互质的。 ninm的模乘逆,由n*ni mod m = 1确定。

这在https://en.wikipedia.org/wiki/Affine_cipher中有更详细的解释。


如果与字符关联的值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

因此,您必须替换main方法

info[i] = ((int)c.charAt(i) - 32);

使用

info[i] = (int)c.charAt(i);

encryption方法变为:

public static int encryption(int u) {
    return mod(n * (u - offset) + s, m) + offset;
}

decryption方法

public static int decryption(int v) {
    return mod(ni * ((v - offset) - s), m) + offset;
}

带有字段

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>;

此外,对于mod操作,使用以下方法(请参见Encryption/decryption program not working properly):

private static int mod(int a, int b) {
    return ((a % b) + b) % b;
}

示例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

测试:

String c = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

输出(用字符代替它们的值):

Plain text:     ABCDEFGHIJKLMNOPQRSTUVWXYZ
Encrypted text: JMPSVYBEHKNQTWZCFILORUXADG
Decrypted text: ABCDEFGHIJKLMNOPQRSTUVWXYZ

示例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

测试:

String c = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; 

输出(用字符代替其值):

Plain text:      !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
Encrypted text: ),/258;>ADGJMPSVY\_behknqtwz}!$'*-0369<?BEHKNQTWZ]`cfilorux{~"%(+.147:=@CFILORUX[^adgjmpsvy| #&
Decrypted text:  !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~