package com.yuchen.saas.park.service.util;


import cn.hutool.core.codec.Base64;

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

public class EncryptUntil {

    public static final String password = "xlinkiot";

    public static final String message = "1234567891234567890";  //原数据，不加时间去掉下划线和去掉下面的startTime，加了时间有效为当前时间的30分钟，不加时间，有效期以下发的时间为准


    /* public static void main(String[] args) throws Exception {

        String startTime = System.currentTimeMillis() / 1000+"";

        String encode = Base64.encode(encrypt((message  ).getBytes("utf-8"),password));

        System.out.printf("加密数据:" + encode);

        System.out.printf("\n");


        byte[] bytes = Base64.decode(encode);

        byte[] abc = decrypt(bytes,password);

        System.out.printf("解密数据:" + new String(abc));
    } */


    /**
     * 加密
     * @param datasource byte[]
     * @return byte[]
     */
    public static  byte[] encrypt(byte[] datasource) {
        try{
            SecureRandom random = new SecureRandom();
            DESKeySpec desKey = new DESKeySpec(password.getBytes());
            //创建一个密匙工厂，然后用它把DESKeySpec转换成
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey securekey = keyFactory.generateSecret(desKey);
            //Cipher对象实际完成加密操作
            Cipher cipher = Cipher.getInstance("DES");
            //用密匙初始化Cipher对象,ENCRYPT_MODE用于将 Cipher 初始化为加密模式的常量
            cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
            //现在，获取数据并加密
            //正式执行加密操作
            return cipher.doFinal(datasource); //按单部分操作加密或解密数据，或者结束一个多部分操作
        }catch(Throwable e){
            e.printStackTrace();
        }
        return null;
    }


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

}
