用于循环的Java Transposition密码

时间:2017-03-25 19:20:02

标签: java python encryption

我一直在尝试使用转置密码重新创建一个python函数来解密消息。虽然它不断给我不正确的输出并使消息更长。我认为错误与数组和for循环有关,但我不是100%肯定。

我的代码

 def decryptMessage(key, message):
     # The transposition decrypt function will simulate the "columns" and
     # "rows" of the grid that the plaintext is written on by using a list
     # of strings. First, we need to calculate a few values.

     # The number of "columns" in our transposition grid:
     numOfColumns = math.ceil(len(message) / key)
     # The number of "rows" in our grid will need:
     numOfRows = key
     # The number of "shaded boxes" in the last "column" of the grid:
     numOfShadedBoxes = (numOfColumns * numOfRows) - len(message)

     # Each string in plaintext represents a column in the grid.
     plaintext = [''] * numOfColumns

     # The col and row variables point to where in the grid the next
     # character in the encrypted message will go.
     col = 0
     row = 0

     for symbol in message:
         plaintext[col] += symbol
         col += 1 # point to next column

         # If there are no more columns OR we're at a shaded box, go back to
         # the first column and the next row.
         if (col == numOfColumns) or (col == numOfColumns - 1 and row >= numOfRows - numOfShadedBoxes):
             col = 0
             row += 1

     return ''.join(plaintext)

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

1. Open Android Studio
2. Open Right Side Gradle (From Right Side Panel)
3. Select Project with root. Under TASKS-->android-->signing report
4. Now at the bottom right corner click on the Gradle-Console to get SHA1 and MD5

1 个答案:

答案 0 :(得分:0)

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

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;

    }

}