【问题标题】:Matching characters from two 1D arrays into a 2D array将两个一维数组中的字符匹配到一个二维数组中
【发布时间】:2022-01-03 11:03:07
【问题描述】:

基本上,我创建了一个 vigenere 密码。 Vigenere Cipher matrix

这是一种用于编码消息的方法。我已经为 Vigenere 密码数组创建了二维数组。我收到要编码的 .txt 文件形式的消息。然后我将其转换为常规的“一维字符数组”。本质上是读取文本并将每个字符放入一个新数组中。

然后我还从用户那里获取密钥的输入,然后获取并重复该密钥以匹配字符数组的长度。所以现在我有一个“键数组”。

Vingere 密码的工作原理是密钥的第一个字母和文本的第一个字母匹配。所以在 X 轴上是“消息”,在 Y 轴上是“键”。

例如,如果我做关键:道路

和消息:汽车

加密的消息是:torv,

我会得到 T,因为我从 Y 轴上的 R 开始并将其与 X 轴上的 C 匹配。

这就是我设置Vigenere Cipher. 的方式,其中“字母”是this. 我只是无法将两个数组(加密密钥)和(消息)的字符“匹配”到我的 Vigenere 密码,然后保存该输入作为稍后使用的方法中的数组。目前,我不太担心大写字母等。

维吉尼密码:

public static char[][] arrayMatrix (){
    char[][] arrayTabula = new char[26][26];
    for (int i=0;i<26;i++){
        int x = i;
        for (int j=0; j<26; j++){
            arrayTabula[i][j] = alphabet[x];
            if (x == 25){
                x = 0;
            }
            else
            x++;
        }
    }
    return arrayTabula;
}

我考虑过将字母表中的字母转换为数字,然后使用数字将所说的“数字”与我的制表符匹配。我只是认为这太乏味了,无法重新转换回“正常”文本。我只是想知道是否有一种直接的方法可以将我想要的 X 轴与我的“消息”和 Y 轴与我的“键”匹配,然后找到横截面点。

有人能帮帮我吗?

【问题讨论】:

    标签: java arrays matrix


    【解决方案1】:

    矩阵表示可以这样认为: matrix[y][x]

    所以在 Vigenere Cipher 中,作为y,您将使用key 的索引,作为x,您将使用message 的索引每个字符。

    tabula[(index of 'r')][(index of 'c')] // would equal to = t as you mentioned for cars and road
    

    我看到message lengthkey length 这就是为什么应该相等,这就是我们要使用和迭代的。

    我的方法是这样的:

    public static void encrypt(String message, String key) {
            int stringLength = message.length(); // they've common length
            char[][] arrayTabula = arrayMatrix(); // table that you've created
            String encryptedText = "";
    
            for (int i = 0; i < stringLength; i++) {
    
                // 97 is ascii code of 'a'. 
                // We want to access first index when we have 'a';
                // means = (int) 'a' - 97 = 0;
                // Applying for both y part and x part.
                int keysIndex = key.charAt(i) - 97; 
                int messagesIndex = message.charAt(i) - 97;
    
                //then correspondent `y` and `x` value is my encrypted char add it to my str
                encryptedText += arrayTabula[keysIndex][messagesIndex];
            }
            System.out.println(encryptedText);
        }
    

    或者,如果您不想使用 97 或任何其他 ascii 代码。我建议你使用alphabet char[] 来查找每个字符的索引。

    可能是这样的:

     public static int findIndex(char ch) {
            for (int i = 0; i < alphabet.length; i++) {
                if (alphabet[i] == ch) {
                    return i;
                }
            }
            return -1; // for not existing chars
        }
    

    那么更新后的代码将是:

    ...
    int keysIndex = findIndex(key.charAt(i));
    int messagesIndex = findIndex(message.charAt(i));
    ...
    

    【讨论】:

      【解决方案2】:

      根据您的 arrayTabula 代码输出将是“a b c d e f g h i j k l m n o p q r s t u v w x y z” 26 次。我认为您不再需要创建 x 变量,使用 i 我不太明白你的例子,你能举个例子吗? ...

      public static char[][] arrayMatrix() {
              char[][] arrayTabula = new char[26][26];
              char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();
              for (int i = 0; i < 26; i++) {
                  for (int j = 0; j < 26; j++) {
                      arrayTabula[i][j] = alphabet[j];
                      System.out.print(arrayTabula[i][j]+" ");
                  }
                  System.out.println();
              }`enter code here`
              return arrayTabula;
          }
      

      ...

      【讨论】:

        【解决方案3】:

        仅当您想在没有数组的情况下使用小写:

        public static String transform(String message, String key){
        String mLower = message.toLowerCase();
        String kLower = key.toLowerCase();
        String result = "";
        for(int i = 0; i < message.length(); i++){
          int mNum = mLower.charAt(i) - 'a';
          int kNum = kLower.charAt(i) - 'a';
          int transformed = (kNum + mNum) % 26;
          transformed += 'a';
          char t = (char)transformed;
          result += t;
        }
        return result;
        

        }

        因为每一行都以自己的字符开头,并通过将键字母的索引添加到消息字母的索引来循环字母表,将它们添加它们得到 26 的模数,你应该得到你想要的结果。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-11-13
          • 1970-01-01
          • 2021-10-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多