2017-03-25 9 views
0

私は転置暗号を使ってメッセージを解読するためにpython関数を再作成しようとしています。それは私に間違った出力を与え続け、メッセージを長くする。私はエラーが配列とforループとすることだと思うが、私は100%確実ではない。Javaトランスポーズ暗号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

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) 
+0

複数のエラーがありますが、そのうちの1つを修正しました。これを修正するために、NormalArrayの各要素の値は空白にする必要があります。forループを追加する前に値を設定します。for(int i = 0; i

答えて

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; 

    } 

}