Classical cryptographic algorithm to achieve

1, can be divided into classical password instead of a password and password for both the replacement, transposition transformation achieved here instead of the password in the password affine transformation and replacement of.

 

2, the affine transformation:

Encryption process: e (x) = ax + b (mod m)

Decryption process: d (e (x)) = a ^ (- 1) * (e (x) - b) mod m

Parameters required: a, m coprime; a, b coprime; m is the number of elements in a set. (E.g., all the elements of the current takes 1 to 9 and a ~ z is as a set, m is 36)

Encryption implementation:

. 1  Import java.util.Scanner;
 2  
. 3  public  class the Main {
 . 4      public  static  void main (String [] args) {
 . 5          int m = 36, thisNum, index = 0; // m is the number of elements in a set (e.g. the current take a 1 to 9 and z all elements as a set of ~, m is 36) 
. 6          Scanner S = new new Scanner (the System.in);
 . 7          // string conversion for an input character array 
. 8          char [] = BUFF s.nextLine () toCharArray ();.
 . 9          // parameters a, b manual input 
10          int A = s.nextInt ();
 . 11          int B = s.nextInt ();
12          @ parameters required: a, m coprime; a, b coprime 
13 is          the while (fun1 (m, A) = || fun1. 1 (Math.max (A, B), Math.min (A, B)! !) =. 1 ) {
 14              System.out.println ( "parameter does not meet the requirements, please re-enter" );
 15              A = s.nextInt ();
 16              B = s.nextInt ();
 . 17          }
 18 is          for ( char I : BUFF) {
 . 19              // by the character into a digital 
20 is              IF (I> '. 9') thisNum = ( int ) I - 87 ;
 21 is              the else thisNum = ( int ) I - 48 ;
 22 is              //The Digital Encryption 
23 is              thisNum = (thisNum * A + B)% m;
 24              // encrypted numbers to characters after the 
25              IF (thisNum <10) BUFF [index ++] = ( char ) (thisNum + 48 );
 26 is              the else BUFF [index ++] = ( char ) (thisNum + 87 );
 27          }
 28          System.out.println (BUFF);
 29          S.CLOSE ();
 30      }
 31 is  
32      // Euclidean algorithm for the two numbers the greatest common factor of 
33 is      public  static  int fun1 ( int A, int B) {
 34 is          return b == 0 ? a : fun1(b, a%b);
35     }
36 }

Decryption achieve:

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4     public static void main(String []args) {
 5         int m = 36, thisNum, index = 0, k;
 6         Scanner s = new Scanner(System.in);
 7         char[] buff = s.nextLine().toCharArray();
 8         int a = s.nextInt();
 9         int b = s.nextInt();
10         while (fun1(m, a) != 1 || fun1(Math.max(a, b), Math.min(a, b)) != 1) {
11             System.out.println ( "parameter does not meet the requirements, please re-enter" );
 12 is              a = s.nextInt ();
 13 is              B = s.nextInt ();
 14          }
 15          // K is the inverse of a modulo m 
16          = K fun2 (a, m);
 . 17          for ( char I: BUFF) {
 18 is              // convert the characters to the encrypted digital 
. 19              IF (I> '. 9') thisNum = ( int ) I - 87 ;
 20 is              the else thisNum = ( int ) I - 48 ;
 21 is              // decryption process D (E (X)) = A ^ (-. 1) * (E (X) -b) m MOD 
22 is             = thisNum ((thisNum-B) * K)% m;
 23 is              // If the result is negative, into a positive number, principle% B = A (A% B + B)% B 
24              IF (thisNum <0) thisNum + = m;
 25              // Finally, the digital conversion of the decrypted character 
26 is              IF (thisNum <10) BUFF [index ++] = ( char ) (thisNum + 48 );
 27              the else BUFF [index ++] = ( char ) (thisNum +87 );
 28          }
 29          System.out.println (BUFF);
 30      }
 31 is  
32      public  static  int fun1 ( int A, int B) {
33 is          return B == 0 a:? Fun1 (B, a% B);
 34 is      }
 35  
36      // cycle find the inverse element of a modulo m 
37 [      public  static  int fun2 ( int a, int m) {
 38 is          for ( int I 0 =; I <m; I ++ ) {
 39              IF (A == * I m. 1% ) {
 40                  A = I;
 41 is                  BREAK ;
 42 is              }
 43 is          }
 44 is          return A;
 45      }
 46 is }

 

