程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> Java-密碼加密,java-加密

Java-密碼加密,java-加密

編輯:JAVA綜合教程

Java-密碼加密,java-加密


介紹兩種密碼加密的方法:

這兩種很常見可以再百度隨意找到。

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 }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved