package com.cico.nest.lcdp.boot.core.system.util;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.SecureRandom;
import java.util.Base64;

/**
 * DES加密，加密模式ECB，填充模式 pkcs7padding
 * @author qianf
 * @date 2023/9/11 11:32
 */
public class CryptoUtil {
    /**
     * 加密
     *
     * @param datasource byte[]
     * @param password   String
     * @return byte[]
     */
    public static byte[] encrypt(byte[] datasource, String password) {
        try {
            SecureRandom random = new SecureRandom();
            DESKeySpec desKey = new DESKeySpec(password.getBytes());
            //创建一个密匙工厂，然后用它把DESKeySpec转换成
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKey);
            //Cipher对象实际完成加密操作
            Cipher cipher = Cipher.getInstance("DES");
            //用密匙初始化Cipher对象
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, random);
            //现在，获取数据并加密
            //正式执行加密操作
            return cipher.doFinal(datasource);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 解密
     *
     * @param src      byte[]
     * @param password String
     * @return byte[]
     * @throws Exception
     */
    public static byte[] decrypt(byte[] src, String password) throws Exception {
        // DES算法要求有一个可信任的随机数源
        SecureRandom random = new SecureRandom();
        // 创建一个DESKeySpec对象
        DESKeySpec desKey = new DESKeySpec(password.getBytes());
        // 创建一个密匙工厂
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        // 将DESKeySpec对象转换成SecretKey对象
        SecretKey secretKey = keyFactory.generateSecret(desKey);
        // Cipher对象实际完成解密操作 默认DES/ECB/pkcs7padding
        Cipher cipher = Cipher.getInstance("DES");
        // 用密匙初始化Cipher对象
        cipher.init(Cipher.DECRYPT_MODE, secretKey, random);
        // 真正开始解密操作
        return cipher.doFinal(src);
    }

    /**
     * 解密
     *
     * @param src      已加密数据,经Base64编码的数据
     * @param password 解密密码
     * @return 解密后字符串
     * @throws Exception
     */
    public static String decrypt(String src, String password) throws Exception {
        byte[] base64Decodes = Base64.getDecoder().decode(src);
        byte[] result = decrypt(base64Decodes, password);
        return new String(result);
    }

    /**
     * 加密
     *
     * @param src      待加密数据
     * @param password 加密密码
     * @return 加密并进行Base64编码后数据
     */
    public static String encrypt(String src, String password) {
        byte[] srcBytes = src.getBytes();
        byte[] result = encrypt(srcBytes, password);
        return Base64.getEncoder().encodeToString(result);
    }

    public static void main(String[] args) throws Exception {
        System.out.println(CryptoUtil.encrypt("18268508492", "4werwero2396i678;"));
        System.out.println(CryptoUtil.encrypt("r*3s3KnDB9fK", "4werwero2396i678;"));

        System.out.println(CryptoUtil.decrypt("a2rEqlIUDxrpYFDyHq9a3g==", "4werwero2396i678;"));
        System.out.println(CryptoUtil.decrypt("qOovY991Qb4", "4werwero2396i678;"));

    }
}
