Browse Source

加解密方法类

gaodm 5 years ago
parent
commit
2f4941d3d5
1 changed files with 222 additions and 0 deletions
  1. 222 0
      prec-service/src/main/java/com/diagbot/util/AESCipher.java

+ 222 - 0
prec-service/src/main/java/com/diagbot/util/AESCipher.java

@@ -0,0 +1,222 @@
+package com.diagbot.util;
+
+import sun.misc.BASE64Decoder;
+
+import javax.crypto.BadPaddingException;
+import javax.crypto.Cipher;
+import javax.crypto.IllegalBlockSizeException;
+import javax.crypto.NoSuchPaddingException;
+import javax.crypto.spec.IvParameterSpec;
+import javax.crypto.spec.SecretKeySpec;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.security.InvalidAlgorithmParameterException;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+import java.text.ParseException;
+
+/**
+ * AES加密(指定一个初始化向量:IV_STRING,的加密方式)
+ *
+ * @author Administrator
+ */
+public class AESCipher {
+
+    private static final String IV_STRING = "z76rxggpnykxeyb1";
+
+    /**
+     * 加密
+     *
+     * @param content 加密内容
+     * @param key     加密秘钥
+     * @return
+     */
+    public static String aesEncryptString(String content, String key)
+            throws InvalidKeyException, NoSuchAlgorithmException,
+            NoSuchPaddingException, InvalidAlgorithmParameterException,
+            IllegalBlockSizeException, BadPaddingException,
+            UnsupportedEncodingException {
+        byte[] contentBytes = content.getBytes("UTF-8");
+        byte[] keyBytes = key.getBytes("UTF-8");
+        //加密,得到byte数组
+        byte[] encryptedBytes = aesEncryptBytes(contentBytes, keyBytes);
+        //将加密结果byte数组转换成16进制字符串
+        String hex = byte2HexStr(encryptedBytes);
+
+        return hex;
+    }
+
+    /**
+     * 解密
+     *
+     * @param content 解密内容
+     * @param key     解密秘钥
+     * @return
+     */
+    public static String aesDecryptString(String content, String key)
+            throws Exception {
+        byte[] decryptFrom = parseHexStr2Byte(content);
+        byte[] keyBytes = key.getBytes("UTF-8");
+        //解密,得到byte数组
+        byte[] decryptedBytes = aesDecryptBytes(decryptFrom, keyBytes);
+        //将解密结果byte数组转换成字符串
+        return new String(decryptedBytes, "UTF-8");
+    }
+
+    /**
+     * 将byte数组转换成16进制字符串
+     *
+     * @param b
+     * @return
+     */
+    public static String byte2HexStr(byte[] b) {
+        String stmp = "";
+        StringBuilder sb = new StringBuilder("");
+        for (int n = 0; n < b.length; n++) {
+            stmp = Integer.toHexString(b[n] & 0xFF);
+            sb.append(stmp.length() == 1 ? "0" + stmp : stmp);
+        }
+        return sb.toString().toUpperCase().trim();
+    }
+
+    public static String ebotongDecrypto(String str) {
+        BASE64Decoder base64decoder = new BASE64Decoder();
+        try {
+            byte[] encodeByte = base64decoder.decodeBuffer(str);
+            return new String(encodeByte);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return str;
+    }
+
+    /**
+     * 将16进制字符串转换成byte数组
+     *
+     * @param hexStr
+     * @return
+     */
+    public static byte[] parseHexStr2Byte(String hexStr) {
+        if (hexStr.length() < 1) {
+            return null;
+        }
+        byte[] result = new byte[hexStr.length() / 2];
+        for (int i = 0; i < hexStr.length() / 2; i++) {
+            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
+            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),
+                    16);
+            result[i] = ((byte) (high * 16 + low));
+        }
+        return result;
+    }
+
+    /**
+     * 加密,得到byte数组
+     *
+     * @param contentBytes 加密内容的byte数组
+     * @param keyBytes     加密秘钥的byte数组
+     * @return
+     */
+    public static byte[] aesEncryptBytes(byte[] contentBytes, byte[] keyBytes)
+            throws NoSuchAlgorithmException, NoSuchPaddingException,
+            InvalidKeyException, InvalidAlgorithmParameterException,
+            IllegalBlockSizeException, BadPaddingException,
+            UnsupportedEncodingException {
+        return cipherOperation(contentBytes, keyBytes, Cipher.ENCRYPT_MODE);
+    }
+
+    /**
+     * 解密,得到byte数组
+     *
+     * @param contentBytes 解密内容的byte数组
+     * @param keyBytes     解密秘钥的byte数组
+     * @return
+     */
+    public static byte[] aesDecryptBytes(byte[] contentBytes, byte[] keyBytes)
+            throws NoSuchAlgorithmException, NoSuchPaddingException,
+            InvalidKeyException, InvalidAlgorithmParameterException,
+            IllegalBlockSizeException, BadPaddingException,
+            UnsupportedEncodingException {
+        return cipherOperation(contentBytes, keyBytes, Cipher.DECRYPT_MODE);
+    }
+
+    /**
+     * 加密/解密(指定一个初始化向量:IV_STRING)
+     *
+     * @param contentBytes 内容的byte数组
+     * @param keyBytes     秘钥的byte数组
+     * @param mode         加密:Cipher.ENCRYPT_MODE / 解密:Cipher.DECRYPT_MODE
+     * @return
+     */
+    private static byte[] cipherOperation(byte[] contentBytes, byte[] keyBytes,
+                                          int mode) throws UnsupportedEncodingException,
+            NoSuchAlgorithmException, NoSuchPaddingException,
+            InvalidKeyException, InvalidAlgorithmParameterException,
+            IllegalBlockSizeException, BadPaddingException {
+        SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");
+
+        byte[] initParam = IV_STRING.getBytes("UTF-8");
+        IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
+
+        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
+        cipher.init(mode, secretKey, ivParameterSpec);
+
+        return cipher.doFinal(contentBytes);
+    }
+
+    public static void main(String[] args) throws ParseException {
+        //        String time = "2018-03-29 13:09:43";
+        //        String serial = "000000ECC4BE0564";
+        //        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        //        Date date = formatter.parse(time);
+        //        long times = date.getTime();
+        //        String subSerial = serial.substring(serial.length() - 8, serial.length());
+        //        String stimes = String.valueOf(times);
+        //        String subtimes = stimes.substring(stimes.length() - 8, stimes.length());
+        //        String keys = subSerial + subtimes;
+        //        System.out.println("【subSerial】="+subSerial);
+        //        System.out.println("【stimes】="+stimes);
+        //        System.out.println("【times】="+times);
+        //        System.out.println("【subtimes】="+subtimes);
+        //        System.out.println("【keys】=" + keys);
+        //        String money = String.valueOf(100);
+        //        System.out.println("【money】=" + money);
+        //
+        //        String en = null;
+        //        try
+        //        {
+        //            en = aesEncryptString(money, keys);
+        //        }
+        //        catch (Exception e)
+        //        {
+        //            e.printStackTrace();
+        //        }
+        //        String de = null;
+        //        try
+        //        {
+        //            de = aesDecryptString(en, keys);
+        //        }
+        //        catch (Exception e)
+        //        {
+        //            e.printStackTrace();
+        //        }
+        //        System.out.println("加密前金额:"+de);
+        //        System.out.println("加密后金额:"+en);
+        String content = "test";
+        String en = "";
+        String de = "";
+        try {
+            en = aesEncryptString(content, IV_STRING);
+            System.out.println(en);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+        try {
+            de = aesDecryptString(en, IV_STRING);
+            System.out.println(de);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+}