3, transposition cipher

The encryption process: all the characters remain the same plaintext, the plaintext rearranged according to certain rules.

Decryption process: the inverse of the encryption process.

Note: The encryption and decryption process is the process of creating the index, that is, which character which put an array storage location, and finally reassembled into ciphertext or plaintext through index.

Example:

Expressly matrix:
asdfg
hjklm
nbvc

Ciphertext matrix:
dagsf
khmjl
vnbc

(Calculated result there is a space between n and b, but the output will remove the spaces)

Plaintext: asdfghjklmnbvc
Key: 31524

Encryption implementation:

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4     public static void main(String []args) {
 5         Scanner s = new Scanner(System.in);
 6         char[] mingwen = s.nextLine().toCharArray();
 7         char[] miyao = s.nextLine().toCharArray();
 8         StringBuffer miwen = new StringBuffer();
 9         int[] poi = new int[miyao.length];
10         int index = 0, thisRow = 0, RealPoi;
 . 11  
12 is          // number of columns and rows of the matrix calculating plaintext 
13 is          int COL = miyao.length;
 14          int Row = (mingwen.length / COL) + (% mingwen.length COL 0 == 0:?. 1 );   // where plaintext matrix processing of the last line may be less than
 15          
16          // key location format (all the key characters are greater than or equal to '1' so that a final deceleration to facilitate subsequent calculations) 
. 17          for ( int I = 0 ; i <poi.length; i ++) POI [i] = Miyao [i] - 48 -. 1 ;
 18 is  
. 19  
20 is          for ( int i = 0; i <* Row COL; i ++ ) {
 21 is              // to calculate the current position of the i real characters (e.g., bit 0 is 2, i.e., the encrypted character d), if the character is present in the clear, it is added to the ciphertext 
22 is              IF((POI realPoi = [index ++] + thisRow * COL) < mingwen.length) miwen.append (mingwen [realPoi]);
 23 is              // if the current position is not a space character instead of 
24              the else miwen.append ( '' );
 25              
26 is              IF (index> = COL) {
 27                  index = index% COL;
 28                  thisRow ++ ;
 29              }
 30          }
 31 is  
32          // ciphertext to spaces (decrypt ciphertext spaces need to be retained) 
33 is          for ( int I = 0; I <miwen.length (); I ++ ) {
 34 is              IF (miwen.charAt (I) == '') Miwen.deleteCharAt (I);
 35          }
 36  
37 [          // outputting the encrypted ciphertext 
38 is          System.out.println (miwen);
 39      }
 40 }

Decryption achieve:

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4     public static void main(String []args) {
 5         Scanner s = new Scanner(System.in);
 6         char[] miwen = s.nextLine().toCharArray();
 7         char[] miyao = s.nextLine().toCharArray();
 8         StringBuffer mingwen = new StringBuffer();
 9         int []poi = new int[miyao.length];
10         int index = 0, thisRow = 0, RealPoi;
 . 11  
12 is          // acquires a decryption key and a key format position (the last minus one), i.e. the inverse of the encryption process 
13 is          for ( char C: Miyao) {
 14              POI [( int ) C - 48 -. 1 ] = index ++ ;
 15          }
 16          index = 0 ;
 . 17  
18 is          // number of columns and rows of the matrix ciphertext 
. 19          int COL = miyao.length;
 20 is          int row = (miwen.length / COL) + (COL miwen.length% 0 0 ==:. 1? );
 21 is  
22 is          for ( int I = 0; I <COL * Row; I ++ ) {
 23 is              //Calculate the real current position of the character and the character i is added to the plaintext string 
24              IF ((POI realPoi = [index ++] + thisRow * COL) < miwen.length) mingwen.append (miwen [realPoi]);
 25              
26 is              IF (index> = COL) {
 27                  index = index% COL;
 28                  thisRow ++ ;
 29              }
 30          }
 31 is  
32          // plaintext decrypted output 
33 is          System.out.println (mingwen);
 34 is      }
 35 }

 

Guess you like

Origin www.cnblogs.com/GjqDream/p/11540961.html