package com.jiayue.ssi.service;

import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.BCUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.SM2;
import com.jiayue.ssi.constant.SecretKeyConstants;
import com.jiayue.ssi.util.SM2CryptUtils;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.asn1.gm.GMNamedCurves;
import org.bouncycastle.asn1.gm.GMObjectIdentifiers;
import org.bouncycastle.asn1.x9.X9ECParameters;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.engines.SM2Engine;
import org.bouncycastle.crypto.params.*;
import org.bouncycastle.crypto.signers.PlainDSAEncoding;
import org.bouncycastle.crypto.signers.SM2Signer;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.jce.spec.ECParameterSpec;
import org.bouncycastle.util.Strings;
import org.bouncycastle.util.encoders.Hex;

import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;

/**
*
*
* @author xsl
* @since 2023/08/08
*/
@Slf4j
public class BcSm2Util {
    public static void main(String[] args) {
        //需要加密的明文
//        String text = "测试";
//        String pv = "6155d63ee27cbeca07f3e40c4f8856f1be8119fcbda1aadc7e0e595e52bad7bd";
//        String pu = "041967638ca43d4577d8dba166bff4437fde944270101f398a95b846ec2f8177d09f8abc5d62b6cd2c7216274d7abe0c8e04b0bb691207a32dd2e12d6bd2798672";
//

//        String a = SM2CryptUtils.decrypt("8fe02b3e2bd96292691c899e1e3008ec076708a56c509ff8572a96f7da45f7d649f441d988479e14d6f0230520b1c02f6b2df95f49f4833d791757fc5a6aee6d46ed445c60856d8e625aa92fa64d09a7c3f22705d82f0a3bfe5e91b99ee013587fc6c4240c7269989e", "6155d63ee27cbeca07f3e40c4f8856f1be8119fcbda1aadc7e0e595e52bad7bd");
//        System.out.println(a);



        //创建sm2 对象
//        SM2 sm2 = SmUtil.sm2(pv,pu);
        //这里会自动生成对应的随机秘钥对 , 注意！ 这里一定要强转，才能得到对应有效的秘钥信息
//        byte[] privateKey = BCUtil.encodeECPrivateKey(sm2.getPrivateKey());
//        //这里公钥不压缩  公钥的第一个字节用于表示是否压缩  可以不要
//        byte[] publicKey = ((BCECPublicKey) sm2.getPublicKey()).getQ().getEncoded(false);
        //这里得到的 压缩后的公钥   ((BCECPublicKey) sm2.getPublicKey()).getQ().getEncoded(true);
        // byte[] publicKeyEc = BCUtil.encodeECPublicKey(sm2.getPublicKey());
        //打印当前的公私秘钥
//        System.out.println("私钥: " + HexUtil.encodeHexStr(pv.getBytes()));
//        System.out.println("公钥: " + HexUtil.encodeHexStr(pu.getBytes()));
        //得到明文对应的字节数组
//        byte[] dateBytes = text.getBytes();
//        System.out.println("数据: " + HexUtil.encodeHexStr(dateBytes));
//        //这里需要手动设置，sm2 对象的默认值与我们期望的不一致
//        sm2.setMode(SM2Engine.Mode.C1C3C2);
//        sm2.setEncoding(new PlainDSAEncoding());
//        //计算签名
//        byte[] sign = sm2.sign(dateBytes, null);
//        System.out.println("签名: " + HexUtil.encodeHexStr(sign));
//        // 校验  验签
//        boolean verify = sm2.verify(dateBytes, sign);
//        System.out.println(verify);
//
//
//        String signold = SM2CryptUtils.sign(pv,text);
//        System.out.println(signold);

        // SM3加密密码
        String sm3password = SmUtil.sm3("asbcde").toUpperCase();
         //再对密码sm2

        String keystore = SM2CryptUtils.encrypt(sm3password,SecretKeyConstants.SERVER_PUBLIC_KEY);
        System.out.println("keystore："+keystore);
//
//        System.out.println("==========mysql配置==========");
//        String sm2dbusername = SM2CryptUtils.encrypt("root",SecretKeyConstants.SERVER_PUBLIC_KEY);
//        System.out.println("数据库用户名："+sm2dbusername);
//        String sm2dbpwd = SM2CryptUtils.encrypt("!QAZ2root",SecretKeyConstants.SERVER_PUBLIC_KEY);
//        System.out.println("数据库密码："+sm2dbpwd);
//
//        System.out.println("==========mail配置==========");
//        String mailUserName = SM2CryptUtils.encrypt("xushilong@jiayuepowertech.com",SecretKeyConstants.SERVER_PUBLIC_KEY);
//        System.out.println("mailUserName："+mailUserName);
//        String mailPassWord = SM2CryptUtils.encrypt("jiayue123456",SecretKeyConstants.SERVER_PUBLIC_KEY);
//        System.out.println("mailPassWord："+mailPassWord);

//        String mailPassWord1 = SM2CryptUtils.encrypt("系统管理员(内置)",SecretKeyConstants.SERVER_PUBLIC_KEY);
//        System.out.println("mailPassWord1："+mailPassWord1);
//        String mailPassWord2 = SM2CryptUtils.encrypt("审计管理员(内置)",SecretKeyConstants.SERVER_PUBLIC_KEY);
//        System.out.println("mailPassWord2："+mailPassWord2);
//        String mailPassWord3 = SM2CryptUtils.encrypt("审核管理员(内置)",SecretKeyConstants.SERVER_PUBLIC_KEY);
//        System.out.println("mailPassWord3："+mailPassWord3);
//        String mailPassWord4 = SM2CryptUtils.encrypt("业务审计员",SecretKeyConstants.SERVER_PUBLIC_KEY);
//        System.out.println("mailPassWord4："+mailPassWord4);
//        String mailPassWord5 = SM2CryptUtils.encrypt("业务管理员",SecretKeyConstants.SERVER_PUBLIC_KEY);
//        System.out.println("mailPassWord5："+mailPassWord5);
//        String mailPassWord6 = SM2CryptUtils.encrypt("业务操作员",SecretKeyConstants.SERVER_PUBLIC_KEY);
//        System.out.println("mailPassWord6："+mailPassWord6);
//        String mailPassWord7 = SM2CryptUtils.encrypt("业务配置员",SecretKeyConstants.SERVER_PUBLIC_KEY);
//        System.out.println("mailPassWord7："+mailPassWord7);
    }
}
