Java implementation of the code of the example of the algorithm

Time:2020-5-21

1、 Algorithm description

The porefei code is a kind of symmetric cipher, which is the first encryption method replaced by two letters.

The following describes the algorithm steps:

1. Extract plaintext information and ciphertext from QR code M05, M05 format: < XXX xxx|yyy… YYY >, where plaintext XXX XXX, the key part information is YYY Extract all alphabetic information in YYY.

2. Key the extracted English letters. Remove duplicate letters. Add the key letters one by one into the 5 × 5 matrix, and add the remaining space in the order of A-Z. (remove q)

3. Divide the message to be encrypted into two groups. If the letters in the group are the same, add x to the first letter of the group and regroup. If there is one word left, add x as well.

4. In each group, find out where the two letters are in the matrix.
If two letters are not the same as each other or different columns, find another two letters in the matrix, and make these four letters into four corners of a rectangle.   
If two letters are in the same line, take the letter to the right of the two letters (if the letter is in the far right, take the far left letter).   
If two letters are in the same column, take the letter below the two letters (if the letter is at the bottom, take the letter at the top).

5. The newly found two letters are the result of the original two letter encryption.

6. Take the first three characters and the last three characters (uppercase letters) of the ciphertext as the corresponding 6-digit infrared alarm opening code.

2、 Example of algorithm process

Example: the content of QR code is: < hide the gold| play 5fair9example >.

1. The clear text message hide the gold and the key play fair xample

2. Form a matrix of 5 * 5 according to the key.


 P L A Y F
 I R E X M
 B C D G H
 J K N O S
 T U V W Z

3. Clear text processing: “Hi de th eg ol DX”

4. You will get the ciphertext: “BM nd ZB XD kyge”,

5. Take the alarm code of 6 digits corresponding to the first 6 characters (capital letters) of the ciphertext: 0x42, 0x4d, 0x4e, 0x44, 0x5a, 0x42

3、 The specific code is as follows:

import sun.applet.Main;

public class blf {
  public static void main(String[] args) {
    String s = "<hidethegold|play5fair9example>";
    get_blf(s);
  }

  public static void get_blf(String ssss){
    String eng = "ABCDEFGHIJKLMNOPRSTUVWXYZ";
    String beg = ssss.replaceAll("[<>0-9]", "");
    String []ss = beg.split("\|");
    String mw = ss[0].toUpperCase();
    String str = ss[1].toUpperCase();
    str = removeMethod(str);
    System.out.println(str);
    int bs = str.length() / 5;
    int ys = str.length() % 5;
    System.out.println(ys);
    System.out.println(bs);

    char[][] arr = new char[5][5];
    for (int i = 0; i < bs; i++) {
      arr[i] = str.subSequence(i * 5, (i+1) * 5).toString().toCharArray();
    }
    String yss = str.subSequence(bs*5, (bs*5+ys)).toString();
    String other = eng.replaceAll("["+ str +"]", "");
    System.out.println("other=" + other);
    arr[bs] = (yss + other.subSequence(0,(5-ys) )).toString().toCharArray();

    Int BS1 = BS + 1; // complete the remainder
    Int oth = 25 - (BS1 * 5); // remaining length
    other = other.subSequence((5 - ys), (oth + 5 - ys)).toString();

    System.out.println("other=" + other);


    int c = 5 - bs1;
    System.out.println("c=" + c);
    for (int i = 0; i < c; i++) {
      System.out.println("bs1=" + bs1);
      arr[bs1++] = other.subSequence(i * 5, (i+1) * 5).toString().toCharArray();
    }

    for (int i = 0; i < arr.length; i++) {
      for (int j = 0; j < arr[i].length; j++) {
        System.out.print(arr[i][j] + "\t");
      }
      System.out.println();
    }
// arr[0] = one.toCharArray();
// arr[1] = two.toCharArray();
// arr[2] = three.toCharArray();
// arr[3] = four.toCharArray();
// arr[4] = five.toCharArray();

    String s= "";
    for (int i = 0; i < mw.length()-1; i = i + 2) {
      if(mw.charAt(i) != mw.charAt(i+1)){
        s += "" + mw.charAt(i) + mw.charAt(i + 1) + " ";
      }
      if(i == (mw.length() - 3)){
        s += mw.charAt(i+2) + "X";
      }
    }
    System.out.println("s="+s);
    String []s1 = s.split(" ");
    String s2 = "";

    for (int i = 0; i < s1.length; i++) {
      s2 += resolve(arr,s1[i]);
    }
    System.out.println(s2);
    String fin ="";
    for (int i = 0; i < 6; i++) {
      fin += s2.charAt(i);
    }

    byte[] br = fin.getBytes();
    for (int i = 0; i < br.length; i++) {
      System.out.print(decimalToHex(br[i]) + "\t");
    }
  }

  public static String resolve(char[][] arr,String s1){
    int a = 99;
    int b = 99;
    int a1 = 99;
    int b1 = 99;
    String res = "";
    for (int i = 0; i < arr.length; i++) {
      for (int j = 0; j < arr[i].length; j++) {
        if((arr[i][j] == s1.charAt(0))){
          a = i;
          b = j;
        }else if(arr[i][j] == s1.charAt(1)){
          a1 = i;
          b1 = j;
        }
        if((a != 99) && (b !=99) && (a1 !=99) && (b1 != 99)){
          if(((a1 - a) !=0) && (((b1 - b) !=0))){
            res = "" + arr[a][b1] + arr[a1][b];
          }else if((a1 - a == 0) && (b1 - b != 0)){
            if((b == 4)){
              res = "" + arr[a][0] + arr[a1][b1+1];
            }else if(b1 == 4){
              res = "" + arr[a][b+1] + arr[a1][0];
            }else{
              res = "" + arr[a][b+1] + arr[a1][b1+1];
            }
          }else if((a1 - a !=0 ) && (b1 - b == 0)){
            if((a == 4)){
              res = "" + arr[0][b] + arr[a1+1][b1];
            }else if(a1 == 4){
              res = "" + arr[a+1][b] + arr[0][b1];
            }else{
              res = "" + arr[a+1][b] + arr[a1+1][b1];
            }
          }
        }
      }
    }
    return res;
  }

  public static String removeMethod(String s) {
    StringBuffer sb = new StringBuffer();
    int len = s.length();
    for (int i = 0; i < len; i++) {
      char c = s.charAt(i);
      If (s.indexof (c) = = s.lastindexof (c)) {// the first position of this character is consistent with the last position, i.e. there must be no repeated direct addition
        sb.append(c);
      }Else {// the same secondary character appears more than once
        Int fristposition = s.indexof (c); // the first occurrence of the second character
        If (fristposition = = I) {// the first occurrence position is the same as the current position, that is, the first occurrence of the add
          sb.append(c);
        }
      }
    }
    return sb.toString();
  }

  public static String decimalToHex(byte decimal) {
    String hex = "";
    while(decimal != 0) {
      int hexValue = decimal % 16;
      hex = toHexChar(hexValue) + hex;
      decimal = (byte)(decimal / 16);
    }
    return hex;
  }

  //Convert decimal numbers from 0 to 15 to hexadecimal numbers from 0 to f
  public static char toHexChar(int hexValue) {
    if(hexValue <= 9 && hexValue >= 0)
      return (char)(hexValue + '0');
    else
      return (char)(hexValue - 10 + 'A');
  }
}

4、 Operation result:

The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.