Java implementation of the boleifei cryptographic algorithm example code

1、 Algorithm description

Polefeigh cipher is a symmetric cipher, which is the first encryption method with double letter substitution.

The algorithm steps are described below:

1. Extract plaintext information and ciphertext from No. 1 QR code M05. M05 format: < XXX... Xxx|yyy... YYY >, where plaintext XXX... XXX and key part information are all English letter information extracted from YYY... YYY.

2. Use the extracted English letters as the key. Remove duplicate letters. Add the letters of the key one by one into 5 × In the matrix of 5, the remaining space will add the English letters not added in the order of A-Z. (remove q)

3. Divide the messages 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 the X word.

4. In each group, find out where the two letters are in the matrix. If two letters are in different rows and columns, find another two letters in the matrix to make the four letters become the four corners of a rectangle. If two letters are in the same row, take the letter to the right of the two letters (if the letter is at the right, take the letter to the left). If the 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 (capital 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|play5fair9example >.

1. Plaintext information hidethe gold and key playfairxample

2. Form a 5 * 5 matrix 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 6-digit alarm code corresponding to the first 6 characters (capital letters) of the ciphertext: 0x42, 0x4d, 0x4e, 0x44, 0x5a, 0x42

3、 The specific codes are 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;  //把余数补全
    int oth = 25 - (bs1 * 5);//剩下的长度
    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)) {//此字符第一次位置和最后位置一致 即肯定没有重复的直接添加
        sb.append(c);
      } else {//同理 次字符出现过多次
        int fristposition=s.indexOf(c);//次字符第一次出现的位置
        if(fristposition==i){//第一次出现的位置和当前位置一致 即第一次出现添加
          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;
  }

  //将0~15的十进制数转换成0~F的十六进制数
  public static char toHexChar(int hexValue) {
    if(hexValue <= 9 && hexValue >= 0)
      return (char)(hexValue + '0');
    else
      return (char)(hexValue - 10 + 'A');
  }
}

4、 Operation results:

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>