package com.imdroid.common.util;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.nio.charset.StandardCharsets;
import java.security.Key;

public class Des3Utils {
    private static final String KEY_ALGORITHM = "DESede";
    private static final String CIPHER_ALGORITHM_CBC = "DESede/CBC/PKCS5Padding";
    private static final String CIPHER_ALGORITHM_ECB = "DESede/ECB/PKCS5Padding";


    static String mode = "DESede/CFB/NoPadding";

    public static byte[] encrypt(Key deskey, IvParameterSpec desiv, String mode, byte[] data) {
        byte[] res = null;

        Cipher cipher;
        try {
            cipher = Cipher.getInstance(mode);
            cipher.init(Cipher.ENCRYPT_MODE, deskey, desiv);
            res = cipher.doFinal(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    public static String encrypt(String data, byte[] key, byte[] iv) {
        String res = "";

        ByteBuf buf = Unpooled.buffer(256);
        byte[] databyte = data.getBytes(StandardCharsets.UTF_8);
        buf.writeBytes(databyte);
        int a = (8 - databyte.length % 8);
        for (int c = 0; c < a; c++) {
            buf.writeByte(0x00);
        }
        databyte = new byte[buf.readableBytes()];
        buf.readBytes(databyte);

        DESedeKeySpec spec;
        SecretKeyFactory keyfactory;
        Key deskey;
        try {
            keyfactory = SecretKeyFactory.getInstance("DESede");
            spec = new DESedeKeySpec(key);
            deskey = keyfactory.generateSecret(spec);
            IvParameterSpec desiv = new IvParameterSpec(iv);

            byte[] encryptbyte = encrypt(deskey, desiv, mode, databyte);
            res = Base64.encodeBase64String(encryptbyte);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return res;

    }

    public static String encryptCBC(byte[] key, byte[] iv, String data) throws Exception {
        DESedeKeySpec spec = new DESedeKeySpec(key);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
        Key desKey = keyFactory.generateSecret(spec);
        Cipher cipher = Cipher.getInstance(mode);
        IvParameterSpec ips = new IvParameterSpec(iv);
        cipher.init(Cipher.ENCRYPT_MODE, desKey, ips);
        byte[] encryptedData = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
        return Base64.encodeBase64String(encryptedData);
    }

    public static String decryptCBC(byte[] key, byte[] iv, String data) throws Exception {
        DESedeKeySpec spec = new DESedeKeySpec(key);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
        Key desKey = keyFactory.generateSecret(spec);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC);
        IvParameterSpec ips = new IvParameterSpec(iv);
        cipher.init(Cipher.DECRYPT_MODE, desKey, ips);
        byte[] decryptedData = cipher.doFinal(Base64.decodeBase64(data));
        return new String(decryptedData, StandardCharsets.UTF_8);
    }

    public static String encryptECB(byte[] key, String data) throws Exception {
        DESedeKeySpec spec = new DESedeKeySpec(key);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
        Key desKey = keyFactory.generateSecret(spec);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_ECB);
        cipher.init(Cipher.ENCRYPT_MODE, desKey);
        byte[] encryptedData = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
        return Base64.encodeBase64String(encryptedData);
    }

    public static String decryptECB(byte[] key, String data) throws Exception {
        DESedeKeySpec spec = new DESedeKeySpec(key);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
        Key desKey = keyFactory.generateSecret(spec);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_ECB);
        cipher.init(Cipher.DECRYPT_MODE, desKey);
        byte[] decryptedData = cipher.doFinal(Base64.decodeBase64(data));
        return new String(decryptedData, StandardCharsets.UTF_8);
    }
}
