码迷,mamicode.com
首页 > 编程语言 > 详细

Java-密码加密

时间:2016-07-01 21:19:09      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:

介绍两种密码加密的方法:

这两种很常见可以再百度随意找到。

1.摩斯密码;说道密码加密不得不提的方法。很是经典。

首先说一下他的对照表,直接上图。

技术分享

技术分享

技术分享

技术分享

核心思想就是替换明文密码,将字符对应的替换成-与.两种符号的组合;

2、栅栏密码;

所谓栅栏密码,就是把要加密的明文分成N个一组,然后把每组的第1个字连起来,形成一段无规律的话。 不过栅栏密码本身有一个潜规则,就是组成栅栏的字母一般不会太多。(一般不超过30个,也就是一、两句话)

这个没有什么好说的了,举个栗子:对明文ASDFGHJKL进行加密;分成两组:基数位置一组,偶数位置一组。然后两组合并成ADGJLSFHK;

 

 

我对这两种加密方式进行了结合,对一个明文密码进行了三重加密:第一层栅栏一次,第二层在栅栏一次,第三层在一次摩斯加密;

先是栅栏部分:

 1 /*栅栏解密解密类*/
 2 public class Fence {
 3     /*加密部分*/
 4     public String encryption (String password){
 5         String p = new String();
 6         String p1 = new String();
 7         String p2 = new String();
 8         for    (int i = 0; i < password.length(); i++){
 9             if(i%2 == 0)
10                 p1 += p.valueOf(password.charAt(i));
11             else
12                 p2 += p.valueOf(password.charAt(i)); 
13         }
14         return p1+p2;
15     }
16     /*解密部分*/
17     public String decode(String FencePassword){
18         String password = new String();
19         String p = new String();
20         String p1 = FencePassword.substring(0, FencePassword.length()/2);
21         String p2 = FencePassword.substring(FencePassword.length()/2);
22         int i;
23         for    (i = 0; i < p1.length(); i++){
24             password += p.valueOf(p1.charAt(i)) + p.valueOf(p2.charAt(i));
25         }
26         if(FencePassword.length()%2 != 0)
27             password += p.valueOf(p2.charAt(i));
28         return password;
29     }
30 }

然后是摩斯部分:

这一部分比较繁琐,在于摩斯密码与符号之间的匹配问题上,想着用一个数组去存储,然后循环进行匹配;但是我是一个不怕麻烦只求简单的一个人,就进行了一个个的比较;

