package com.xhpc.evcs.encryption;

import com.xhpc.evcs.cyc.dto.CYCChargeOrderInfoResponse;
import com.xhpc.evcs.domain.AuthSecretToken;
import com.xhpc.evcs.dto.ChargeOrderInfoResponse;
import com.xhpc.evcs.dto.DTOJsonHelper;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;
import java.util.Optional;

import static com.xhpc.evcs.domain.AuthSecretToken.SECRET_TOKEN_TYPE_OUT;

public class Aes128Cbc {

    private static int AES_128 = 128;

    public static String encrypt(String msg, String dataSecret, String dataSecretIV) throws NoSuchAlgorithmException,
            IllegalBlockSizeException, InvalidKeyException, BadPaddingException,
            InvalidAlgorithmParameterException, NoSuchPaddingException {

        KeyGenerator keyGenerator = KeyGenerator.getInstance(CryptoMngr.ALGORITHM);
        keyGenerator.init(AES_128);
        byte[] ds = (dataSecret).getBytes(StandardCharsets.UTF_8);
        ds = Arrays.copyOf(ds, 16);
        SecretKey key = new SecretKeySpec(ds, "AES");
        byte[] dsiv = (dataSecretIV).getBytes(StandardCharsets.UTF_8);
        dsiv = Arrays.copyOf(dsiv, 16);
        SecretKey IV = new SecretKeySpec(dsiv, "AES");
        byte[] cipherText = CryptoMngr.encrypt(key.getEncoded(), IV.getEncoded(), msg.getBytes());
        return Base64.getEncoder().encodeToString(cipherText);
    }

    public static byte[] decryptBytes(String encryptedMsg, String dataSecret, String dataSecretIV) throws NoSuchAlgorithmException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, InvalidAlgorithmParameterException, NoSuchPaddingException {

        if (encryptedMsg.startsWith("{") || encryptedMsg.startsWith("[")) {
            throw new IllegalArgumentException("不是合法的加密字符串");
        }
        KeyGenerator keyGenerator = KeyGenerator.getInstance(CryptoMngr.ALGORITHM);
        keyGenerator.init(AES_128);
        byte[] ds = (dataSecret).getBytes(StandardCharsets.UTF_8);
        ds = Arrays.copyOf(ds, 16);
        SecretKey key = new SecretKeySpec(ds, "AES");
        byte[] dsiv = (dataSecretIV).getBytes(StandardCharsets.UTF_8);
        dsiv = Arrays.copyOf(dsiv, 16);
        SecretKey IV = new SecretKeySpec(dsiv, "AES");
        byte[] cipherText = Base64.getDecoder().decode(encryptedMsg.getBytes());
        byte[] decryptedBytes = CryptoMngr.decrypt(key.getEncoded(), IV.getEncoded(), cipherText);
        return decryptedBytes;
    }

    public static String decryptString(String encryptedMsg, String dataSecret, String dataSecretIV) throws NoSuchAlgorithmException,
            IllegalBlockSizeException, InvalidKeyException, BadPaddingException,
            InvalidAlgorithmParameterException, NoSuchPaddingException {

        if (encryptedMsg == null || encryptedMsg.length() == 0) return "";
        byte[] decryptedBytes = decryptBytes(encryptedMsg, dataSecret, dataSecretIV);
        return new String(decryptedBytes);
    }

    public static void main(String[] args) throws BadPaddingException, InvalidAlgorithmParameterException,
            NoSuchAlgorithmException, IllegalBlockSizeException, UnsupportedEncodingException, NoSuchPaddingException,
            InvalidKeyException {
        try{
            String date="XvsjHvtpkhS6dBDn/N1gkCPlUOmBIFhxyrYrd+E1nuEmk+jkHbLEYmiKSPpLzxmtvz+ZRXY/N6072RqMlxH9bHX9T6JtbMjmFNP8RvwDFiwK2jW638CIJ2gp+agr7gYKZ6J5fsxCqj8+TGQTYuTdTGhUTy1X9Y7yuv/ptWR2nfh2uPuZYoDyO03umvMSwDS3";
            String dataSecret="EOAVn9uGuUuLzfCq";
            String dataSecretIV="ruExy9reF1W1YVMj";
            String s = decryptString(date, dataSecret, dataSecretIV);
            System.out.println("=======ss======="+s);
        }catch (Exception e){
            e.printStackTrace();
        }



    }
}
