这次写的移位加密是比上次的移位加密稍微加了一下其他元素,虽然不是很适用,但是对于我来说是一次提升,锻炼自己的机会,以后会去研究一些比较复杂的加密,直到可以放心的加入项目中使用,好了,不扯了进入正题:这次的加密呢,只对数字加密,对数字的位数也是有要求的不要超出15位就可以。
移位步骤:
- 1.先运算一下(9936/2)+(9936%2)+5-2
- 2.转为字节数组
- 3.遍历数组的每个值乘6在放入List(二次运算),
- 4.移位加密
解密步骤就是跟移位步骤反过来。
代码部分:
public class Encryption {
public static void main(String[] args) {
Encryptions(1234L);
}
//加密
public static void Encryptions(long str){
System.out.println(“加密前密码:”+str);
//第一次算法
long result=(str9936/2)+(str9936%2)+5-2;
byte[] bt=null;
List list=new ArrayList();
StringBuilder password=new StringBuilder();
try {
//转为byte数组
bt = String.valueOf(result).getBytes(“gbk”);
//第二次算法
for (int i = 0; i < bt.length; i++) {
list.add(bt[i]*3);
}
//移位加密//反转
Collections.reverse(list);
//遍历集合拼接成字符串
for (int i = 0; i < list.size(); i++) {
char c=(char)(int)(Math.random()*26+97);
password.append(list.get(i)).append©;
}
System.out.println(“加密后密码”+password.toString());
//调用解密
decode(password.toString());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
//解密
public static void decode(String str){
List list=new ArrayList();
Pattern pa=Pattern.compile("\d+");
Matcher ma=pa.matcher(str);
while(ma.find()){
list.add(Integer.valueOf(ma.group(0)));
}
//反转
Collections.reverse(list);
byte[] bt=new byte[list.size()];
for (int i = 0; i < list.size(); i++) {
//一次解密
bt[i]=(byte) (list.get(i)/3);
}
String str2=new String(bt,Charset.forName(“gbk”));
//二次解密
long count=(Long.valueOf(str2)-5L+2+(Long.valueOf(str2)9936%2))2/(9936);
System.out.println(“解密后密码”+count);
}
}
效果图:
撰写日期:2019-7-12
作者:拍氹