证书工具
This commit is contained in:
parent
48a1aa828c
commit
ee395aebee
@ -6,4 +6,24 @@ public class CertUtil {
|
||||
public static String encodeBase64(byte[] data){
|
||||
return new BASE64Encoder().encode( data );
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断正式是否是国密证书
|
||||
* @param cert 证书 base64 字符
|
||||
* @return true 国密, false 非国密
|
||||
*/
|
||||
public static boolean isSM2Cert(String cert){
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断 p7 是否是国密证书签的
|
||||
* @param pkcs7 pkcs7
|
||||
* @return true 国密, false 非国密
|
||||
*/
|
||||
public static boolean isSignedBySM2Cert( String pkcs7 ){
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
67
src/test/java/sample/SamplePKCS1.java
Normal file
67
src/test/java/sample/SamplePKCS1.java
Normal file
@ -0,0 +1,67 @@
|
||||
package sample;
|
||||
|
||||
|
||||
import com.sunyard.SydApi;
|
||||
import com.sunyard.proto.Util;
|
||||
import racal.sunyard.main.SydApi4j;
|
||||
|
||||
|
||||
public class SamplePKCS1 {
|
||||
|
||||
private static final String testId = "8012";
|
||||
private static final String testData = "1234567812345678";
|
||||
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("192.168.0.200", 8889, null, 1000);
|
||||
|
||||
|
||||
try {
|
||||
|
||||
// 原始数据模式
|
||||
case1( api );
|
||||
|
||||
// 外部 hash
|
||||
case2( api );
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
api.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
// 签名验签 1
|
||||
public static void case1(SydApi api) {
|
||||
|
||||
System.out.println("\n\n----- 裸签名 -----");
|
||||
byte[] sign = api.SYD_NakedSign(testData.getBytes(), testCertNo);
|
||||
System.out.println("签名结果 " + Util.bytes2HexString(sign) );
|
||||
|
||||
|
||||
boolean pass = api.SYD_NakedVerify(testData.getBytes(), sign, testCertNo);
|
||||
System.out.println("验签结果 " + pass);
|
||||
}
|
||||
|
||||
// 签名验签 2
|
||||
public static void case2(SydApi api) {
|
||||
|
||||
String pk = api.SM2GetPublicKeyC( testId ,0);
|
||||
|
||||
System.out.println( pk );
|
||||
|
||||
byte[] data = api.SM3Hash( Util.hexString2Bytes(pk) );
|
||||
|
||||
byte[] sign = api.SYD_NakedSign(data, testCertNo);
|
||||
System.out.println("签名结果 " + Util.bytes2HexString(sign) );
|
||||
|
||||
|
||||
boolean pass = api.SYD_NakedVerify(data, sign, testCertNo);
|
||||
System.out.println("验签结果 " + pass);
|
||||
}
|
||||
|
||||
}
|
||||
121
src/test/java/sample/SamplePKCS7.java
Normal file
121
src/test/java/sample/SamplePKCS7.java
Normal file
@ -0,0 +1,121 @@
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user