简体   繁体   English

将两个一维 arrays 中的字符匹配到一个二维数组中

[英]Matching characters from two 1D arrays into a 2D array

Essentially, I've created a vigenere cipher.从本质上讲,我创建了一个 vigenere 密码。 Vigenere Cipher matrix Vigenere 密码矩阵

It's a method used for coding messages.这是一种用于编码消息的方法。 I've created the 2D array for the Vigenere cipher array.我已经为 Vigenere 密码数组创建了二维数组。 I receive a message in a form of a.txt file to be encoded.我收到要编码的 a.txt 文件形式的消息。 I then convert this into a regular '1D character array'.然后我将其转换为常规的“一维字符数组”。 Essentially reading the text and putting every single character into a new array.本质上是阅读文本并将每个字符放入一个新数组中。

I then also get input from the user for the key, this key is then taken and repeated to match the length of the character array.然后我还从用户那里获取密钥的输入,然后获取并重复该密钥以匹配字符数组的长度。 So now I have a 'key array'.所以现在我有一个“键数组”。

How a Vingere Cipher works is that the key's first letter and the texts first letter is matched. Vingere Cipher 的工作原理是密钥的第一个字母和文本的第一个字母匹配。 So on the X-Axis is the 'message' and on the Y-Axis is the 'key'.所以在 X 轴上是“消息”,在 Y 轴上是“键”。

For example, if I do key: road例如,如果我做 key: road

and message: cars和信息:汽车

the encrypted message would be: torv,加密的消息是:torv,

I would get T, because I started with R on the Y-Axis and matched it to C on the X-axis.我会得到 T,因为我从 Y 轴上的 R 开始并将其与 X 轴上的 C 匹配。

This is how I setup my Vigenere Cipher.这就是我设置Vigenere Cipher 的方式。 where 'alphabet' is this. “字母表”在哪里 I'm just having trouble 'matching' the characters of the two arrays (encryption key) and (message) to my Vigenere Cipher, then saving that input as an array inside a method to be used later on.我只是无法将两个 arrays(加密密钥)和(消息)的字符“匹配”到我的 Vigenere Cipher,然后将该输入作为数组保存在稍后使用的方法中。 Currently, I'm not too worried about capital letters and so on.目前,我不太担心大写字母等。

Vigenere cipher code:维吉尼密码:

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

I've thought about converting the letters in the alphabet to a number, then using numbers to match said 'numbers' with my Tabula.我考虑过将字母表中的字母转换为数字,然后使用数字将所说的“数字”与我的 Tabula 匹配。 I just thought this would be too tedious to do and re-convert back into 'normal' text.我只是认为这太乏味了,无法重新转换回“正常”文本。 I am just wondering if there's a direct method of just matching my wanted X-axis with my 'message' and Y-axis with my 'key' and then finding the cross-sectional point.我只是想知道是否有一种直接的方法可以将我想要的 X 轴与我的“消息”和 Y 轴与我的“键”匹配,然后找到横截面点。

Is anyone able to help me out?有人能帮我吗?

As per your code output of arrayTabula would be "ab c defghijklmnopq r stuvwxyz " for 26 times.I think you no need to create x variable anymore, use i I am not quite understand your example, could you please one example? As per your code output of arrayTabula would be "ab c defghijklmnopq r stuvwxyz " for 26 times.I think you no need to create x variable anymore, use i I am not quite understand your example, could you please one example? ... ...

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

... ...

Matrix representation can be thought as this: matrix[y][x]矩阵表示可以这样认为: matrix[y][x]

So in Vigenere Cipher, as a y you're going to use key 's index and as a x you're going to use message 's index for each character .因此,在 Vigenere Cipher 中,作为y ,您将使用key的索引,作为x ,您将使用message每个 character的索引。

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

I see that message length and key length that's why should be equal, this is what we are going to use and iterate through.我看到message lengthkey length应该相等,这就是我们要使用和迭代的。

Shortly my approach was something like this:不久我的方法是这样的:

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

Or if you don't want to use, 97 or any other ascii code.或者,如果您不想使用 97 或任何其他 ascii 代码。 I would suggest you to use your alphabet char[] to find the index of each character.我建议您使用您的alphabet char[]来查找每个字符的索引。

Might be like that:可能是这样的:

 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
    }

then the updated code would be:那么更新的代码将是:

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

for lower case only if you want to do it without the array:仅当您想在没有数组的情况下使用小写字母时:

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;

} }

since each row starts with its own character and loops the alphabet by adding the index of the letter of the key to that of the letter of the message add them them get the modular of 26 you should get the result that you want.因为每一行都以自己的字符开头,并通过将键的字母的索引添加到消息的字母的索引来循环字母表,将它们添加它们得到 26 的模数,你应该得到你想要的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM