用于循环的Java转置密码
我一直在尝试重新创建一个python函数,使用转置密码对消息进行解密。虽然它不断给我错误的输出,使信息更长。我认为错误与数组和for循环有关,但我不能100%确定 我的代码用于循环的Java转置密码,java,python,encryption,Java,Python,Encryption,我一直在尝试重新创建一个python函数,使用转置密码对消息进行解密。虽然它不断给我错误的输出,使信息更长。我认为错误与数组和for循环有关,但我不能100%确定 我的代码 public String TranspositionDecryptText (String EncryptedText, int Key) { double NumColumnsD = Math.ceil(EncryptedText.length() / Key); int NumColumns = (i
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;
}
我发现给定的文本字符串需要可以被密钥整除才能进行解密。在找到这个之后,我添加了一个循环,在加密之前在文本的末尾添加空格,这样解密程序就必须在加密之前修剪返回的文本。代码现在如下所示:
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;
}
}
公共类转置iPhone{
公共字符串TranspositionEncryptText(字符串文本,int键){
while(Text.length()%Key!=0){
Text=Text+“”;
}
ArrayList EncryptedText=new ArrayList(Key);//创建一个数组,数组中的元素数与Key的元素数相同
字符串HoldText=“”;
for(int col=0;col=NumRows-numshadedboxs))){
col=0;
行=行+1;
}
}
字符串NormalString=“”;
对于(int i=0;i
刚刚发现有多个错误,但我已经修复了其中一个错误。NormalArray元素的每个值在应该为空时都为空,为了解决这个问题,我在添加值之前使用了for循环设置值,for(int i=0;i
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;
}
}