自制32位加密(java版)
闲来无事,自己制作32位建议加密工具,我把自己的思路和大家分享下:
下载加密程序链接:https://download.csdn.net/download/qickcao/13125302
1、创建加密字典
char encrp[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
2、判断需要加密的密码长度,
int len = password.length();
if(len < 32){
//TODO 补足32位
}else if(len == 32){
//直接加密
password = encrp(password);
}else{
System.out.println("警告:需要加密的密码超出32位");
password = null;
}
3、加密模块
根据简单的公式把密码中的单个字符转换成字典中的字符。
private static String encrp(String password){
int encrpLength = encrp.length;//加密字典长度
char temp;//临时字符,用来接收循环出来的字符
int encrpIndex;//加密字典坐标,用来确定加密后的字符在字典的下标,并取出加密字符
String returnString = "";
for(int i=0;i<password.length();i++){
temp = password.charAt(i);
//确定该字符在加密库中的坐标: [(字符在字符串的坐标+字符在Integer的坐标)*字符在加密字符库的坐标] % 加密库的长度
encrpIndex = ((i+Integer.valueOf(temp))* ArrayUtil.getIndex(encrp, temp)) % encrpLength;
//把对应的值取出来
returnString += encrp[encrpIndex];
}
//返回
return returnString;
}
4、最主要的是在补足32位模块,最先的思路是直接在原密码后面补足,现在的密码一般用户都设置为8位,这样后面补24位,尾部很长一段都相同,这样做的加密效果不明显,因此Pass掉了这种方案。
修正后方案:把原密码分割成4块,这样每块应该有8个字符,然后不足8个字符的补成8个,
for(int i=0;i<4;i++){//分割成4段
String tempStr = "";
for(int j=0;j<divisionLength;j++){//取出4段的值
char c1 = password.charAt(k);
k++;
tempStr += c1;
}
int needLength = 8-divisionLength;//需要补的长度
char strLastChar = tempStr.charAt(tempStr.length()-1);//字符串最后一位字符
int location = Integer.valueOf(strLastChar);//最后一个字符在库中的索引号,用他来判断是在前面补足还是在后面补足
if(location%2==0){//索引号为偶数在后面补数
for(int l=0;l<needLength;l++){//补足每段的长度为8位
tempStr += ((l+divisionLength)*163%7);
}
}else{//索引号为奇数在前面补数
String temp = "";
for(int l=0;l<needLength;l++){//补足每段的长度为8位
temp += ((l+divisionLength)*163%7);
}
temp += tempStr;
tempStr = temp;
}
splicing += tempStr;
}
5、用法演示:
package test;
import util.MD5Util;
public class TestMd5Util {
public static void main(String[] args) {
String password = "wef857879";
password = MD5Util.to32Bit(password);
System.out.println("加密后的密码为:"+password);
System.out.println("加密后的密码长度为:"+password.length());
}
}
6、演示结果:
写在最后: 我下载里面有完整代码链接:有空的话你也可以试一下,欢迎评论交流。