|
|
@@ -0,0 +1,197 @@
|
|
|
+package com.zd.common.core.utils;
|
|
|
+
|
|
|
+import javax.crypto.Cipher;
|
|
|
+import javax.crypto.SecretKey;
|
|
|
+import javax.crypto.spec.SecretKeySpec;
|
|
|
+import java.util.Random;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>DES可逆加密</p>
|
|
|
+ *
|
|
|
+ * @author: linft
|
|
|
+ * @date: 2021/3/7
|
|
|
+ * @since:
|
|
|
+ */
|
|
|
+public class DESUtils {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 随意定一个私钥(长度必须为24位)
|
|
|
+ */
|
|
|
+ private static final String SECRET_KEY = "ABCDEFGHIJKLMN0123456789";
|
|
|
+
|
|
|
+ private static final String INSTANCE_STR = "DESede";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 加密
|
|
|
+ *
|
|
|
+ * @param inStr 需要加密的内容
|
|
|
+ * @return 加密后的数据
|
|
|
+ */
|
|
|
+
|
|
|
+ public static String encrypt(String inStr) {
|
|
|
+ SecretKey deskey = new SecretKeySpec(SECRET_KEY.getBytes(), INSTANCE_STR);
|
|
|
+ Cipher cipher;
|
|
|
+ String outStr = null;
|
|
|
+ try {
|
|
|
+ cipher = Cipher.getInstance(INSTANCE_STR);
|
|
|
+ cipher.init(Cipher.ENCRYPT_MODE, deskey);
|
|
|
+ outStr = byte2hex(cipher.doFinal(inStr.getBytes()));
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.err.println("3DES加密异常"+ e.getMessage());
|
|
|
+ }
|
|
|
+ return outStr;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解密
|
|
|
+ *
|
|
|
+ * @param inStr 需要解密的内容
|
|
|
+ * @return 解密后的数据
|
|
|
+ */
|
|
|
+ public static String decrypt(String inStr) {
|
|
|
+ SecretKey deskey = new SecretKeySpec(SECRET_KEY.getBytes(), INSTANCE_STR);
|
|
|
+ Cipher cipher;
|
|
|
+ String outStr = null;
|
|
|
+ try {
|
|
|
+ cipher = Cipher.getInstance(INSTANCE_STR);
|
|
|
+ cipher.init(Cipher.DECRYPT_MODE, deskey);
|
|
|
+ outStr = new String(cipher.doFinal(hex2byte(inStr)));
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.err.println("3DES解密异常"+e.getMessage());
|
|
|
+ }
|
|
|
+ return outStr;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 转化为16进制字符串方法
|
|
|
+ *
|
|
|
+ * @param digest 需要转换的字节组
|
|
|
+ * @return 转换后的字符串
|
|
|
+ */
|
|
|
+ private static String byte2hex(byte[] digest) {
|
|
|
+ StringBuffer hs = new StringBuffer();
|
|
|
+ String stmp = "";
|
|
|
+ for (int n = 0; n < digest.length; n++) {
|
|
|
+ stmp = Integer.toHexString(digest[n] & 0XFF);
|
|
|
+
|
|
|
+ if (stmp.length() == 1) {
|
|
|
+ hs.append("0" + stmp);
|
|
|
+ } else {
|
|
|
+ hs.append(stmp);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return hs.toString().toUpperCase();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 十六进转二进制
|
|
|
+ *
|
|
|
+ * @param hexStr 待转换16进制字符串
|
|
|
+ * @return 二进制字节组
|
|
|
+ */
|
|
|
+ public static byte[] hex2byte(String hexStr) {
|
|
|
+ if (hexStr == null)
|
|
|
+ return null;
|
|
|
+ hexStr = hexStr.trim();
|
|
|
+ int len = hexStr.length();
|
|
|
+ if (len == 0 || len % 2 == 1)
|
|
|
+ return null;
|
|
|
+ byte[] digest = new byte[len / 2];
|
|
|
+ try {
|
|
|
+ for (int i = 0; i < hexStr.length(); i += 2) {
|
|
|
+ digest[i / 2] = (byte) Integer.decode("0x" + hexStr.substring(i, i + 2)).intValue();
|
|
|
+ }
|
|
|
+ return digest;
|
|
|
+ } catch (Exception e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ String data = "05367892";
|
|
|
+ String key = DESUtils.encrypt(data);
|
|
|
+ System.out.println("加密内容="+key);
|
|
|
+ String res = DESUtils.decrypt(key);
|
|
|
+ System.out.println("解密内容="+res);
|
|
|
+ //随机生成八位字符串
|
|
|
+ getRandomNum();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String getRandomNum() {
|
|
|
+ String randomNum = getRandomPassword(8);
|
|
|
+ System.out.println(randomNum);
|
|
|
+ return randomNum;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回随机产生的8位数
|
|
|
+ */
|
|
|
+ public static String getRandomPassword(int len) {
|
|
|
+ String result = makeRandomPassword(len);
|
|
|
+ if (result.matches(".*[a-z]{1,}.*") && result.matches(".*[A-Z]{1,}.*") && result.matches(".*\\d{1,}.*") &&
|
|
|
+ result.matches(".*[~!@#$%^&*\\.?]{1,}.*")) {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ result = makeRandomPassword(len);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 产生8位随机数
|
|
|
+ *
|
|
|
+ * @param len 长度
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String makeRandomPassword(int len) {
|
|
|
+ char charr[] = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".toCharArray();
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ Random r = new Random();
|
|
|
+ for (int x = 0; x < len; ++x) {
|
|
|
+ sb.append(charr[r.nextInt(charr.length)]);
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 16进制转换10进制丢0,补全0
|
|
|
+ *
|
|
|
+ * @param cardNum 卡号
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String completeMissing(String cardNum) {
|
|
|
+ String[] userchar = cardNum.split("");
|
|
|
+ String placeholder = "";
|
|
|
+ for(int i=0;i<userchar.length;i++){
|
|
|
+ if(i==0){
|
|
|
+ if(userchar[i].equals("0")){
|
|
|
+ placeholder+="0";
|
|
|
+ }else{
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ if(userchar[i].equals("0")){
|
|
|
+ placeholder+="0";
|
|
|
+ }else{
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ long num = Long.parseLong(cardNum,16);
|
|
|
+
|
|
|
+ //部分卡16进制没有上报补0,所以这里先统一认为是10位卡处理
|
|
|
+ if((placeholder+num).length()<10){
|
|
|
+ String numstr = "";
|
|
|
+ for(long i=(placeholder+num).length();i<10;i++){
|
|
|
+ numstr += "0";
|
|
|
+ }
|
|
|
+ return numstr+num;
|
|
|
+ }
|
|
|
+
|
|
|
+ return placeholder+num;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|