zoukankan      html  css  js  c++  java
  • 维吉尼亚密码java代码实现根据密钥长度计算IC值过程

    package cn.longxuzi;

    import java.util.Scanner;

    import org.junit.Test;

    public class ICUtils {

          /**

           * @author龙须子(XuziLong)

           * @param null

           * @return String [][]devideCipherText

           * @Date 2013-11-20 PM 19:00

           */

          public String[][] devideCipherText() {

               // 根据密钥长度将密文分解成密钥长度个小组

               String[][] str;

               System.out.println("请输入密文字符串(以回车键结束)");

               Scanner sc = new Scanner(System.in);

               String ciphertext = sc.nextLine().toUpperCase().trim();

               System.out.println("请输入密钥长度(以回车键结束)");

               int keyLength = sc.nextInt();

               // 计算每一组最多有多少个字母

               int amount = 0;

               if (ciphertext.length() % keyLength == 0) {

                     amount = ciphertext.length() / keyLength;

               } else {

                     amount = (ciphertext.length() / keyLength) + 1;

               }

               // 初始化存储分开组的密文的字符串数组

               str = new String[keyLength][amount];

               int flag = 0, i = 0;

               for (int j = 0; j < keyLength; j++) {

                     for (int k = 0; k < amount; k++) {

                          if (i + 1 > ciphertext.length()) {

                                break;

                          }

                          str[j][k] = ciphertext.substring(i, i + 1);

                          i = i + keyLength;

                     }

                     i = flag + 1;

                     flag++;

               }

               return str;

          }

          /**

           * @author龙须子(XuziLong)

           * @param String

           *            [][]

           * @return double Every[]IC value

           * @Date 2013-11-20 PM 19:23

           */

          public double[] getIC(String[][] str) {

               // 计算每一小组密文的IC值

               double[] IC = new double[str.length];

               char[] letter = { 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77,

                          78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90 };// 创建字母表数组

               int[][] sum = new int[str.length][letter.length];// 创建存放每个密文小组中每个字母出现次数的数组

               int count = 0;// 计数器

               for (int i = 0; i < str.length; i++) {

                     for (int j = 0; j < letter.length; j++) {

                          for (int k = 0; k < str[0].length; k++) {

                                if (String.valueOf(letter[j]).toUpperCase()

                                           .equals(str[i][k])) {

                                      count++;

                                }

                          }

                          sum[i][j] = count;

                          count = 0;

                     }

               }

               double[] result = new double[IC.length];

               for (int j = 0; j < str.length; j++) {

                     for (int k = 0; k < letter.length; k++) {

                          if (sum[j][k] != 0) {

                                result[j] += sum[j][k] * (sum[j][k] - 1);

                          }

                     }

               }

               double N = (double) (str.length * str[0].length);

               for (int j = 0; j < IC.length; j++) {

                     IC[j] = (double) (result[j] / (N * (N - 1)));

               }

               return IC;

          }

          /**

           * @author龙须子(XuziLong)

           * @param double[]IC

           * @return double average []IC

           * @Date 2013-11-21 PM 19:23

           */

          public double getAverageIC(double[] IC) {

               // 计算所有密文小组IC的平均值

               double result = 0;

               for (int i = 0; i < IC.length; i++) {

                     result += IC[i];

               }

               return result / (double) (IC.length);

          }

        //以下是求每一个密文小组IC值及分组情况的测试代码

         

          @Test

          public void tests() {

               while (true) {

                     ICUtils tes = new ICUtils();

                     String[][] str = tes.devideCipherText();

                     for (int i = 0; i < str.length; i++) {

                          System.out.print("第" + (i + 1) + "小组的密文字母串为:");

                          for (int k = 0; k < str[0].length; k++) {

                                if (str[i][k] == null) {

                                      str[i][k] = "";

                                }

                                System.out.print(str[i][k]);

                          }

                          System.out.println();

                     }

                     double[] ic = tes.getIC(str);

                     for (int j = 0; j < ic.length; j++) {

                          System.out.println("该小组密文的IC值为:" + ic[j]);

                     }

                     System.out.println("本次分组中IC的平均值为:"

                                + tes.getAverageIC(tes.getIC(str)));

               }

          }

         

    }

  • 相关阅读:
    指针数组和数组指针表示二维数组
    scanf与getchar()区别
    选择循环
    朴素的思想
    asp.net中word转html碰到的权限异常问题(转)
    MAC下安装Fiddler抓包工具
    抓包工具charles的使用
    mysql 命令大全
    利用反射拼接SQL查询条件字符串
    赚自己的淘宝佣金,让返利网无路可走
  • 原文地址:https://www.cnblogs.com/ljhoracle/p/LiJiHua.html
Copyright © 2011-2022 走看看