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.AsymmetricDecryptRequest;
import cn.unitid.matrix.pkiservice.sdk.ces.response.AsymmetricDecryptResponse;
import cn.unitid.matrix.pkiservice.sdk.consts.CertSearchEnum;
import cn.unitid.matrix.pkiservice.sdk.consts.DataType;
import cn.unitid.matrix.pkiservice.sdk.consts.KeypairTypeEnum;

/**
 * 非对称私钥解密
 */
public class AsymmetricDecryptTest {

    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);


        AsymmetricDecryptRequest request = new AsymmetricDecryptRequest();

        String data = "MGwCIQDpONXxj9Dt/fp5wTEMRZF32jmHivvQGX2FN408/97z0gIgJwaJVwMnEBRaP0KghS2Gjf/k1tQhL8oj0pgKHHvEHBUEIBfYYQ2H6Sk4rauwjesOryWt4GylyovZ5k7rW17QLNmSBANN0aQ=";

        request.setDecryptType(CertSearchEnum.CERT_ID);

        // 解密依赖项的值
        request.setDecryptItemValue("628cb99a51cfb422619db668");

        // 解密依赖项的值
        request.setCiphertext(data);

        // 解密数据类型
        request.setDataType(DataType.BASE64);

        // 解密后数据类型
        request.setDecryptDataType(DataType.BASE64);

        // 用加密密钥 加密
        request.setKeyType(KeypairTypeEnum.ENCRYPT);

        try {
            AsymmetricDecryptResponse response = pkiClient.execute(request);
            // 后续业务
            if (response.isSuccess()) {
                System.out.println("成功：" + response.getResult());
            } else {
                System.out.println("失败：" + response.getBody());
            }
        } catch (ApiException e) {
            e.printStackTrace();
        }
    }
}
