当前位置: 移动技术网 > IT编程>开发语言>Java > java实现波雷费密码算法示例代码

java实现波雷费密码算法示例代码

2020年03月09日  | 移动技术网IT编程  | 我要评论

一、算法描述

波雷费密码是一种对称式密码,是首种双字母取代的加密法。

下面描述算法步骤:

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

四、运行结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网