我一直在空闲时间写这个加密算法几天,我以为我终于让它工作了,但是当我对某些字符进行处理时它就开始出现故障了。我有这个设置用循环键替换字符的转换。问题是在翻译后的一个字符后会被删除。
解密代码如下:
import java.util.Scanner;
import java.io.*;
/* File CycleDeCipher.java*/
public class CycleDeCipher
{
public static void main(String[] args)
{
new CycleDeCipher();
}
public CycleDeCipher()
{
String plainTxt;
Scanner in = new Scanner(System.in);
System.out.println("This program decrypts My Cyclical Substitution Algorithm. v0.2");
System.out.println("Enter a multi digit number : ");
Long mainKey = new Long(in.nextLong());;
System.out.print("Enter your Cipher Text message :");
in.nextLine();
plainTxt = new String(in.next());
in.nextLine();
int[] keys = longParser(mainKey);
String cipherTxt="";
int j = 0;
while(j < plainTxt.length())
{
cipherTxt+=decryptCharacter(plainTxt.charAt(j),keys[j%4]);
j++;
System.out.println("char number " + j + " successfully translated!");
}
System.out.println("Your text is translated to :"+cipherTxt.toUpperCase());
}
private String decryptCharacter(Character ch, int key)
{
System.out.println("Decrypting character "+ch.toString() + " with key "+key);
if(Character.isLetter(ch)){
ch = (char) ((int) Character.toLowerCase(ch) - key%10);
}
else {
ch = (char) ((int) ch-key%10);
}
return(ch.toString());
}
public int[] longParser(Long key)
{
System.out.println("Parsing long to crypto keys...");
int i = 0;
int[] result;
String sInput = new String(key.toString());
char[] keys = new char[sInput.length()];
for(i = 0; i < sInput.length(); i++)
{
keys[i] = sInput.charAt(i);
}
i = 0;
result = new int[sInput.length()];
for(i=0; i<keys.length; i++)
{
result[i] = (int) keys[i];
}
return result;
}
}
import java.util.Scanner;
import java.io.*;
/* File CycleDeCipher.java*/
public class CycleDeCipher
{
public static void main(String[] args)
{
new CycleDeCipher();
}
public CycleDeCipher()
{
String plainTxt;
Scanner in = new Scanner(System.in);
System.out.println("This program decrypts My Cyclical Substitution Algorithm. v0.2");
System.out.println("Enter a multi digit number : ");
Long mainKey = new Long(in.nextLong());;
System.out.print("Enter your Cipher Text message :");
in.nextLine();
plainTxt = new String(in.next());
in.nextLine();
int[] keys = longParser(mainKey);
String cipherTxt="";
int j = 0;
while(j < plainTxt.length())
{
cipherTxt+=decryptCharacter(plainTxt.charAt(j),keys[j%4]);
j++;
System.out.println("char number " + j + " successfully translated!");
}
System.out.println("Your text is translated to :"+cipherTxt.toUpperCase());
}
private String decryptCharacter(Character ch, int key)
{
System.out.println("Decrypting character "+ch.toString() + " with key "+key);
if(Character.isLetter(ch)){
ch = (char) ((int) Character.toLowerCase(ch) - key%10);
}
else {
ch = (char) ((int) ch-key%10);
}
return(ch.toString());
}
public int[] longParser(Long key)
{
System.out.println("Parsing long to crypto keys...");
int i = 0;
int[] result;
String sInput = new String(key.toString());
char[] keys = new char[sInput.length()];
for(i = 0; i < sInput.length(); i++)
{
keys[i] = sInput.charAt(i);
}
i = 0;
result = new int[sInput.length()];
for(i=0; i<keys.length; i++)
{
result[i] = (int) keys[i];
}
return result;
}
}
我不想这样做!“
我只是想知道是否有人可以修复代码,因此它不会放弃这些答案。
答案 0 :(得分:0)
问题在于输入处理,而不是算法。默认情况下,java.util.Scanner在空白字符(包括输入字符串的第二个字符的空格)上分隔标记。因此,对in.next()的调用返回一个带有单个字符('R')的字符串,然后处理该字符串并返回单个输出字符。
解决这个问题的一个简单方法是使用Scanner.nextLine()而不是next来获取输入文本,这将获得该行上的所有字符(包括空格):
System.out.print("Enter your Cipher Text message :");
in.nextLine();
plainTxt = new String(in.nextLine());