繁体   English   中英

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

[英]Algorithm will not encrypt/decrypt properly

我用下面的代码测试了所有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] + " ");
    }
}
}

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

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操作,使用以下方法(请参阅加密/解密程序无法正常工作 ):

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{|}~

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM