package com.yihu.jw.healthCare.utils;

import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.springframework.util.StringUtils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class AES {
    public static final String KEY_ALGORITHM = "AES";
    public static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";

    public AES() {
    }

    public static String base64Encode(byte[] bytes) {
        return (new BASE64Encoder()).encode(bytes);
    }

    public static byte[] base64Decode(String base64Code) throws Exception {
        return StringUtils.isEmpty(base64Code) ? null : (new BASE64Decoder()).decodeBuffer(base64Code);
    }

    private static Key toKey(byte[] key) {
        SecretKey secretKey = new SecretKeySpec(key, "AES");
        return secretKey;
    }

    public static byte[] decrypt(byte[] data, String password) {
        try {
            Key k = toKey(initKey(password));
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(2, k);
            return cipher.doFinal(data);
        } catch (Throwable var4) {
            var4.printStackTrace();
            return null;
        }
    }

    public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {
        return aesDecrypt(encryptStr, decryptKey, "utf-8");
    }

    public static String aesDecrypt(String encryptStr, String decryptKey, String encode) throws Exception {
        return StringUtils.isEmpty(encryptStr) ? null : new String(decrypt(base64Decode(encryptStr), decryptKey), encode);
    }

    public static byte[] encrypt(byte[] data, String password) {
        try {
            Key k = toKey(initKey(password));
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(1, k);
            return cipher.doFinal(data);
        } catch (Throwable var4) {
            var4.printStackTrace();
            return null;
        }
    }

    public static String aesEncrypt(String content, String encryptKey) throws Exception {
        return aesEncrypt(content, encryptKey, "utf-8");
    }

    public static String aesEncrypt(String content, String encryptKey, String encode) throws Exception {
        return base64Encode(encrypt(content.getBytes(encode), encryptKey));
    }

    private static byte[] initKey(String password) throws NoSuchAlgorithmException {
        KeyGenerator kg = KeyGenerator.getInstance("AES");
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        random.setSeed(password.getBytes());
        kg.init(128, random);
        SecretKey secretKey = kg.generateKey();
        return secretKey.getEncoded();
    }

    public static void main(String[] args) {
        try {
            System.out.println(base64Encode(aesEncrypt("jjjk", "zephyr").getBytes()));
        } catch (Exception var2) {
            var2.printStackTrace();
        }

    }
}