Java Cipher 柱状转置

Java CIpher Columnar Transposition

我对解密加密的 txt 感到很不爽。

public class Practica2 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a text: ");
        String txt = input.nextLine();
        System.out.println("Enter a num of columns: ");
        int num = input.nextInt();
        String cipher = cipherTrans(txt, num);
        System.out.println("Coded: " + cipher);
        String decipher = decipherTrans(cipher, num);
        System.out.println("Decoded: " + decipher);
    }

    //This method encrypts
    private static String cipherTrans(String txt, int num) {
        String output = "";
        //takes the length of the string and divides by the num of columns
        int num2 = (int) Math.ceil(txt.length()*1.0/num);
        //2d array 
        char[][] buffer = new char[num2][num];
        int k = 0;
        //loop which will store and rearrange the letter of the txt 
        for (int i = 0; i < num2; i++) {
            for (int j = 0; j < num; j++, k++) {
                if (txt.length() > k) {
                    buffer[i][j] = txt.charAt(k);
                }
            }
        }
    
        //loop which will store the rearrenged txt and output the encrypted txt
        for (int j = 0; j < num; j++) {
            for (int i = 0; i < num2; i++) {
                output += buffer[i][j];
            }
        }
        return output;
    }

    //this method will decrypt the encrypted txt
    private static String decipherTrans(String txt, int num) {
        String output = "";
        int num2 = (int) Math.ceil(txt.length() * 1.0 / num);
        char[][] buffer = new char[num2][num];
        int k = 0;
        for (int i = 0; i < num2; i++) {
            for (int j = 0; j < num; j++, k++) {
                if (txt.length() > k) {
                    buffer[i][j] = txt.charAt(k);
                }
            }
        }
        for (int i = 0; i < num; i++){
            for (int j = 0; j < num2; j++){
                txt += buffer[j][i];
            }
        }
        return output;
    }
}

当前输出:

Enter a text: 

my name is dani

Enter a num of columns: 

5

Coded: mm yed  aninasi

Decoded: mdim n  ayaseni

预期输出:

Enter a text: 

my name is dani

Enter a num of columns: 

5

Coded: mm yed  aninasi

Decoded: my name is dani

转置table

这是您的密文:mm yed aninasi;密钥长度为 5.

您的特定情况的换位 table 如下所示:

char[][] buffer = {
    {'m', 'y', ' ', 'n', 'a'},
    {'m', 'e', ' ', 'i', 's'},
    {' ', 'd', 'a', 'n', 'i'}
};

如何生成转置table?

"... the recipient has to work out the column lengths by dividing the message [ciphertext] length by the key length. Then they can write the message out in columns again, ..."

From: https://en.wikipedia.org/wiki/Transposition_cipher#Columnar_transposition

如何破译消息?

生成换位后 table,只需从左到右逐行阅读即可。

解决方案

根据您的代码,有效的解密方法可能如下所示:

private static String decipherTrans(String txt, int num) {
    int num2 = (int) Math.ceil(txt.length() * 1.0 / num);

    char[][] buffer = new char[num2][num];
    int k = 0;
    for (int i = 0; i < num; i++) {
        for (int j = 0; j < num2; j++, k++) {
            if (txt.length() > k) {
                buffer[j][i] = txt.charAt(k);
            }
        }
    }

    String output = "";
    for (int i = 0; i < num2; i++){
        for (int j = 0; j < num; j++){
            output += buffer[i][j];
        }
    }
    return output;
}

你只需要清楚地知道你在做什么才能正确使用数组索引。