Java-密码加密

Posted A_book

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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         WPYOU主题加密码代码的解码

Java密码加密与解密

如何使用java对密码加密 加密方式aes

DES加密算法详细原理以及Java代码实现

Java通过BCrypt加密

Java Class 加密工具 ClassFinal