📄 caeser1.java
字号:
package my;
/**
* <p>Title: 人工智能实验设计</p>
* <p>Description: 主要有关密码的解密</p>
* <p>Copyright: 李少君@author (c) 2006</p>
* <p>Company: fzu </p>
* @author : 李少君
* @version 1.0
*/
import java.util.*;
import java.io.*;
import javax.swing.*;
public class Caeser1 {
HashMap dic;
String Text = new String();
String explain = new String(" ");
String textPlaint;
int[] Time = new int[26];
int numOfword = 0;
void SetText(String s) {
this.Text = s;
}
void SetExplain(String s) {
this.explain = s;
}
String getout() {
return textPlaint;
}
String getText() {
return Text;
}
String getexplain() {
return explain;
}
private void initialWordClass(String s) {
dic = new HashMap();
String temp = "";
try {
FileInputStream fis = new FileInputStream(s);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader bfr = new BufferedReader(isr);
do {
temp = bfr.readLine();
if (temp == null)
break;
dic.put(temp, temp);
}
while (true);
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, "找不到词库文件!", "Message Diolog ",
JOptionPane.ERROR_MESSAGE);
}
}
public Caeser1() {
}
public int Max(int num, int[] a) {
int max = a[0];
int key = 0;
for (int i = 0; i < 26; i++) {
if (max < a[i]) {
max = a[i];
key = i;
}
}
if (max > (num * 9 / 10) && num != 0)
return key;
else
if (num == 0)
return key;
else
return -1;
}
public void Decode1(int key) {
int L = Text.length();
int flagOfWord = 0;
boolean choose;
StringBuffer out = new StringBuffer();
StringBuffer tent = new StringBuffer();
int countOfWord = 0;
for (int i = 0; i < L; i++) {
char ch = Text.charAt(i);
if (ch >= 'A' && ch <= 'Z') {
ch = (char) ( (ch + 32 - 97 - key + 26) % 26 + 97);
tent.append(ch);
out.append(ch);
countOfWord++;
flagOfWord = 1;
}
else if (ch >= 'a' && ch <= 'z') {
ch = (char) ( (ch - 97 - key + 26) % 26 + 97);
tent.append(ch);
out.append(ch);
countOfWord++;
flagOfWord = 1;
}
else {
if (flagOfWord == 1) {
// long start = System.currentTimeMillis();
choose = dic.containsKey(tent.toString());
// long finish=System.currentTimeMillis();
//System.out.println("time:: "+(finish-start));
if (choose == true) {
// System.out.println("tent:: "+tent);
Time[key]++;
// if(key==0)
//numOfword++;
}
numOfword++;
tent.delete(0, countOfWord);
countOfWord = 0;
flagOfWord = 0;
}
out.append(ch);
}
}
textPlaint = out.toString();
}
public void C_Decode() {
//System.out.println("text: " + Text);
int key;
long start = System.currentTimeMillis();
initialWordClass("word.dic");
for (key = 0; key < 26; key++) {
//System.out.println("key: " + key);
numOfword = 0;
Time[key] = 0;
Decode1(key);
if (Time[key] == numOfword && numOfword != 0) {
long finish = System.currentTimeMillis();
//System.out.println("time:: "+(finish-start));
this.SetExplain(" 解密成功! :)\n 此密文的密钥是" + key + "\n 耗时:" +
(finish - start) + "毫秒!");
break;
}
}
int item;
if (key == 26) {
item = Max(numOfword, Time);
if (item == -1) {
this.SetExplain(" 对不起,无法解密!");
//explain = explain.toString();
}
else {
this.SetExplain(" 解密成功! :)\n 此密文的密钥是" + item);
//explain = explain.toString();
Decode1(item);
}
}
}
//heuristic
public void C_HDecode(int chance) {
//相对高的频率,可能对应于明文字母集合{r,n,I,o,a,s}。
//最低频率的字母,很可能包括在集合{w,v,b,k,x,q,j,z}。
long start1 = System.currentTimeMillis();
initialWordClass("word.dic");
int[] Frequent = new int[26];
char[] Referto = {
'e', 't', 'a', 'o', 'i', 'n', 's', 'h', 'r', 'd', 'l',
'c', 'u', 'm', 'w', 'f', 'g', 'y', 'p', 'b', 'v', 'k', 'j', 'x', 'q',
'z'};
int[] Compare = new int[26];
int key1, key2;
int Long = Text.length();
for (int i = 0; i < Long; i++) { //统计26个字母的出现频率
char c = Text.charAt(i);
if (c <= 'z' && c >= 'a')
Frequent[c - 'a']++;
else if (c <= 'Z' && c >= 'A')
Frequent[c - 'A']++;
}
//选择出频率高的
if (chance == 1) {
int tem = Max(0, Frequent);
for (int i = 0; i < 26; i++) {
key1 = ('a' + tem - Referto[i] + 26) % 26;
numOfword = 0;
Time[key1] = 0;
Decode1(key1);
if (Time[key1] == numOfword && numOfword != 0) {
long finish1 = System.currentTimeMillis();
this.SetExplain(" 解密成功! :)\n 本启发式函数可以解此密文哦!:P\n 此密文的密钥是" + key1 +
"\n 欢迎再次使用!" + "\n 耗时" + (finish1 - start1) + "毫秒!");
break;
}
//}
if (i == 25) {
key2 = Max(numOfword, Time);
if (key2 == -1) {
this.SetExplain("Sorry!:< \n 启发式的启发函数对此密文不够解密!\n也许您可以尝试输入更长的信息密文!\n鉴于本算法是对字母的信息统计!\n还有待改进啊!\n - -0");
//explain = explain.toString();
}
else {
long finish2 = System.currentTimeMillis();
this.SetExplain("解密成功! :)\n 此密文的密钥是" + key2 + "\n 耗时" +
(finish2 - start1) + "毫秒!");
//explain = explain.toString();
Decode1(key2);
}
}
}
}
else { //代入法的程序
//for (int k = 0; k < 6; k++) {
int[] tin = Frequent;
char[] Place = new char[26];
for (int i = 0; i < 26; i++) {
int y = Max(0, tin);
Place[y] = Referto[i];
tin[y] = -1;
char c1 = (char) ('a' + y);
char c2 = (char) ('A' + y);
Text.replace(c1, Place[y]);
Text.replace(c2, Place[y]);
}
//}
//System.out.print("place is:"+Place);
numOfword = 0;
Time[0] = 0;
Decode1(0);
if (Time[0] == numOfword && numOfword != 0 ||
Time[0] > numOfword * 9 / 10) {
String temp = "";
String temp1 = "";
for (int i = 0; i < 26; i++) {
temp += Place[i];
temp += ' ';
temp1 += (char) ('a' + i);
temp1 += ' ';
}
long finish3 = System.currentTimeMillis();
this.SetExplain("解密成功! :)\n 此密文的替换表是:\n" + temp1 + "\n" + temp+"\n耗时"+(finish3-start1)+"微秒!");
}
else
this.SetExplain("Sorry!:< \n 此解密算法不够解密! 您可以尝试输入更多的信息!");
//}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -