Java循环换位密码

2024-05-02 17:31:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试重新创建一个python函数来使用换位密码来解密消息。虽然它总是给我错误的输出,使信息更长。我认为错误与数组和for循环有关,但我不能百分之百确定。在

我的代码

public String TranspositionDecryptText (String EncryptedText, int Key) {

    double NumColumnsD = Math.ceil(EncryptedText.length() / Key);
    int NumColumns = (int) NumColumnsD;
    int NumRows = Key;
    int NumShadedBoxes = (NumColumns * NumRows) - EncryptedText.length();

    String NormalArray[] = new String[NumColumns];

    int col = 0;
    int row = 0;

    for (int i = 0; i < EncryptedText.length(); i++) {

        NormalArray[col] = NormalArray[col] + EncryptedText.charAt(i);
        col = col + 1;

        if ((col == NumColumns) || ((col == NumColumns - 1) && (row >= NumRows - NumShadedBoxes))) {

            col = 0;
            row = row + 1;

        }

    }

    String NormalString = "";

    for (int i = 0; i < NormalArray.length; i++) {

        NormalString = NormalString + NormalArray[i];

    }

    return NormalString;

}

Python版本http://inventwithpython.com/hacking/chapter9.html

^{pr2}$

Tags: keyforstring错误collengthintrow
1条回答
网友
1楼 · 发布于 2024-05-02 17:31:46

我发现给定的文本字符串需要被密钥整除才能进行解密。在找到这个之后,我添加了一个循环,在加密之前在文本的末尾添加空格,这样解密程序就必须在之前修剪返回的文本。代码现在如下所示:

public class TranspositionCipher {

public String TranspositionEncryptText (String Text, int Key) {

    while (Text.length() % Key != 0) {

        Text = Text + " ";

    }

    ArrayList EncryptedText = new ArrayList<String>(Key); // Creates an array with as many elements as key

    String HoldText = "";

    for (int col = 0; col < Key; col++) {

        int pointer = col;
        while (pointer < Text.length()) {

            HoldText = HoldText + Text.charAt(pointer);
            pointer = pointer + Key;

        }

        EncryptedText.add(col, HoldText);
        HoldText = "";

    }

    String EncryptedString = ""; // String version of encrypted text
    Iterator iter = EncryptedText.iterator(); // Used to go through the array

    while (iter.hasNext()) { // While there are more elements in the array

        EncryptedString =  EncryptedString + iter.next(); // Add the element to the string

    }

    return EncryptedString; // Return the encrypted text as a string

}

public String TranspositionDecryptText (String EncryptedText, int Key) {

    double NumColumnsD = Math.ceil(EncryptedText.length() / Key);
    int NumColumns = (int) NumColumnsD;
    int NumRows = Key;
    int NumShadedBoxes = (NumColumns * NumRows) - EncryptedText.length();

    String NormalArray[] = new String[NumColumns];

    int col = 0;
    int row = 0;

    for (int i = 0; i < NumColumns; i++) {

        NormalArray[i] = "";

    }

    for (int i = 0; i < EncryptedText.length(); i++) {

        NormalArray[col] = NormalArray[col] + EncryptedText.charAt(i);
        System.out.println(NormalArray[col]);
        col = col + 1;

        if ((col == NumColumns) || ((col == NumColumns - 1) && (row >= NumRows - NumShadedBoxes))) {

            col = 0;
            row = row + 1;

        }

    }

    String NormalString = "";

    for (int i = 0; i < NormalArray.length; i++) {

        NormalString = NormalString + NormalArray[i];

    }

    NormalString = NormalString.trim();

    return NormalString;

    }

}

相关问题 更多 >