然后在摩斯加密解密的时候进行了两次的栅栏加密解密;这样用到加密解密的时候只要调用摩斯类的加密解密函数即可。

  1 /*摩斯加密解密类*/
  2 import java.util.StringTokenizer;
  3 
  4 public class Morse {
  5     /*摩斯密码与字符之间的匹配常量*/
  6     final String A = ".-";
  7     final String B = "---.";
  8     final String C = "-.-.";
  9     final String D = "-..";
 10     final String E = ".";
 11     final String F = "..-.";
 12     final String G = "--.";
 13     final String H = "....";
 14     final String I = "..";
 15     final String J = ".---";
 16     final String K = "-.-";
 17     final String L = ".-..";
 18     final String M = "--";
 19     final String N = "-.";
 20     final String O = "---";
 21     final String P = ".--.";
 22     final String Q = "--.-";
 23     final String R = ".-.";
 24     final String S = "...";
 25     final String T = "-";
 26     final String U = "..-";
 27     final String V = "...-";
 28     final String W = ".--";
 29     final String X = "-..-";
 30     final String Y = "-.--";
 31     final String Z = "--..";
 32     final String $0 = "-----";
 33     final String $1 = ".----";
 34     final String $2 = "..---";
 35     final String $3 = "...--";
 36     final String $4 = "....-";
 37     final String $5 = ".....";
 38     final String $6 = "-....";
 39     final String $7 = "--...";
 40     final String $8 = "---..";
 41     final String $9 = "----.";
 42     final String period = ".-.-.-"; // .
 43     final String colon = "---..."; // :
 44     final String comma = "--..--"; // ,
 45     final String semicolon = "-.-.-."; // ;
 46     final String question = "..--.."; // ?
 47     final String equal = "-...-"; // =
 48     final String doubleQuotation = ".-..-.";// "
 49     final String singleQuotation = ".----.";//
 50     final String slash = "-..-."; ///
 51     final String exclamation = "-.-.--"; // !
 52     final String hyphen = "-....-"; // -
 53     final String underscore = "..--.-"; // _
 54     final String lroundBrackets = "-.--."; // (
 55     final String rroundBrackets = "-.--.-"; // )
 56     final String $ = "...-..-"; // $
 57     final String ampersand = ".-..."; // &
 58     final String at = ".--.-."; // @
 59     final String plus = ".-.-."; // +
 60     /*加密对应的匹配*/
 61     public String matching(String str) {
 62         switch (str) {
 63         case "A":
 64             return A;
 65         case "B":
 66             return B;
 67         case "C":
 68             return C;
 69         case "D":
 70             return D;
 71         case "E":
 72             return E;
 73         case "F":
 74             return F;
 75         case "G":
 76             return G;
 77         case "H":
 78             return H;
 79         case "I":
 80             return I;
 81         case "J":
 82             return J;
 83         case "K":
 84             return K;
 85         case "L":
 86             return L;
 87         case "M":
 88             return M;
 89         case "N":
 90             return N;
 91         case "O":
 92             return O;
 93         case "P":
 94             return P;
 95         case "Q":
 96             return Q;
 97         case "R":
 98             return R;
 99         case "S":
100             return S;
101         case "T":
102             return T;
103         case "U":
104             return U;
105         case "V":
106             return V;
107         case "W":
108             return W;
109         case "X":
110             return X;
111         case "Y":
112             return Y;
113         case "Z":
114             return Z;
115         case "0":
116             return $0;
117         case "1":
118             return $1;
119         case "2":
120             return $2;
121         case "3":
122             return $3;
123         case "4":
124             return $4;
125         case "5":
126             return $5;
127         case "6":
128             return $6;
129         case "7":
130             return $7;
131         case "8":
132             return $8;
133         case "9":
134             return $9;
135         case ".":
136             return period;
137         case ":":
138             return colon;
139         case ",":
140             return comma;
141         case ";":
142             return semicolon;
143         case "?":
144             return question;
145         case "=":
146             return equal;
147         case "\"":
148             return doubleQuotation;
149         case "\‘":
150             return singleQuotation;
151         case "/":
152             return slash;
153         case "!":
154             return exclamation;
155         case "-":
156             return hyphen;
157         case "_":
158             return underscore;
159         case "(":
160             return lroundBrackets;
161         case ")":
162             return rroundBrackets;
163         case "$":
164             return $;
165         case "&":
166             return ampersand;
167         case "@":
168             return at;
169         case "+":
170             return plus;
171         }
172         return " ";
173     }
174     /*摩斯加密*/
175     public String encryption(String password) {
176         Fence f = new Fence();
177         password = f.encryption(password);
178         password = f.encryption(password);
179         String MorsePasswork = new String();
180         String mp = new String();
181         for (int i = 0; i < password.length(); i++) {
182             mp = mp.valueOf(password.charAt(i));
183             MorsePasswork += matching(mp) + "/";
184         }
185         return MorsePasswork;
186     }
187     /*解密对应换的匹配*/
188     public String matching2(String str) {
189         if (str.equals(A))
190             return "A";
191         else if (str.equals(B))
192             return "B";
193         else if (str.equals(C))
194             return "C";
195         else if (str.equals(D))
196             return "D";
197         else if (str.equals(E))
198             return "E";
199         else if (str.equals(F))
200             return "F";
201         else if (str.equals(G))
202             return "G";
203         else if (str.equals(H))
204             return "H";
205         else if (str.equals(I))
206             return "I";
207         else if (str.equals(J))
208             return "J";
209         else if (str.equals(K))
210             return "K";
211         else if (str.equals(L))
212             return "L";
213         else if (str.equals(M))
214             return "M";
215         else if (str.equals(N))
216             return "N";
217         else if (str.equals(O))
218             return "O";
219         else if (str.equals(P))
220             return "P";
221         else if (str.equals(Q))
222             return "Q";
223         else if (str.equals(R))
224             return "R";
225         else if (str.equals(S))
226             return "S";
227         else if (str.equals(T))
228             return "T";
229         else if (str.equals(U))
230             return "U";
231         else if (str.equals(V))
232             return "V";
233         else if (str.equals(W))
234             return "W";
235         else if (str.equals(X))
236             return "X";
237         else if (str.equals(Y))
238             return "Y";
239         else if (str.equals(Z))
240             return "Z";
241         else if (str.equals($0))
242             return "0";
243         else if (str.equals($1))
244             return "1";
245         else if (str.equals($2))
246             return "2";
247         else if (str.equals($3))
248             return "3";
249         else if (str.equals($4))
250             return "4";
251         else if (str.equals($5))
252             return "5";
253         else if (str.equals($6))
254             return "6";
255         else if (str.equals($7))
256             return "7";
257         else if (str.equals($8))
258             return "8";
259         else if (str.equals($9))
260             return "9";
261         else if (str.equals(period))
262             return ".";
263         else if (str.equals(colon))
264             return ":";
265         else if (str.equals(comma))
266             return ",";
267         else if (str.equals(semicolon))
268             return ";";
269         else if (str.equals(question))
270             return "?";
271         else if (str.equals(equal))
272             return "=";
273         else if (str.equals(doubleQuotation))
274             return "\"";
275         else if (str.equals(singleQuotation))
276             return "\‘";
277         else if (str.equals(slash))
278             return "/";
279         else if (str.equals(exclamation))
280             return "!";
281         else if (str.equals(hyphen))
282             return "-";
283         else if (str.equals(underscore))
284             return "_";
285         else if (str.equals(lroundBrackets))
286             return "(";
287         else if (str.equals(rroundBrackets))
288             return ")";
289         else if (str.equals($))
290             return "$";
291         else if (str.equals(ampersand))
292             return "&";
293         else if (str.equals(at))
294             return "@";
295         else if (str.equals(plus))
296             return "+";
297         else
298             return " ";
299     }
300     /*摩斯解密*/
301     public String decode(String MorsePassword) {
302         String password = new String();
303         Fence f = new Fence();
304         StringTokenizer p = new StringTokenizer(MorsePassword, "/");
305         while (p.hasMoreElements()) {
306             password += matching2(p.nextToken());
307         }
308         password = f.decode(password);
309         password = f.decode(password);
310         return password;
311     }
312 }

 

Java-密码加密

标签:

原文地址:http://www.cnblogs.com/ABook/p/5634238.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!