上篇显示了用错误的key得到了错误的结果,现在使用正确的key

这样就得到了正确的结果。

当然,这个程序只是演示凯撒密码的一个思路,没有对其他符号加密,这样有个很严重的后果,就是很key容易被破解。

举例:(key是*不告诉你,嘿嘿)

对 !A! 加密 得到了 !C!

对 !B! 加密 得到了 !D!

看出来了么?key是几?

我还提到了一个暴力破解,简单的说一种思路:

使用频率字典

对常用的单词做成字典,比如this,he,she,me,hello等

然后对key进行枚举,从0到26一一列举,然后对每一种情况检查是否包含字典中的词。如果有,就输出这种破解结果。对于输出的所有结果进行人工筛选。

输出了三种结果,其中第二种是正确的。

最后附上源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace CaesarCode
{
public partial class WinForm : Form
{
public WinForm()
{
InitializeComponent();
}
private void Caesar(int choose)
{
bool flag;
int key;
int len;
int i;
int member;
string info = info = txtInput.Text;
flag = int.TryParse(txtKey.Text, out key);
if (flag == true && info.Length > 0)
{
if (choose != 1)
key = -key;
txtOutput.Clear();
len = info.Length;
for (i = 0; i < len; i++)
{
member = info[i];
if (member >= 'a' && member <= 'z')
{
member = (member + key % 26);
if (member < 'a') member = (member + 26);
if (member > 'z') member = (member - 26);
txtOutput.Text += (char)member;
}
else if (member >= 'A' && member <= 'Z')
{
member = (member + key % 26);
if (member < 'A') member = (member + 26);
if (member > 'Z') member = (member - 26);
txtOutput.Text += (char)member;
}
else if (member >= '0' && member <= '9')
{
member = (member + key % 10);
if (member < '0') member = (member + 10);
if (member > '9') member = (member - 10);
txtOutput.Text += (char)member;
}
else if (member >= 0 && member <= 47)
{
member = (member + key % 48);
if (member < 0) member = (member + 48);
if (member > 47) member = (member - 48);
txtOutput.Text += (char)member;
}
else if (member >= 58 && member <= 64)
{
member = (member + key % 7);
if (member < 58) member = (member + 7);
if (member > 64) member = (member - 7);
txtOutput.Text += (char)member;
}
else if (member >= 91 && member <= 96)
{
member = (member + key % 6);
if (member < 91) member = (member + 6);
if (member > 96) member = (member - 6);
txtOutput.Text += (char)member;
}
else
{
txtOutput.Text += (char)(member + key);
}
}
}
else
{
MessageBox.Show("密钥输入有误!/文本出现错误!");
txtKey.Clear();
txtKey.Focus();
}
}
private void btnInfoToCode_Click(object sender, EventArgs e)
{
Caesar(1);
}
private void btnCodeToInfo_Click(object sender, EventArgs e)
{
Caesar(2);
}
private void btnClear_Click(object sender, EventArgs e)
{
txtOutput.Clear();
txtInput.Clear();
txtKey.Clear();
}
private void btnForce_Click(object sender, EventArgs e)
{
int len;
int i;
int j;
char member;
bool tryDec = false;
string info = info = txtInput.Text;
string[] dictionary = { "the", "ello", "you", "he", "she" };
string[] result = new string[26];
txtOutput.Clear();
for (j = 0; j < 26; j++)
{
len = info.Length;
result[j] = "";
for (i = 0; i < len; i++)
{
member = info[i];
if (member >= 'a' && member <= 'z')
{
member = (char)(member - j % 26);
if (member < 'a') member = (char)(member + 26);
if (member > 'z') member = (char)(member - 26);
result[j] += member;
}
else if (member >= 'A' && member <= 'Z')
{
member = (char)(member - j % 26);
if (member < 'A') member = (char)(member + 26);
if (member > 'Z') member = (char)(member - 26);
result[j] += member;
}
else if (member >= '0' && member <= '9')
{
member = (char)(member - j % 10);
if (member < '0') member = (char)(member + 10);
if (member > '9') member = (char)(member - 10);
result[j] += member;
}
else
{
result[j] += member + j;
}
}
for (i = 0; i < 5; i++)
{
if (result[j].Contains(dictionary[i]))
{
tryDec = true;
break;
}
}
if (tryDec == true)
{
txtOutput.Text += (result[j] + "/t");
tryDec = false;
}
}
if (txtOutput.Text.Length == 0)
MessageBox.Show("破解失败!");
}
private void btnImport_Click(object sender, EventArgs e)
{
string filePath = "";
OpenFileDialog dialogOpenFile = new OpenFileDialog();
dialogOpenFile.AddExtension = true;
dialogOpenFile.Filter = "text files(*.txt)|*.txt";
dialogOpenFile.CheckPathExists = true;
dialogOpenFile.Title = "Import Text";
DialogResult result = dialogOpenFile.ShowDialog();
if (result == DialogResult.OK)
{
filePath = dialogOpenFile.FileName.ToString();
using (StreamReader myReader = new StreamReader(filePath))
{
txtInput.Text = myReader.ReadToEnd();
}
}
}
private void btnExport_Click(object sender, EventArgs e)
{
string filePath = "";
SaveFileDialog dialogSaveFile = new SaveFileDialog();
dialogSaveFile.AddExtension = true;
dialogSaveFile.Filter = "text files(*.txt)|*.txt";
dialogSaveFile.CheckPathExists = true;
dialogSaveFile.Title = "Export Text";
DialogResult result = dialogSaveFile.ShowDialog();
if (result == DialogResult.OK)
{
filePath = dialogSaveFile.FileName.ToString();
using (StreamWriter myWriter = new StreamWriter(filePath))
{
myWriter.Write(txtOutput.Text);
}
}
}
}
}