双重预防项目-国泰新华二开定制版
kongzy
2024-08-14 135a322f44708b1187d37ea4b6859b5d82113e89
src/main/java/com/ruoyi/doublePrevention/utilsCJ/AesGcm256Util.java
@@ -1,149 +1,149 @@
package com.ruoyi.doublePrevention.utilsCJ;//package com.gkhy.safePlatform.doublePrevention.utilsCJ;
//
//import com.f2bpm.upload.interfaces.ConstantUpload;
//import org.bouncycastle.crypto.DataLengthException;
//import org.bouncycastle.crypto.InvalidCipherTextException;
//import org.bouncycastle.crypto.engines.AESFastEngine;
//import org.bouncycastle.crypto.modes.GCMBlockCipher;
//import org.bouncycastle.crypto.params.AEADParameters;
//import org.bouncycastle.crypto.params.KeyParameter;
//
//import java.io.UnsupportedEncodingException;
//import java.nio.charset.StandardCharsets;
//import java.security.SecureRandom;
//import java.util.Base64;
//
///**
// * AES-GCM-256 工具类
// *
// * @author :LX(长沙麓谷)
// * @date 创建时间: 2022/8/3 14:05
// */
//public class AesGcm256Util {
//
//    /**
//     * 随机数生成
//     */
//    private static final SecureRandom SECURE_RANDOM = new SecureRandom();
//
//    public static final int NONCE_BIT_SIZE = 128;
//    public static final int MAC_BIT_SIZE = 128;
//    public static final int KEY_BIT_SIZE = 256;
//
//
//    private AesGcm256Util(){}
//
//
//    /**
//     * 创建密钥
//     * @return 密钥
//     */
//    public static byte[] key() {
//        byte[] key = new byte[KEY_BIT_SIZE / 8];
//        SECURE_RANDOM.nextBytes(key);
//        return key;
//    }
//
//    /**
//     * 创建向量
//     * @return 向量
//     */
//    public static byte[] iv() {
//        byte[] iv = new byte[NONCE_BIT_SIZE / 8];
//        SECURE_RANDOM.nextBytes(iv);
//        return iv;
//    }
//
//    /**
//     * 编码
//     * @param hexStr 要编码的文本
//     * @return 字节数组
//     */
//    public static byte[] hexToByte(String hexStr) {
//        int len = hexStr.length();
//        byte[] data = new byte[len / 2];
//        for (int i = 0; i < len; i += 2) {
//            data[i / 2] = (byte) ((Character.digit(hexStr.charAt(i), 16) << 4)
//                    + Character.digit(hexStr.charAt(i + 1), 16));
//        }
//        return data;
//    }
//
//    /**
//     * 转换为十六进制
//     * @param data 字节数组
//     * @return 转换的十六进制数据
//     */
//    public static String toHex(byte[] data) {
//        StringBuilder ret = new StringBuilder();
//        for (byte datum : data) {
//            String hex = Integer.toHexString(datum & 0xFF);
//            if (hex.length() == 1) {
//                hex = '0' + hex;
//            }
//            ret.append(hex.toUpperCase());
//        }
//        return ret.toString();
//    }
//
//    /**
//     * 加密
//     * @param plainText 要加密的明文文本
//     * @param key 密钥
//     * @param iv 向量
//     * @return
//     */
//    public static String encrypt(String plainText, String key, String iv) throws UnsupportedEncodingException {
//        return encrypt(plainText, key.getBytes(ConstantUpload.Base.UTF-8), iv.getBytes(ConstantUpload.Base.UTF-8));
//    }
//
//    /**
//     * 加密
//     * @param plainText 要加密的明文文本
//     * @param key 密钥
//     * @param iv 向量
//     * @return
//     */
//    public static String encrypt(String plainText, byte[] key, byte[] iv) {
//        String sr;
//        try {
//            byte[] plainBytes = plainText.getBytes(StandardCharsets.UTF_8);
//            GCMBlockCipher cipher = new GCMBlockCipher(new AESFastEngine());
//            AEADParameters parameters = new AEADParameters(new KeyParameter(key), MAC_BIT_SIZE, iv, null);
//            cipher.init(true, parameters);
//            byte[] encryptedBytes = new byte[cipher.getOutputSize(plainBytes.length)];
//            int retLen = cipher.processBytes(plainBytes, 0, plainBytes.length, encryptedBytes, 0);
//            cipher.doFinal(encryptedBytes, retLen);
//            sr = Base64.getEncoder().encodeToString(encryptedBytes);
//        } catch (Exception ex) {
//            throw new RuntimeException("加密异常! Encryption exception:" + ex.getMessage());
//        }
//        return sr;
//    }
//
//    /**
//     * 解密
//     * @param encryptedText 已加密文本
//     * @param key 密钥
//     * @param iv 向量
//     * @return
//     */
//    public static String decrypt(String encryptedText, byte[] key, byte[] iv) {
//        String sr;
//        try {
//            byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText);
//            GCMBlockCipher cipher = new GCMBlockCipher(new AESFastEngine());
//            AEADParameters parameters = new AEADParameters(new KeyParameter(key), MAC_BIT_SIZE, iv, null);
//            cipher.init(false, parameters);
//            byte[] plainBytes = new byte[cipher.getOutputSize(encryptedBytes.length)];
//            int retLen = cipher.processBytes(encryptedBytes, 0, encryptedBytes.length, plainBytes, 0);
//            cipher.doFinal(plainBytes, retLen);
//            sr = new String(plainBytes, StandardCharsets.UTF_8);
//
//        } catch (IllegalArgumentException | IllegalStateException | DataLengthException | InvalidCipherTextException ex) {
//            throw new RuntimeException("解密异常! Decryption exception:" + ex.getMessage());
//        }
//        return sr;
//    }
//}
//
package com.ruoyi.doublePrevention.utilsCJ;//package com.gkhy.safePlatform.doublePrevention.utilsCJ;
//
//import com.f2bpm.upload.interfaces.ConstantUpload;
//import org.bouncycastle.crypto.DataLengthException;
//import org.bouncycastle.crypto.InvalidCipherTextException;
//import org.bouncycastle.crypto.engines.AESFastEngine;
//import org.bouncycastle.crypto.modes.GCMBlockCipher;
//import org.bouncycastle.crypto.params.AEADParameters;
//import org.bouncycastle.crypto.params.KeyParameter;
//
//import java.io.UnsupportedEncodingException;
//import java.nio.charset.StandardCharsets;
//import java.security.SecureRandom;
//import java.util.Base64;
//
///**
// * AES-GCM-256 工具类
// *
// * @author :LX(长沙麓谷)
// * @date 创建时间: 2022/8/3 14:05
// */
//public class AesGcm256Util {
//
//    /**
//     * 随机数生成
//     */
//    private static final SecureRandom SECURE_RANDOM = new SecureRandom();
//
//    public static final int NONCE_BIT_SIZE = 128;
//    public static final int MAC_BIT_SIZE = 128;
//    public static final int KEY_BIT_SIZE = 256;
//
//
//    private AesGcm256Util(){}
//
//
//    /**
//     * 创建密钥
//     * @return 密钥
//     */
//    public static byte[] key() {
//        byte[] key = new byte[KEY_BIT_SIZE / 8];
//        SECURE_RANDOM.nextBytes(key);
//        return key;
//    }
//
//    /**
//     * 创建向量
//     * @return 向量
//     */
//    public static byte[] iv() {
//        byte[] iv = new byte[NONCE_BIT_SIZE / 8];
//        SECURE_RANDOM.nextBytes(iv);
//        return iv;
//    }
//
//    /**
//     * 编码
//     * @param hexStr 要编码的文本
//     * @return 字节数组
//     */
//    public static byte[] hexToByte(String hexStr) {
//        int len = hexStr.length();
//        byte[] data = new byte[len / 2];
//        for (int i = 0; i < len; i += 2) {
//            data[i / 2] = (byte) ((Character.digit(hexStr.charAt(i), 16) << 4)
//                    + Character.digit(hexStr.charAt(i + 1), 16));
//        }
//        return data;
//    }
//
//    /**
//     * 转换为十六进制
//     * @param data 字节数组
//     * @return 转换的十六进制数据
//     */
//    public static String toHex(byte[] data) {
//        StringBuilder ret = new StringBuilder();
//        for (byte datum : data) {
//            String hex = Integer.toHexString(datum & 0xFF);
//            if (hex.length() == 1) {
//                hex = '0' + hex;
//            }
//            ret.append(hex.toUpperCase());
//        }
//        return ret.toString();
//    }
//
//    /**
//     * 加密
//     * @param plainText 要加密的明文文本
//     * @param key 密钥
//     * @param iv 向量
//     * @return
//     */
//    public static String encrypt(String plainText, String key, String iv) throws UnsupportedEncodingException {
//        return encrypt(plainText, key.getBytes(ConstantUpload.Base.UTF-8), iv.getBytes(ConstantUpload.Base.UTF-8));
//    }
//
//    /**
//     * 加密
//     * @param plainText 要加密的明文文本
//     * @param key 密钥
//     * @param iv 向量
//     * @return
//     */
//    public static String encrypt(String plainText, byte[] key, byte[] iv) {
//        String sr;
//        try {
//            byte[] plainBytes = plainText.getBytes(StandardCharsets.UTF_8);
//            GCMBlockCipher cipher = new GCMBlockCipher(new AESFastEngine());
//            AEADParameters parameters = new AEADParameters(new KeyParameter(key), MAC_BIT_SIZE, iv, null);
//            cipher.init(true, parameters);
//            byte[] encryptedBytes = new byte[cipher.getOutputSize(plainBytes.length)];
//            int retLen = cipher.processBytes(plainBytes, 0, plainBytes.length, encryptedBytes, 0);
//            cipher.doFinal(encryptedBytes, retLen);
//            sr = Base64.getEncoder().encodeToString(encryptedBytes);
//        } catch (Exception ex) {
//            throw new RuntimeException("加密异常! Encryption exception:" + ex.getMessage());
//        }
//        return sr;
//    }
//
//    /**
//     * 解密
//     * @param encryptedText 已加密文本
//     * @param key 密钥
//     * @param iv 向量
//     * @return
//     */
//    public static String decrypt(String encryptedText, byte[] key, byte[] iv) {
//        String sr;
//        try {
//            byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText);
//            GCMBlockCipher cipher = new GCMBlockCipher(new AESFastEngine());
//            AEADParameters parameters = new AEADParameters(new KeyParameter(key), MAC_BIT_SIZE, iv, null);
//            cipher.init(false, parameters);
//            byte[] plainBytes = new byte[cipher.getOutputSize(encryptedBytes.length)];
//            int retLen = cipher.processBytes(encryptedBytes, 0, encryptedBytes.length, plainBytes, 0);
//            cipher.doFinal(plainBytes, retLen);
//            sr = new String(plainBytes, StandardCharsets.UTF_8);
//
//        } catch (IllegalArgumentException | IllegalStateException | DataLengthException | InvalidCipherTextException ex) {
//            throw new RuntimeException("解密异常! Decryption exception:" + ex.getMessage());
//        }
//        return sr;
//    }
//}
//