package tw.com.altob.goodpk.Util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import android.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class CryptionUtil {

    //AES解密
    public static String aesDecryptBase64(String carNo, String CryptoKey){

        byte[] key = new byte[32];
        byte[] iv = new byte[16];
        String decKey = "";
        String decString = "";

        decKey = sha256encrypt(CryptoKey).substring(0, 10);
        byte[] sha = sha384encrypt(decKey);

        System.arraycopy(sha, 0, key, 0, 32);
        System.arraycopy(sha, 32, iv, 0, 16);

        try {
            decString = decrypt(carNo, key, iv);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return decString;
    }

    //AES加密
    public static String aesEncryptBase64(String carNo, String CryptoKey){

        byte[] key = new byte[32];
        byte[] iv = new byte[16];
        String encKey = "";
        String encString = "";

        encKey = sha256encrypt(CryptoKey).substring(0, 10);
        byte[] sha = sha384encrypt(encKey);

        System.arraycopy(sha, 0, key, 0, 32);
        System.arraycopy(sha, 32, iv, 0, 16);

        try {
            encString = encrypt(carNo, key, iv);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return encString;
    }

    //SHA384加密
    private static byte[] sha384encrypt(String input){

        MessageDigest objSHA = null;

        try {
            objSHA = MessageDigest.getInstance("SHA-384");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        byte[] bytSHA = objSHA.digest(input.getBytes());

        return bytSHA;
    }

    //SHA256加密
    public static String sha256encrypt(String input){

        MessageDigest md = null;
        String strDes = null;
        String encName = null;

        byte[] bt = input.getBytes();
        try {
            encName = "SHA-256";
            md = MessageDigest.getInstance(encName);
            md.update(bt);
            strDes = bytes2Hex(md.digest()); // to HexString

        } catch (NoSuchAlgorithmException e) {
            return null;
        }
        return strDes;
    }

    private static String bytes2Hex(byte[] bts) {
        String des = "";
        String tmp = null;
        for (int i = 0; i < bts.length; i++) {
            tmp = (Integer.toHexString(bts[i] & 0xFF));
            if (tmp.length() == 1) {
                des += "0";
            }
            des += tmp;
        }
        return des;
    }

    private static String decrypt(String sSrc, byte[] key, byte[] ivs) throws Exception {
        try {

            byte[] raw = key;

            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

            IvParameterSpec iv = new IvParameterSpec(ivs);

            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

            byte[] encrypted1 = Base64.decode(sSrc, Base64.DEFAULT);// 先用base64解密
            byte[] original = cipher.doFinal(encrypted1);

            String originalString = new String(original, "utf-8");

            return originalString;
        } catch (Exception ex) {
            return null;
        }
    }

    private static String encrypt(String sSrc, byte[] key, byte[] ivs) throws Exception {

        SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

        IvParameterSpec iv = new IvParameterSpec(ivs);

        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

        byte[] p = sSrc.getBytes("UTF-8");
        byte[] result = cipher.doFinal(p);

        String encoded = new String(Base64.encode(result, Base64.DEFAULT), "utf-8");

        return encoded;
    }
}
