?? caeser1.java~64~
字號:
package my;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @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 + -