我尝试使用Railfence Cipher的转置密码实现。
我以3个轨道为例。
代码成功打印加密代码,但代码的问题是它停止后并且不打印解密文本。因此,我无法找到停止的确切位置。
请帮忙。
import java.util.Scanner;
class RailfenceCipher
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
String input=in.nextLine();
StringBuffer[] rail=new StringBuffer[3];
for(int t=0;t<3;t++) rail[t]=new StringBuffer();
int i=0,j=0;
while(true)
{
if(j<input.length())
{
while(i<=2)
{
rail[i].append(input.charAt(j++));
i++;
}
i=1;
while(i>=0)
{
rail[i].append(input.charAt(j++));
i--;
}
i=1;
}
else break;
}
System.out.println("Enciphered text: ");
for(int u=0;u<3;u++)
{
System.out.print(""+rail[u]);
}
StringBuffer decipher=new StringBuffer();
int tot=0,min=rail[0].length();
for(int u=0;u<3;u++)
{
tot+=rail[u].length();
min=(rail[u].length()<min)?rail[u].length():min;
}
i=0;j=0;
while(decipher.length()<=tot)
{{
while(i<=2)
{
decipher.append(rail[i].charAt(j));
i++;
}
i=1;
j++;
while(i>=0)
{
decipher.append(rail[i].charAt(j));
i--;
}
i=1;
j++;}
}
System.out.println();
System.out.println("Deciphered text: ");
System.out.println(decipher);
}
}
我得到的输出是:
D:\ exp-cipher&gt; javac RailfenceCipher.java
D:\exp-cipher>java RailfenceCipher
WEAREDISCOVEREDFLEEATONCE
Enciphered text:
WECRLTEERDSOEEFEAOCAIVDEN