java Realizes Encryption and Decryption of

  • 2021-07-10 19:45:32
  • OfStack

In this paper, we share the example of Java to achieve replacement password encryption and decryption for your reference, the specific contents are as follows

Train of thought

The replacement password is just a simple transposition. What is written here is a replacement password with a group length of 7. Because of the limited knowledge, it is troublesome to write. Here, I will briefly introduce the following ideas:

1. Because the replacement password should be grouped first, the grouping length here is 7, and the insufficient digits are supplemented by 0. You can select a 2-dimensional array for operation, define a 2-dimensional array, and there are as many 1-dimensional arrays as there are 2-dimensional arrays in plaintext, and the length of 1-dimensional is the grouping length
2. Defines a 1-D array key as the encryption key and a 1-D array trankey as the decryption key, where the elements are written by oneself
3. Enter plaintext and store it in the expanded new 1-D array, but the padding bit is null character '\ 0', and then replace it with '0' and store it in the 2-D array
4. Encryption: Encrypt the plaintext in the order in the secret key key and store it in the 2-dimensional array miwen
5. Decryption: Decrypt the ciphertext in the order in the decryption secret key trankey and store it in the 2-D array arr
6. Eliminate the filled zeros, calculate how many zeros are filled, and then subtract the number of zeros from the packet length to the number of unfilled bits in the last 1-D array in the 2-D array, store them in the 1-D array, and replace the last 1-D array with this 1-D array, thus eliminating all filled zeros

Code


import java.util.Scanner;

public class replacement {


 public static void main(String[] args) {
  Scanner sc = new Scanner(System.in);
  System.out.println(" Please enter encrypted plaintext: ");
  String plaintexts = sc.nextLine();
  int len = plaintexts.length() % 7 == 0 ? plaintexts.length() / 7 : (plaintexts.length() / 7) + 1;// Definition 2 Dimension array is to be used, and its main function is to determine 2 In the dimension array 1 Number of dimension arrays 
  char[] plaintext_char = new char[7 * len];// Save the plaintext first 1 Dimension array, and then store it in the 2 In the dimension array 
  char[][] arr = new char[len][7];// Storing plaintext and decrypted plaintext 2 Dimensional array 
  char[][] miwen = new char[len][7];// Storing ciphertext 2 Dimensional array 
  char[] n=new char[7];// Used to eliminate redundant padding during decryption 0
  int[] key = {6, 3, 0, 4, 1, 5, 2};// Encryption key 
  int[] transkey = {2, 4, 6, 1, 3, 5, 0};// Decryption key 

  fill(plaintexts, plaintext_char, len, arr);
  encryption(len, miwen, arr, key);
  System.out.println(" The encrypted plaintext is: ");
  for (int i = 0; i < len; i++) {
   for (int j = 0; j < miwen[i].length; j++) {
    System.out.print(miwen[i][j]);
   }
  }// Output ciphertext 
  System.out.println(" ");
  System.out.println(" Press 1 Decrypt or press any other key to exit: ");
  String a=sc.nextLine();
  if (a.equals("1")){
   decryption(n,len, miwen, arr, transkey,plaintexts);
   for (int i = 0; i < len; i++) {
    for (int j = 0; j < arr[i].length; j++) {
     System.out.print(arr[i][j]);
    }
   }
  }else {
   System.out.println(" Quit! ");
  }// Output plaintext or exit 
 }

 public static void fill(String plaintexts, char[] plaintext_char, int len, char[][] arr) {
  for (int i = 0; i < plaintexts.length(); i++) {
   plaintext_char[i] = plaintexts.charAt(i);
  }// Save the input string into the expanded 1 Dimension array, the length of the array has reached the standard at this time, but the extra fill is yes '\0'
  for (int i = 0; i < plaintext_char.length; i++) {
   if (plaintext_char[i] == '\u0000') {
    plaintext_char[i] = '0';
   }// Null characters in a string array '\0' Replace with 0
 for (int i = 0; i < len; i++) {
   for (int j = i * 7, k = 0; j < 7 * (i + 1) && k < 7; j++, k++) {
    arr[i][k] = plaintext_char[j];
   }
  }// Save the expanded plaintext into 2 In an array of dimensions, each 7 A 1 A 1 Dimensional array 
 }

 public static void encryption(int len, char[][] miwen, char[][] arr, int[] key) {
  for (int i = 0; i < len; i++) {
   for (int j = 0; j < 7; j++) {
    miwen[i][j] = arr[i][key[j]];
   }
  }
 }// Encryption method 
 public static void decryption(char[] n,int len, char[][] miwen, char[][] arr, int[] trankey,String plaintexts) {
  for (int i = 0; i < len; i++) {
   for (int j = 0; j < 7; j++) {
    arr[i][j] = miwen[i][trankey[j]];
   }
  }
  for (int i = 0; i <7-(7*len-plaintexts.length()); i++) {
   n[i]=arr[arr.length-1][i];
  }//7-(7*len-plaintexts.length()) Yes 2 The last in the dimension array 1 A 1 The number of unfilled bits in the dimension array, adding the unfilled bits to the n Medium 
  arr[len-1]=n;// Use n Replace 2 The last in the dimension array 1 An array of arrays that are equivalent to the 0 Erase 
 }// Decryption method 
}

Running result

Please enter encrypted plaintext:
jdlalsdkpa1548796
The encrypted plaintext is:
dajldsl81k5p4a0070906
Press 1 to decrypt or press any other key to exit:
1
jdlalsdkpa1548796


Related articles: