java明文密码三重加密方法
程序员文章站
2024-03-31 21:09:46
本文介绍了两种密码加密的方法,这两种很常见可以再百度随意找到。
1.摩斯密码;
说道密码加密不得不提的方法。很是经典。
首先说一下他的对照表,直接上图。...
本文介绍了两种密码加密的方法,这两种很常见可以再百度随意找到。
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; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。