java明文密码三重加密方法
作者:A_book 时间:2022-09-01 05:59:26
本文介绍了两种密码加密的方法,这两种很常见可以再百度随意找到。
1.摩斯密码;
说道密码加密不得不提的方法。很是经典。
首先说一下他的对照表,直接上图。
核心思想就是替换明文密码,将字符对应的替换成-与.两种符号的组合;
2、栅栏密码;
所谓栅栏密码,就是把要加密的明文分成N个一组,然后把每组的第1个字连起来,形成一段无规律的话。 不过栅栏密码本身有一个潜规则,就是组成栅栏的字母一般不会太多。(一般不超过30个,也就是一、两句话)
这个没有什么好说的了,举个栗子:对明文ASDFGHJKL进行加密;分成两组:基数位置一组,偶数位置一组。然后两组合并成ADGJLSFHK;
我对这两种加密方式进行了结合,对一个明文密码进行了三重加密:第一层栅栏一次,第二层再栅栏一次,第三层再一次摩斯加密;
先是栅栏部分:
/*栅栏解密解密类*/
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;
}
}
然后是摩斯部分:
这一部分比较繁琐,在于摩斯密码与符号之间的匹配问题上,想着用一个数组去存储,然后循环进行匹配;但是我是一个不怕麻烦只求简单的一个人,就进行了一个个的比较;
然后在摩斯加密解密的时候进行了两次的栅栏加密解密;这样用到加密解密的时候只要调用摩斯类的加密解密函数即可。
/*摩斯加密解密类*/
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;
}
}
标签:java,密码,加密
0
投稿
猜你喜欢
使用idea和gradle编译spring5源码的方法步骤
2022-04-02 12:21:53
Spring Boot集成MyBatis实现通用Mapper的配置及使用
2023-07-03 18:17:10
Android App开发中创建Fragment组件的教程
2022-05-18 04:34:38
Android自定义listview布局实现上拉加载下拉刷新功能
2023-05-12 23:27:28
简单介绍java中equals以及==的用法
2023-01-28 07:47:41
详解Mybatis通用Mapper介绍与使用
2023-11-29 08:49:08
基于javaMybatis存进时间戳的问题
2023-11-29 02:55:51
c#图片处理之图片裁剪成不规则图形
2023-02-23 02:43:58
Spring JPA之save方法示例详解
2023-09-06 15:50:35
c#定时器使用示例详解
2022-01-09 03:31:05
CentOS安装jdk的三种方法
2022-01-13 06:24:41
App内切换语言详解
2023-04-12 11:21:44
Kotlin 封装万能SharedPreferences存取任何类型详解
2021-11-16 06:16:46
java读取properties文件的方法
2021-12-25 11:30:36
idea生成类注释和方法注释的正确方法(推荐)
2022-09-11 03:45:14
java String的深入理解
2022-10-30 23:50:37
Java图像之自定义角度旋转(实例)
2023-10-07 21:04:28
关于IDEA中spring-cloud-starter-alibaba-nacos-discovery 无法引入问题
2022-09-14 19:34:21
C#针对xml文件转化Dictionary的方法
2021-09-12 12:33:48
一篇文章带你深入了解Java基础(2)
2023-12-09 21:06:23