package com.iformall.utils;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.Security;

public class CipherUtil {

    /**
     * 密钥算法
     */
    private static final String ALGORITHM = "AES";
    /**
     * 加解密算法/工作模式/填充方式
     */
    private static final String ALGORITHM_MODE_PADDING = "AES/ECB/PKCS7Padding";

    static {
        try {
            Security.addProvider(new BouncyCastleProvider());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * AES解密
     */
    public static String decryptData(byte[] b, String serectKey) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
        SecretKeySpec key = new SecretKeySpec(HashUtil.md5(serectKey).toLowerCase().getBytes(), ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, key);
        return new String(cipher.doFinal(b));
    }

    /**
     * decryptReqInfo
     * @param req_info
     * @param key
     * @return
     * @throws Exception
     */
    public static String decryptReqInfo(String req_info, String key) throws Exception {
        byte[] b = Base64Util.decode(req_info);
        String B = decryptData(b, key);
        return B;
    }



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

        String A = "Ndaz8Fj4VUkxnD9iQ2ghD8yRgG8M9gBa0qIP4/GebJtzFWswbquDH/+kX3ZGB6zVb//4ZLIxMC4IZ0hzhsNc8JsDGbPKSYlaBI1prdbXr8YMzqAAHg+UJKNPyibJAV0o+soGNYeVrzVhzjWk0oFzCk2rvAtLlnq6RKjwTLOlIUxWXJMvtsCtpXKvcWMxRx+8pn42Barx6B2D0lM+39Vmi5Mu6XXHi9xUO+KPF7sgfv4hz/2NBBBfsR9Df0CxfQrEKmjxWgrl4Cn1g8JvBhZJiqUsP0oxS+xBZVGRLRY66oDNaLlNtzmwsrkEnDc1hWXgy3AymB0oLRz5SAOry0vKrzqv/yw1pWzyltAZ/M7ge6iD7gP/ILcxty5HjOZE22p01J6MqW8UAiTizrURucC/e3f9N935qs3DR8oqDO2Di4KR1pZnVQcxStnrlnq5YkaZRYXAdp3od80zCaNnN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