簡體   English   中英

將兩個一維 arrays 中的字符匹配到一個二維數組中

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

從本質上講,我創建了一個 vigenere 密碼。 Vigenere 密碼矩陣

這是一種用於編碼消息的方法。 我已經為 Vigenere 密碼數組創建了二維數組。 我收到要編碼的 a.txt 文件形式的消息。 然后我將其轉換為常規的“一維字符數組”。 本質上是閱讀文本並將每個字符放入一個新數組中。

然后我還從用戶那里獲取密鑰的輸入,然后獲取並重復該密鑰以匹配字符數組的長度。 所以現在我有一個“鍵數組”。

Vingere Cipher 的工作原理是密鑰的第一個字母和文本的第一個字母匹配。 所以在 X 軸上是“消息”,在 Y 軸上是“鍵”。

例如,如果我做 key: road

和信息:汽車

加密的消息是:torv,

我會得到 T,因為我從 Y 軸上的 R 開始並將其與 X 軸上的 C 匹配。

這就是我設置Vigenere Cipher 的方式。 “字母表”在哪里 我只是無法將兩個 arrays(加密密鑰)和(消息)的字符“匹配”到我的 Vigenere Cipher,然后將該輸入作為數組保存在稍后使用的方法中。 目前,我不太擔心大寫字母等。

維吉尼密碼:

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

我考慮過將字母表中的字母轉換為數字,然后使用數字將所說的“數字”與我的 Tabula 匹配。 我只是認為這太乏味了,無法重新轉換回“正常”文本。 我只是想知道是否有一種直接的方法可以將我想要的 X 軸與我的“消息”和 Y 軸與我的“鍵”匹配,然后找到橫截面點。

有人能幫我嗎?

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[y][x]

因此,在 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

我看到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));
...

僅當您想在沒有數組的情況下使用小寫字母時:

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 的模數,你應該得到你想要的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM