package com.fdkankan.sign;


import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
import java.util.Random;
import java.util.UUID;

/**
 * AES 128bit 加密解密工具类，用于密码加密
 */
public class AesUtil {
    //使用AES-128-CBC加密模式，key需要为16位,key和iv可以相同！

    public static String key = "0000000563613487";

    public static String iv = "vyno4ve9shdq937e";
    /**
     * 加密方法
     * @param data  要加密的数据
     * @param key 加密key
     * @param iv 加密iv
     * @return 加密的结果
     * @throws Exception
     */
    public static String encrypt(String data, String key, String iv)  throws Exception{
        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");//"算法/模式/补码方式"
        int blockSize = cipher.getBlockSize();

        byte[] dataBytes = data.getBytes();
        int plaintextLength = dataBytes.length;
        if (plaintextLength % blockSize != 0) {
            plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
        }

        byte[] plaintext = new byte[plaintextLength];
        System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);

        SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
        IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

        cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
        byte[] encrypted = cipher.doFinal(plaintext);	// 加密

        return Base64.getEncoder().encodeToString(encrypted); 	//通过Base64转码返回
    }

    public static String encrypt(String data) throws Exception{
        return encrypt(data,key,iv);
    }

        /**
         * 解密方法
         * @param data 要解密的数据
         * @param key  解密key
         * @param iv 解密iv
         * @return 解密的结果
         * @throws Exception
         */
    public static String desEncrypt(String data, String key, String iv)  throws Exception{
        byte[] encrypted1 = Base64.getDecoder().decode(data);

        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
        SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
        IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

        cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec); //使用密钥初始化，设置为解密模式

        byte[] original = cipher.doFinal(encrypted1);	//执行操作
        return new String(original).trim();
    }

    public static String desEncrypt(String data) throws Exception{
        return desEncrypt(data,key,iv);
    }

    /**
     *   用于生成一组16位随机数 key
     * @return
     */
    public static String getRandomStringKey() {
        int hashCodeValue = UUID.randomUUID().hashCode();
        if(hashCodeValue < 0) hashCodeValue = -hashCodeValue;
        return String.format("%016d",hashCodeValue);//左边补0,16位，进制（d,x）
    }

    /**
     *   用于生成16位的随机数 iv
     * @return
     */
    public static String getRandomStringIv(){
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random=new Random();
        StringBuffer key = new StringBuffer();
        for(int i=0;i<16;i++){
            int keyNumber = random.nextInt(base.length());
            key.append(base.charAt(keyNumber));
        }
        return key.toString();
    }


    /**
     * 测试
     */
    public static void main(String args[]) throws Exception {
        String data = "Aa111111";
//        String key = getRandomStringKey();  //0000001210830863
//        String iv = getRandomStringIv();    //1xo1ub7m2rsz92ev

        System.out.println(key);
        System.out.println(iv);
        String enData = encrypt(data, key, iv);

        System.out.println(enData);
        System.out.println(desEncrypt("vvEsbkugGPaFjGJeZ6MHK2joojv0juIMeUoa\\/oJMUi8=", key, iv));
    }
}

