如何在Java中以Polybius密码获得单词的坐标?

时间:2019-07-25 08:36:43

标签: java arrays encryption multidimensional-array 2d

我正在尝试构建需要加密和解密的Polybius密码。

因此,例如,我最初如何获得这个正方形中“世界”一词的坐标?

public static char[][] cypher = {
    {'p', 'h', '0', 'q', 'g', '6'}, 
    {'4', 'm', 'e', 'a', '1', 'y'}, 
    {'l', '2', 'n', 'o', 'f', 'd'},
    {'x', 'k', 'r', '3', 'c', 'v'}, 
    {'s', '5', 'c', 'w', '7', 'b'}, 
    {'j', '9', 'u', 't', 'i', '8'},};

我知道“世界”应该是43 23 32 20 25

1 个答案:

答案 0 :(得分:0)

实际上 Polybius棋盘格 2D阵列。要找到Polybius棋盘格元素索引,您可以按照2D数组搜索伪代码

for i=0 : array.length
    for j=0 : array[i].length
        %check the desiare value with array[i][j] element%
    end;
end;

在您的代码上实现此伪代码:

public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        char[][] cypher = {
            {'p', 'h', '0', 'q', 'g', '6'},
            {'4', 'm', 'e', 'a', '1', 'y'},
            {'l', '2', 'n', 'o', 'f', 'd'},
            {'x', 'k', 'r', '3', 'c', 'v'},
            {'s', '5', 'c', 'w', '7', 'b'},
            {'j', '9', 'u', 't', 'i', '8'}};

        char[] data = {'w', 'o', 'r', 'l', 'd'};//data convert to char array

        for (char c : data) {
            for (int i = 0; i < cypher.length; i++) {
                char[] cs = cypher[i];
                for (int j = 0; j < cs.length; j++) {
                    char d = cs[j];
                    if (d == c) {
                        sb.append(i);
                        sb.append(j);
                        sb.append(" "); 
                    }

                }

            }
        }
        System.out.println(sb.toString());
    }

此代码的输出是:43 23 32 20 25