package com.pj.utils;


import org.bouncycastle.jce.interfaces.ECPublicKey;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;
import java.security.Security;

//报文验签&解密
public class CryptoUtil {
    //private final static String sm4Key="56b5f803c4eeaf9bc9322b46c95b8145";//测试
    private final static String sm4Key="0c1a54cf431473b8f11d74da7e1fa504";//生产

    private static Logger logger = LoggerFactory.getLogger(CryptoUtil.class);
    static{
        Security.addProvider(new BouncyCastleProvider());
    }

    public static String getDecrypt(String m,String sm4Key,String sm2Key) throws Exception{
        logger.info("原文: {}", m);
        Encoder encoder = Encoder.Hex;
        byte[] mtextByte = Encoder.Hex.decode(m);
        // 签名
        int siglen = (int) mtextByte[0];
        byte[] signature = new byte[siglen];
        System.arraycopy(mtextByte, 1, signature, 0, siglen);
        //内容密文
        byte[] mcontent = new byte[mtextByte.length - siglen - 1];
        System.arraycopy(mtextByte, 1 + siglen, mcontent, 0, mtextByte.length - siglen - 1);

        //SM4解密密文
        SM4 SM4_key = SM4.recovery(sm4Key);
        // SM4_key.getEncodedKeyStr(); // 交换编码密钥字符串 . Base64/Hex
        byte[] keyBytes = encoder.decode(sm4Key); // 从交换编码密钥字符串还原为密钥字节码
        byte[] plainBytesDecrypted = SM4.decrypt(keyBytes, mcontent, SM4_key.getTransformation()); // 解密后字节码
        logger.info("解密出来的 明文:{}", new String(plainBytesDecrypted, "UTF-8"));

        // SM2验签
        ECPublicKey pub = SM2.recoveryPubKeyFromQ(encoder.decode(sm2Key));
        logger.info("SM2 - 验签结果 - {}", SM2.verify(pub, null, plainBytesDecrypted, signature));
        if(!SM2.verify(pub, null, plainBytesDecrypted, signature)){
            throw new Exception("验签未通过！！");
        }
        return new String(plainBytesDecrypted, "UTF-8");
    }

    public static String encrypt(String m) {
        return encrypt(m,sm4Key);
    }

    public static String decrypt(String m) {
        return decrypt(m,sm4Key);
    }

    public static String encrypt(String m,String sm4Key) {
        logger.info("原文: {}", m);
        Encoder encoder = Encoder.Hex;
        SM4 SM4_key = SM4.recovery(sm4Key);
        byte[] keyBytes = encoder.decode(sm4Key); // 从交换编码密钥字符串还原为密钥字节码
        byte[] plainBytesEncrypted = SM4.encrypt(keyBytes, m.getBytes(StandardCharsets.UTF_8), SM4_key.getTransformation());
        String plainEncrypted = encoder.encode(plainBytesEncrypted);
        logger.info("加密出来的 密文:{}", plainEncrypted);
        return plainEncrypted;
    }

    public static String decrypt(String m,String sm4Key) {
        logger.debug("原文: {}", m);
        Encoder encoder = Encoder.Hex;
        byte[] mtextByte = Encoder.Hex.decode(m);
        SM4 SM4_key = SM4.recovery(sm4Key);
        byte[] keyBytes = encoder.decode(sm4Key); // 从交换编码密钥字符串还原为密钥字节码
        byte[] plainBytesDecrypted = SM4.decrypt(keyBytes, mtextByte, SM4_key.getTransformation()); // 解密后字节码
        logger.debug("解密出来的 明文:{}", new String(plainBytesDecrypted, StandardCharsets.UTF_8));
        return new String(plainBytesDecrypted, StandardCharsets.UTF_8);
    }


    public static void main(String[] args) {
        String srt="10647709f1b40467917932891d4360bb4759d7b45a931e416fdefc176ece24b941ddd67999dde1e03377cf8ab903abe8cda5e57d8d0f82892a0717fb14faa0fd3604ba07c632ff8c70a9cdf821ff82732122cf2dba8b3e6587cab29ac8fbbe80616fa56a6ce27c92b075ed970523842f12455e488452a82bd15a3693d5643554ec9609459d2ea0ab97145630795b0f333e66d8ae8cb564d57c6272e370fa96de6ef24b01c22063b3f2b60358b14353d363c5e5b77db5f57b20823afedd23a7bbcfaa76e56f730f91c6882d29f3d3a5301b87af39d16712dbf8dcac0aa03d74a485ab22978a6b39ab75bb825a2b3965ff927ff8f82bc9f465c9592f74bd0c14f92e";
        System.out.println(decrypt(srt));

    }
}
