我正在尝试解密文本文件。我有原始文本和加密文本,加上加密代码本身。我认为它与XOR类似 密码,但我不确定。我读过有关已知明文攻击的内容,但无法找到在我的任务中尝试实施的好例子。 我知道java正在使用线性同余公式,我可以使用强力来找到解密的密钥。 在我的代码中,我试图使用种子。我尝试在解密后每次都比较字符串,但我没有成功。任何帮助都会非常感激。
public class test {
public static int KEY = 0; // enter code here
public static Random _rand = new Random(KEY);
private static int counter = 0;
public static void main(String[] args) throws Exception {
init_seed();
simple_test0();
decMSG();
}
public static void init_seed() {
_rand = new Random(KEY);
}
public static void simple_test0() {
String [] msg = {"Iron Beast"};
int len = msg.length;
String[] msg_enc = new String[len];
init_seed();
for(int i = 0; i < len; i++) {
msg_enc[i] = enc(msg[i]);
}
init_seed();
System.out.println(_rand);
for(int i=0;i<len;i++) {
String se2 = enc(msg_enc[i]);
System.out.println(i+") orig: "+msg[i]+"\n"+" enc(orig): "+msg_enc[i]+"\n"+" enc(enc(orig)): "+se2);
System.out.println();
}
}
public static String enc(String msg) {
String ans ="";
for(int i = 0; i < msg.length(); i++) {
char c = msg.charAt(i);
int s = c;
int rd = _rand.nextInt() % (256*256);
int s2 = s ^ rd;
char c2 = (char) (s2);
ans += c2;
}
return ans;
}
public static void decMSG() throws Exception {
boolean ans = false;
String file_name = "msg";
String msg = "first text line";
FileReader fr = new FileReader(file_name);
BufferedReader is = new BufferedReader(fr);
String s = is.readLine();
int key = 0;
while(ans == false ) {
KEY =key;
s = enc(s);
if(msg.equals(s)) {
System.out.println(s + "\t" + KEY);
ans = true;
counter++;
}
_rand = new Random(key);
System.out.println( KEY);
if(key > 10000000) {
key=0;
}
key++;
}
System.out.println(counter);
is.close();
fr.close();
}
}