122 lines
3.7 KiB
Java
122 lines
3.7 KiB
Java
package sample;
|
||
|
||
|
||
import com.sunyard.SydApi;
|
||
import racal.sunyard.main.SydApi4j;
|
||
|
||
|
||
public class SamplePKCS7 {
|
||
|
||
private static final String testData = "<MainBody><MsgId><Id>101557000040870201809190000000001</Id><CreDtTm>2018-09-19T10:13:39</CreDtTm></MsgId><Qryr><MbrId>101557</MbrId><BrId>000040870</BrId></Qryr><Rspnr><MbrId>201597</MbrId><BrId>000039754</BrId></Rspnr><BizQryInf><QryDt>2018-09-19</QryDt><QryId>00000001</QryId><Cnts>123456,测试</Cnts></BizQryInf></MainBody>";
|
||
private static final String testPubKey = "034200043EDA3A107B7CC35CC005C7FBF0BB4E6B43A07EE14E360603D22ECA2A600B2D0FA03055D400D1240300F54648B849D13F50B330FF14351B5645B13686820A65BB";
|
||
private static final String testCertNo = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1";
|
||
|
||
public static void main(String[] args) {
|
||
|
||
// 连接加密机
|
||
// SydApi4j api = (SydApi4j) new SydApi4j().connect("172.16.17.162", 8889, null, 1000);
|
||
SydApi4j.SYD_Set_Parameter_HA("unkown", "192.168.0.100", 8889, 1000);
|
||
SydApi4j api = (SydApi4j) new SydApi4j();
|
||
|
||
try {
|
||
|
||
// 基础接口 原始数据模式
|
||
case1( api );
|
||
|
||
// 基础接口 HASH 数据模式
|
||
case2( api );
|
||
|
||
// 原始数据模式专用接口
|
||
case3( api );
|
||
|
||
// HASH 模式专用接口
|
||
case4( api );
|
||
|
||
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
} finally {
|
||
api.disconnect();
|
||
}
|
||
}
|
||
|
||
public static void case1(SydApi api) {
|
||
System.out.println("\n\n----- PKCS7 签名验签 基础接口原始数据模式 -----");
|
||
String sign = api.detachedSign(
|
||
SydApi.DATA_ORIGIN,
|
||
testData.getBytes(),
|
||
testCertNo
|
||
);
|
||
|
||
System.out.println("\t签名结果: " + sign);
|
||
|
||
boolean verify = api.detachedVerify(
|
||
SydApi.DATA_ORIGIN,
|
||
testData.getBytes(),
|
||
sign
|
||
);
|
||
|
||
System.out.println("\t验签结果: " + verify);
|
||
}
|
||
|
||
public static void case2(SydApi api) {
|
||
System.out.println("\n\n----- PKCS7 签名验签 基础接口 hash 数据模式 -----");
|
||
byte[] hash = api.SM3Hash(testPubKey, testData.getBytes());
|
||
|
||
String sign = api.detachedSign(
|
||
SydApi.DATA_HASH,
|
||
hash,
|
||
SamplePKCS7.testCertNo
|
||
);
|
||
|
||
System.out.println("\t签名结果: " + sign);
|
||
|
||
boolean verify = api.detachedVerify(
|
||
SydApi.DATA_HASH,
|
||
hash,
|
||
sign
|
||
);
|
||
|
||
System.out.println("\t验签结果: " + verify);
|
||
}
|
||
|
||
public static void case3(SydApi api) {
|
||
System.out.println("\n\n----- PKCS7 签名验签 原始数据模式 -----");
|
||
|
||
String sign = api.SYD_DetachedSign(
|
||
testData.getBytes(),
|
||
testCertNo
|
||
);
|
||
|
||
System.out.println("\t签名结果: " + sign);
|
||
|
||
boolean verify = api.SYD_DetachedVerify(
|
||
testData.getBytes(),
|
||
sign
|
||
);
|
||
|
||
System.out.println("\t验签结果: " + verify);
|
||
}
|
||
|
||
public static void case4(SydApi api) {
|
||
System.out.println("\n\n----- PKCS7 签名验签 hash 数据模式 -----");
|
||
|
||
String sign = api.sm3AndDetachedSign(
|
||
testData.getBytes(),
|
||
testPubKey,
|
||
SamplePKCS7.testCertNo
|
||
);
|
||
|
||
System.out.println("\t签名结果: " + sign);
|
||
|
||
boolean verify = api.sm3AndDetachedVerify(
|
||
testData.getBytes(),
|
||
testPubKey,
|
||
sign
|
||
);
|
||
|
||
System.out.println("\t验签结果: " + verify);
|
||
}
|
||
|
||
}
|