简体   繁体   中英

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:

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). Furthermore, in my encryption and decryption functions I would replace 95 with 26 (26 letters).

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. 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. In addition, n has to be chosen so that n and m are coprime. ni is the modular multiplicative inverse of n modulo m and is determined by n*ni mod m = 1 .

This is in more detail explained at 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

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

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

with

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

The encryption -method becomes:

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

and the decryption -method

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 ):

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

Example 1: Uppercase letters 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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