我一直在尝试重新创建一个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;
}
^{pr2}$
我发现给定的文本字符串需要被密钥整除才能进行解密。在找到这个之后,我添加了一个循环,在加密之前在文本的末尾添加空格,这样解密程序就必须在之前修剪返回的文本。代码现在如下所示:
相关问题 更多 >
编程相关推荐