1. 程式人生 > 程式設計 >java實現波雷費密碼演算法示例程式碼

java實現波雷費密碼演算法示例程式碼

一、演算法描述

波雷費密碼是一種對稱式密碼,是首種雙字母取代的加密法。

下面描述演算法步驟:

1、從1號二維碼M05,提取明文資訊和密文,M05格式:<xxx…xxx|yyy…yyy>,其中明文xxx…xxx,金鑰部分資訊為yyy…yyy中的提取所有英文字母資訊。

2、將提取的英文字母作密匙。除去重複出現的字母。將密匙的字母逐個逐個加入5×5的矩陣內,剩下的空間將未加入的英文字母依A-Z的順序加入。(將Q去除)

3、將要加密的訊息分成兩個一組。若組內的字母相同,將X加到該組的第一個字母后,重新分組。若剩下一個字,也加入X字。

4、在每組中,找出兩個字母在矩陣中的地方。
若兩個字母不同行也不同列,在矩陣中找出另外兩個字母,使這四個字母成為一個長方形的四個角。   

若兩個字母同行,取這兩個字母右方的字母(若字母在最右方則取最左方的字母)。   
若兩個字母同列,取這兩個字母下方的字母(若字母在最下方則取最上方的字母)。

5、新找到的兩個字母就是原本的兩個字母加密的結果。

6、取密文前3個字元與後三個字元(大寫字母)作為對應6位的紅外報警開啟碼。

二、演算法過程示例

例:二維碼內容為:<hidethegold|play5fair9example>。

1.明文資訊hidethegold和密匙playfairexample  

2.根據金鑰形成5*5的矩陣。

 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.明文處理為:“HI DE TH EG OL DX”

4.就會得到密文:“BM ND ZB XD KY GE”,

5.取密文前6個字元(大寫字母)對應6位的報警碼:0X42,0X4D,0X4E,0X44, 0X5A, 0X42

三、具體程式碼如下:

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');
  }
}

四、執行結果:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。