【发布时间】:2011-02-02 04:47:42
【问题描述】:
这几天我一直在空闲时间写这个加密算法,我以为我终于让它工作了,但是当我对某些字符进行处理时它开始出现故障。我进行了此设置,以使用循环键执行字符转换的替换。问题是仅在翻译一个字符后就被删除了。
解密代码如下:
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;
}
}
我给它的破坏程序的输入是123089648734
作为密钥,以及R EWW'U(AO)TP(MO!\QAU)
作为密文。它应该出来
我不想那样做!`
我只想知道是否有人可以修复代码,这样它就不会放弃这些答案。
【问题讨论】:
-
“我不知道代码格式是否按照我的方式通过这里。”当您在问题文本区域下方键入时,您会看到预览屏幕。你可能想用它来格式化你的问题。
-
我正在研究它。我现在编辑了几次并修复了它。对不起。
标签: java encryption shift