package cn.unitid.matrix.pkiservice.test.ces;

import cn.unitid.matrix.pkiservice.sdk.ApiException;
import cn.unitid.matrix.pkiservice.sdk.PkiClient;
import cn.unitid.matrix.pkiservice.sdk.PkiOkhttpClient;
import cn.unitid.matrix.pkiservice.sdk.ces.request.KeypairImportSm2Request;
import cn.unitid.matrix.pkiservice.sdk.ces.response.CommonResponse;
import cn.unitid.matrix.pkiservice.sdk.ces.response.KeypairImportSm2Response;

public class KeypairImportSm2Test {

    public static void main(String[] args) {

        // 提供的URL
        String url = "http://172.16.3.232:25115";

        // 您的appKey
        String appKey = "eB4P623i5h";

        // 您的appSecret
        String appSecret = "RzToiB0vzP6hnWd0Rt8u3Om2RDL0YHAA";

        // 原客户端
        PkiClient pkiClient = new PkiOkhttpClient(url, appKey, appSecret);

        KeypairImportSm2Request request = new KeypairImportSm2Request();

        Integer virtualIndex = 20023;

        String encPrivateKey = "bs4PSVKe0c8SD4US/14/ZEPuHnG2h4u6z7nfUUsVQAs=";

        String encPublicKey = "BKH4KjNJRpHO3mL7tk9/+v0O+QJZJb0xlOxlODHICPF+s9DHESicyAVqQbpYZRS+2MTf1Pru+cYC2psQrxetxAo=";

        // 虚拟索引
        request.setVirtualIndex(virtualIndex);

        // 加密私钥base64
        request.setEncPrivateKey(encPrivateKey);

        // 加密公钥base64
        request.setEncPublicKey(encPublicKey);

        // 加密私钥无保护结构
        request.setProtectiveStructure(0);

        try {
            KeypairImportSm2Response response = pkiClient.execute(request);
            // 后续业务
            if (response.isSuccess()) {
//                System.out.println("成功：" + response.getResult());
                KeypairImportSm2Response.Result result = response.getResult();
                System.out.println("加密密钥对公钥: " + result.getEncPublicKey());
            } else {
                System.out.println("失败：" + response.getBody());
            }
        } catch (ApiException e) {
            e.printStackTrace();
        }

    }
}
