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