簡體   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