本文介绍了两种密码加密的方法,这两种很常见可以再百度随意找到。
1.摩斯密码;
说道密码加密不得不提的方法。很是经典。
首先说一下他的对照表,直接上图。
核心思想就是替换明文密码,将字符对应的替换成-与.两种符号的组合;
2、栅栏密码;
所谓栅栏密码,就是把要加密的明文分成N个一组,然后把每组的第1个字连起来,形成一段无规律的话。 不过栅栏密码本身有一个潜规则,就是组成栅栏的字母一般不会太多。(一般不超过30个,也就是一、两句话)
这个没有什么好说的了,举个栗子:对明文ASDFGHJKL进行加密;分成两组:基数位置一组,偶数位置一组。然后两组合并成ADGJLSFHK;
我对这两种加密方式进行了结合,对一个明文密码进行了三重加密:第一层栅栏一次,第二层再栅栏一次,第三层再一次摩斯加密;
先是栅栏部分:
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
|
/*栅栏解密解密类*/ public class Fence { /*加密部分*/ public String encryption (String password){ String p = new String(); String p1 = new String(); String p2 = new String(); for (int i = 0; i < password.length(); i++){ if(i%2 == 0) p1 += p.valueOf(password.charAt(i)); else p2 += p.valueOf(password.charAt(i)); } return p1+p2; } /*解密部分*/ public String decode(String FencePassword){ String password = new String(); String p = new String(); String p1 = FencePassword.substring( 0 , FencePassword.length()/ 2 ); String p2 = FencePassword.substring(FencePassword.length()/ 2 ); int i; for (i = 0 ; i < p1.length(); i++){ password += p.valueOf(p1.charAt(i)) + p.valueOf(p2.charAt(i)); } if (FencePassword.length()% 2 != 0 ) password += p.valueOf(p2.charAt(i)); return password; } } |
然后是摩斯部分:
这一部分比较繁琐,在于摩斯密码与符号之间的匹配问题上,想着用一个数组去存储,然后循环进行匹配;但是我是一个不怕麻烦只求简单的一个人,就进行了一个个的比较;
然后在摩斯加密解密的时候进行了两次的栅栏加密解密;这样用到加密解密的时候只要调用摩斯类的加密解密函数即可。
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
/*摩斯加密解密类*/ import java.util.StringTokenizer; public class Morse { /*摩斯密码与字符之间的匹配常量*/ final String A = ".-"; final String B = "---."; final String C = "-.-."; final String D = "-.."; final String E = "."; final String F = "..-."; final String G = "--."; final String H = "...."; final String I = ".."; final String J = ".---"; final String K = "-.-"; final String L = ".-.."; final String M = "--"; final String N = "-."; final String O = "---"; final String P = ".--."; final String Q = "--.-"; final String R = ".-."; final String S = "..."; final String T = "-"; final String U = "..-"; final String V = "...-"; final String W = ".--"; final String X = "-..-"; final String Y = "-.--"; final String Z = "--.."; final String $0 = "-----"; final String $1 = ".----"; final String $2 = "..---"; final String $3 = "...--"; final String $4 = "....-"; final String $5 = "....."; final String $6 = "-...."; final String $7 = "--..."; final String $8 = "---.."; final String $9 = "----."; final String period = ".-.-.-"; // . final String colon = "---..."; // : final String comma = "--..--"; // , final String semicolon = "-.-.-."; // ; final String question = "..--.."; // ? final String equal = "-...-"; // = final String doubleQuotation = ".-..-.";// " final String singleQuotation = ".----.";// ' final String slash = "-..-."; /// final String exclamation = "-.-.--"; // ! final String hyphen = "-....-"; // - final String underscore = "..--.-"; // _ final String lroundBrackets = "-.--."; // ( final String rroundBrackets = "-.--.-"; // ) final String $ = "...-..-"; // $ final String ampersand = ".-..."; // & final String at = ".--.-."; // @ final String plus = ".-.-."; // + /*加密对应的匹配*/ public String matching(String str) { switch (str) { case "A": return A; case "B": return B; case "C": return C; case "D": return D; case "E": return E; case "F": return F; case "G": return G; case "H": return H; case "I": return I; case "J": return J; case "K": return K; case "L": return L; case "M": return M; case "N": return N; case "O": return O; case "P": return P; case "Q": return Q; case "R": return R; case "S": return S; case "T": return T; case "U": return U; case "V": return V; case "W": return W; case "X": return X; case "Y": return Y; case "Z": return Z; case "0": return $0; case "1": return $1; case "2": return $2; case "3": return $3; case "4": return $4; case "5": return $5; case "6": return $6; case "7": return $7; case "8": return $8; case "9": return $9; case ".": return period; case ":": return colon; case ",": return comma; case ";": return semicolon; case "?": return question; case "=": return equal; case "\"": return doubleQuotation; case "\'": return singleQuotation; case "/": return slash; case "!": return exclamation; case "-": return hyphen; case "_": return underscore; case "(": return lroundBrackets; case ")": return rroundBrackets; case "$": return $; case "&": return ampersand; case "@": return at; case "+": return plus; } return " "; } /*摩斯加密*/ public String encryption(String password) { Fence f = new Fence(); password = f.encryption(password); password = f.encryption(password); String MorsePasswork = new String(); String mp = new String(); for (int i = 0; i < password.length(); i++) { mp = mp.valueOf(password.charAt(i)); MorsePasswork += matching(mp) + "/"; } return MorsePasswork; } /*解密对应换的匹配*/ public String matching2(String str) { if (str.equals(A)) return "A"; else if (str.equals(B)) return "B"; else if (str.equals(C)) return "C"; else if (str.equals(D)) return "D"; else if (str.equals(E)) return "E"; else if (str.equals(F)) return "F"; else if (str.equals(G)) return "G"; else if (str.equals(H)) return "H"; else if (str.equals(I)) return "I"; else if (str.equals(J)) return "J"; else if (str.equals(K)) return "K"; else if (str.equals(L)) return "L"; else if (str.equals(M)) return "M"; else if (str.equals(N)) return "N"; else if (str.equals(O)) return "O"; else if (str.equals(P)) return "P"; else if (str.equals(Q)) return "Q"; else if (str.equals(R)) return "R"; else if (str.equals(S)) return "S"; else if (str.equals(T)) return "T"; else if (str.equals(U)) return "U"; else if (str.equals(V)) return "V"; else if (str.equals(W)) return "W"; else if (str.equals(X)) return "X"; else if (str.equals(Y)) return "Y"; else if (str.equals(Z)) return "Z"; else if (str.equals($0)) return "0"; else if (str.equals($1)) return "1"; else if (str.equals($2)) return "2"; else if (str.equals($3)) return "3"; else if (str.equals($4)) return "4"; else if (str.equals($5)) return "5"; else if (str.equals($6)) return "6"; else if (str.equals($7)) return "7"; else if (str.equals($8)) return "8"; else if (str.equals($9)) return "9"; else if (str.equals(period)) return "."; else if (str.equals(colon)) return ":"; else if (str.equals(comma)) return ","; else if (str.equals(semicolon)) return ";"; else if (str.equals(question)) return "?"; else if (str.equals(equal)) return "="; else if (str.equals(doubleQuotation)) return "\""; else if (str.equals(singleQuotation)) return "\'"; else if (str.equals(slash)) return "/"; else if (str.equals(exclamation)) return "!"; else if (str.equals(hyphen)) return "-"; else if (str.equals(underscore)) return "_"; else if (str.equals(lroundBrackets)) return "("; else if (str.equals(rroundBrackets)) return ")"; else if (str.equals($)) return "$"; else if (str.equals(ampersand)) return "&"; else if (str.equals(at)) return "@"; else if (str.equals(plus)) return "+"; else return " "; } /*摩斯解密*/ public String decode(String MorsePassword) { String password = new String(); Fence f = new Fence(); StringTokenizer p = new StringTokenizer(MorsePassword, "/" ); while (p.hasMoreElements()) { password += matching2(p.nextToken()); } password = f.decode(password); password = f.decode(password); return password; } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。