package info.aspirecn.iov.sjjh.supplier10000027.util;

import com.fri.ctid.security.jit.Api;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * Created by on 2018/3/29.
 * 保留数据区
 * 加密保留数据与照片工具类
 */
public class EncryptedReservedData {
    private static String certID = "rzfw01";
    private static BASE64Encoder be = new BASE64Encoder();
    static Api api = new Api();
    static com.fri.ctid.security.bjca.Api api2 = new com.fri.ctid.security.bjca.Api();

    //加密保留数据(吉大正元)
    public static byte[] encryptEnvelope(byte[] msg) throws Exception {
        api.initConnection("./config/cssconfig.properties");
        byte[] result = api.encryptEnvelope(msg,certID);
        return result;
    }

    //加密保留数据(北京CA)
    public static String encodeEnvelopedDataForJit(String inData) {
        String envelopeCertID = "5fbec52c7f1bc951";
        byte[] DecodeEnvelopeData = api2.encryptEnvelope(inData.getBytes(), envelopeCertID);
        String de = be.encode(DecodeEnvelopeData);
        return de;
    }
    //解密保留数据(北京CA)
    public static String decodeEnvelopedDataForJit(String inData) {
        String envelopeCertID = "282c16739b20b580";
        byte[] DecodeEnvelopeData = api2.decryptEnvelope(inData.getBytes(), envelopeCertID);
        String de = DecodeEnvelopeData.toString();
        return de;
    }

    public static void main(String[] args) {
        try {
            String base64 = "MIIBtwYKKoEcz1UGAQQCA6CCAacwggGjAgEAMYHQMIHNAgEAMD0wMTELMAkGA1UEBhMCQ04xETAP\n" +
                    "BgNVBAoTCEdMQ1RJRDAxMQ8wDQYDVQQDEwZHTENBMDECCH96wSriUCnPMA0GCSqBHM9VAYItAwUA\n" +
                    "BHoweAIgV2vtuzfYt9EdI1aceGrSVYtbinCGQsmQ4AhZEcArCPQCIEuxBZwXQN9+fRlJ4aZi5z08\n" +
                    "dhM1BJ+LC9OXt67M0pf+BCDR5pkv4Z3cgiv7SG2SfJMwBDYMwwCRJktIFtwSl5AJrQQQeGHrcKC0\n" +
                    "wF697aInqEjAGzCBygYKKoEcz1UGAQQCATAJBgcqgRzPVQFogIGwQwxRUh51/nZZ0It9Qyo3iOsO\n" +
                    "WvbDyaYGpzZYchXKef8fbBczVtIoCA51JIr5VHPkWc3P55b+v66ApN3BI2JVjOZAT7XRgUFZFWOl\n" +
                    "LzJF6or6Skrz0S68d7RWQZ4fFFHnEGMzIcb8lDEVfSZ6ZIYdFO4ayAG3gXIRzyK3G3bXBYDs5HJZ\n" +
                    "dtuZpSSZSHUX7bAUJo0/P68zWwhLngUmcVSkUwemuskexS46mzPdTXH+i4s=)";
            System.out.println(decodeEnvelopedDataForJit(base64));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
