java數字加密演算法
阿新 • • 發佈:2018-12-14
數字加密在專案中時常會遇到,如手機號,身份證號資訊等,下面小白將自己手寫的數字加密演算法分享給大家,可在專案中直接運用.
加密規則,入參時傳遞一個欄位時間戳 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
總結:此加密演算法雖然簡單,但加密思想和加密原理值得學習,可以隨意修改數字祕鑰、轉換字元、混淆字母、總加密字串長度,並用於專案中。