数字加密在项目中时常会遇到,如手机号,身份证号信息等,下面小白将自己手写的数字加密算法分享给大家,可在项目中直接运用.
加密规则,入参时传递一个字段时间戳 time:
* 1.以字母代替数字,0-9分别为["D","e","C","A","#","b","J","I","z","M"]
* 2.混淆字母为FxYNgq;
* 3.加密字符串等于初始数字加时间戳乘以数字密钥(5658116)再把数字结果转换成字母;
* 4.对于加密字符串长度小于总加密字符串长度(32)的随机插入混淆字母
例:(15755141030 + 1544769549545)*5658116=8829629720137456700-->再转换成字母 qFzzYFCMJYqCMgICDNxFeAqI#bJgIFDD;
package com.example.demo.t2; import java.util.regex.Pattern; /** * Created by ypf on 2018/12/14. */ public class Test { //数字秘钥 private final static long SECRET_KEY = 5658116; //转换字符(0-9分别为["D","e","C","A","#","b","J","I","z","M"] private final static String CONVERT_KEY = "DeCA#bJIzM"; //混淆字母 private final static String CONFUSED_WORDS_KEY = "FxYNgq"; //总加密字符串长度 private final static int LEN_KEY = 32; /** * 数字加密算法 **/ public String encrypt(String str,long time){ //数字校验 if(!isNumber(str)){ System.out.println(str + "不是数字"); return null; } long number = Long.parseLong(str); long newNumber = (number + time) * SECRET_KEY; String[] numArr = String.valueOf(newNumber).split(""); String[] initArr = CONVERT_KEY.split(""); int len = numArr.length; StringBuffer buffer = new StringBuffer(); //数字转字母 for(int i = 0; i < len; i++){ int inx = Integer.parseInt(numArr[i]); buffer.append(initArr[inx]); } //随机加入混淆字符 String[] cwkArr = CONFUSED_WORDS_KEY.split(""); if(len < LEN_KEY){ int l = LEN_KEY - len; for(int i = 0; i < l; i++){ int index = (int)(Math.random()*buffer.length()); int inx = (int)(Math.random()*(CONFUSED_WORDS_KEY.length())); buffer.insert(index,cwkArr[inx]); } } String result = buffer.toString(); System.out.println("加密字符串:" + result); return result; } /** * 解密算法 * */ public String decrypt(String str,long time){ if(null == str || "".equals(str)){ System.out.println("参数为空"); return null; } int l = CONFUSED_WORDS_KEY.length(); String[] cwkArr = CONFUSED_WORDS_KEY.split(""); for(int i = 0; i < l; i++){ str = str.replaceAll(cwkArr[i],""); } String[] initArr = str.split(""); int len = initArr.length; StringBuffer result = new StringBuffer(); for(int i = 0; i < len; i++ ){ int k = CONVERT_KEY.indexOf(initArr[i]); if(k == -1){ System.out.println("转化失败:" + str); return null; } result.append(k); } Long number; try { long total = Long.parseLong(result.toString()); long sum = total/SECRET_KEY; number = sum - time; System.out.println("解密后数字:" + number); } catch (NumberFormatException e) { e.printStackTrace(); return null; } return number.toString(); } /** * 测试 **/ public static void main(String[] args) { long time = System.currentTimeMillis(); System.out.println("time:" + time); Test t = new Test(); String number = "15755141030"; System.out.println(number); String result = t.encrypt(number,time); t.decrypt(result,time); } /** * 数字校验 * */ public static boolean isNumber(String value) { String pattern = "^[0-9]*[1-9][0-9]*$"; boolean isMatch = Pattern.matches(pattern, value); return isMatch; } }
运行结果如下:
Connected to the target VM, address: '127.0.0.1:62962', transport: 'socket'
time:1544769549545
15755141030
加密字符串:qFzzYFCMJYqCMgICDNxFeAqI#bJgIFDD
解密后数字:15755141030
Disconnected from the target VM, address: '127.0.0.1:62962', transport: 'socket'
Process finished with exit code 0
总结:此加密算法虽然简单,但加密思想和加密原理值得学习,可以随意修改数字秘钥、转换字符、混淆字母、总加密字符串长度,并用于项目中。