增加验证测试程序
This commit is contained in:
parent
f05c04f957
commit
b37b962dd5
2
pom.xml
2
pom.xml
@ -243,7 +243,7 @@
|
|||||||
<configuration>
|
<configuration>
|
||||||
<archive>
|
<archive>
|
||||||
<manifest>
|
<manifest>
|
||||||
<mainClass>racal.sunyard.main.Version</mainClass>
|
<mainClass>com.Sample.TestDemo</mainClass>
|
||||||
</manifest>
|
</manifest>
|
||||||
</archive>
|
</archive>
|
||||||
<descriptorRefs>
|
<descriptorRefs>
|
||||||
|
|||||||
111
src/main/java/com/Sample/TestDemo.java
Normal file
111
src/main/java/com/Sample/TestDemo.java
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
package com.Sample;
|
||||||
|
|
||||||
|
import com.sunyard.RetWrap;
|
||||||
|
import com.sunyard.proto.Util;
|
||||||
|
import com.sunyard.util.Configure;
|
||||||
|
import java.io.IOException;
|
||||||
|
import racal.sunyard.main.SydApi4j;
|
||||||
|
import racal.sunyard.main.SydApiBuilder;
|
||||||
|
|
||||||
|
public class TestDemo {
|
||||||
|
public static String sign(SydApi4j api, String certDn, String orgData) {
|
||||||
|
byte[] nakedSign = api.SYD_NakedSignAll(2, orgData.getBytes(), certDn);
|
||||||
|
System.out.println("签名数据:"+ Util.bytes2HexString(nakedSign));
|
||||||
|
return Util.bytes2HexString(nakedSign);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Boolean signVer(SydApi4j api, String certDn, String orgData, String sign) {
|
||||||
|
boolean nakedVerify = api.SYD_NakedVerifyAll(2, orgData.getBytes(), Util.hexString2Bytes(sign), certDn);
|
||||||
|
System.out.println("验签结果:"+ nakedVerify);
|
||||||
|
return nakedVerify;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String enData(SydApi4j api, String certDn, String orgData) {
|
||||||
|
RetWrap genSM2 = api.generateDEByDnAll(3, certDn, orgData.getBytes());
|
||||||
|
byte[] cipherKey = (byte[])genSM2.get("CipherKey");
|
||||||
|
System.out.println("加密结果:"+ new String(cipherKey));
|
||||||
|
return new String(cipherKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String deData(SydApi4j api, String certDn, String sign) {
|
||||||
|
RetWrap denSM2 = api.decryptDEByDNAll(3, certDn, sign.getBytes());
|
||||||
|
byte[] oData = (byte[])denSM2.get("oData");
|
||||||
|
System.out.println("解密数据:"+ new String(oData));
|
||||||
|
return new String(oData);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String detachSign(SydApi4j api, String certDn, String orgData) {
|
||||||
|
String sign = api.detachedSignAll(2, orgData.getBytes(), certDn);
|
||||||
|
System.out.println("detach签名结果:"+ sign);
|
||||||
|
return sign;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Boolean detachSignVer(SydApi4j api, String orgData, String sign) {
|
||||||
|
boolean signVerify = api.detachedVerifyAll(2, orgData.getBytes(), sign);
|
||||||
|
System.out.println("detach验签:"+ signVerify);
|
||||||
|
return Boolean.valueOf(signVerify);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void testAll(SydApi4j api, String certDn, String orgData) {
|
||||||
|
byte[] nakedSign = api.SYD_NakedSignAll(2, orgData.getBytes(), certDn);
|
||||||
|
System.out.println("签名数据:"+ Util.bytes2HexString(nakedSign));
|
||||||
|
boolean nakedVerify = api.SYD_NakedVerifyAll(2, orgData.getBytes(), nakedSign, certDn);
|
||||||
|
System.out.println("验签结果:"+ nakedVerify);
|
||||||
|
RetWrap genSM2 = api.generateDEByDnAll(3, certDn, orgData.getBytes());
|
||||||
|
byte[] cipherKey = (byte[])genSM2.get("CipherKey");
|
||||||
|
System.out.println("加密结果:"+ new String(cipherKey));
|
||||||
|
RetWrap denSM2 = api.decryptDEByDNAll(3, certDn, cipherKey);
|
||||||
|
byte[] oData = (byte[])denSM2.get("oData");
|
||||||
|
System.out.println("解密数据:"+ new String(oData));
|
||||||
|
String sign = api.detachedSignAll(2, orgData.getBytes(), certDn);
|
||||||
|
System.out.println("detach签名结果:"+ sign);
|
||||||
|
boolean signVerify = api.detachedVerifyAll(2, orgData.getBytes(), sign);
|
||||||
|
System.out.println("detach验签:"+ signVerify);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws InterruptedException {
|
||||||
|
try {
|
||||||
|
String ip = Configure.getKey("ip");
|
||||||
|
System.out.println("ip:"+ ip);
|
||||||
|
int port = Integer.parseInt(Configure.getKey("port"));
|
||||||
|
System.out.println("port:"+ port);
|
||||||
|
int timeout = Integer.parseInt(Configure.getKey("timeout"));
|
||||||
|
System.out.println(""+ timeout);
|
||||||
|
String certDn = Configure.getKey("certDn");
|
||||||
|
System.out.println("certDn:"+ certDn);
|
||||||
|
String signorenData = Configure.getKey("signorenData");
|
||||||
|
System.out.println("signorenData:"+ signorenData);
|
||||||
|
String orgData = Configure.getKey("orgData");
|
||||||
|
System.out.println("orgData:"+ orgData);
|
||||||
|
int type = Integer.parseInt(Configure.getKey("type"));
|
||||||
|
System.out.println("type:"+ type);
|
||||||
|
SydApiBuilder builder = (new SydApiBuilder()).setIp(ip).setPort(port).setTimeout(timeout);
|
||||||
|
SydApi4j api = (SydApi4j)builder.build();
|
||||||
|
switch (type) {
|
||||||
|
case 1:
|
||||||
|
sign(api, certDn, orgData);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
signVer(api, certDn, orgData, signorenData);
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
enData(api, certDn, orgData);
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
deData(api, certDn, signorenData);
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
detachSign(api, certDn, orgData);
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
detachSignVer(api, orgData, signorenData);
|
||||||
|
break;
|
||||||
|
case 0:
|
||||||
|
testAll(api, certDn, orgData);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -63,6 +63,11 @@ public class SydApiException extends RuntimeException{
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case 0x7007 : {
|
||||||
|
msg = "实体白名单未添加";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
case 32770:
|
case 32770:
|
||||||
msg = "签名验证错误(P7) ";
|
msg = "签名验证错误(P7) ";
|
||||||
break;
|
break;
|
||||||
|
|||||||
18
src/main/java/com/sunyard/util/Configure.java
Normal file
18
src/main/java/com/sunyard/util/Configure.java
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package com.sunyard.util;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
public class Configure {
|
||||||
|
public static String getKey(String key) throws IOException {
|
||||||
|
Properties p = new Properties();
|
||||||
|
InputStream in = new FileInputStream("./sunyard.properties");
|
||||||
|
InputStreamReader sr = new InputStreamReader(in, "UTF-8");
|
||||||
|
p.load(sr);
|
||||||
|
String value = p.getProperty(key).trim();
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -654,13 +654,15 @@ public class SydApi4j implements SydApi {
|
|||||||
// MD5
|
// MD5
|
||||||
data.put("rFlag", 'M');
|
data.put("rFlag", 'M');
|
||||||
}
|
}
|
||||||
if (rFlag == 2) {
|
else if (rFlag == 2) {
|
||||||
// SHA-1
|
// SHA-1
|
||||||
data.put("rFlag", 'S');
|
data.put("rFlag", 'S');
|
||||||
}
|
}
|
||||||
if (rFlag == 3) {
|
else if (rFlag == 3) {
|
||||||
// SHA-256
|
// SHA-256
|
||||||
data.put("rFlag", 'R');
|
data.put("rFlag", 'R');
|
||||||
|
}else {
|
||||||
|
throw new SydApiException("不支持的摘要算法",10001);
|
||||||
}
|
}
|
||||||
data.put("signatureDataType$option", 1);
|
data.put("signatureDataType$option", 1);
|
||||||
data.put("DN", dn);
|
data.put("DN", dn);
|
||||||
|
|||||||
11
sunyard.properties
Normal file
11
sunyard.properties
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
ip=172.16.18.34
|
||||||
|
port=8889
|
||||||
|
timeout=5000
|
||||||
|
#证书主题
|
||||||
|
certDn=C=CN,ST=zj,L=hz,O=sunyard,CN=welkin
|
||||||
|
#验签数据或待解密数据
|
||||||
|
signorenData=XXXX
|
||||||
|
#原始数据
|
||||||
|
orgData=123456
|
||||||
|
#交易类型 1-签名 2-验签 3-加密 4-解密 0 默认(签名验签+加密解密)
|
||||||
|
type=0
|
||||||
Loading…
Reference in New Issue
Block a user