Merge branch 'dev-icbc' into dev-shangqing
# Conflicts: # .gitignore # doc/java接口说明文档.docx # src/main/java/racal/sunyard/main/SydApi4j.java # src/test/java/com/sunyard/sydapi/test/TestAllMethods.java # src/test/java/com/sunyard/sydapi/test/TestSV.java
This commit is contained in:
@@ -11,4 +11,5 @@
|
||||
/.DS_Store
|
||||
src/test/java/com/sunyard/sydapi/sample/*.*
|
||||
*.p7
|
||||
~$*
|
||||
/pack/
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,85 @@
|
||||
{
|
||||
"vars": {
|
||||
"version": "%project.git_branch_version%",
|
||||
"out_path": "./pack"
|
||||
},
|
||||
"clear": [
|
||||
{
|
||||
"func": "rm",
|
||||
"path": "%out_path%"
|
||||
}
|
||||
],
|
||||
"init": [
|
||||
{
|
||||
"func": "mkdir",
|
||||
"path": "%out_path%"
|
||||
},
|
||||
{
|
||||
"func": "mkdir",
|
||||
"path": "%out_path%/jar"
|
||||
},
|
||||
{
|
||||
"func": "mkdir",
|
||||
"path": "%out_path%/jar/dependences"
|
||||
},
|
||||
{
|
||||
"func": "mkdir",
|
||||
"path": "%out_path%/doc"
|
||||
}
|
||||
],
|
||||
"gen" : [{
|
||||
"cmd" : "dproto"
|
||||
}],
|
||||
"build": [
|
||||
{
|
||||
"cmd": "clear"
|
||||
},
|
||||
{
|
||||
"cmd": "init"
|
||||
},
|
||||
{
|
||||
"shell": "mvn package -DskipTests",
|
||||
"check": "BUILD SUCCESS"
|
||||
},
|
||||
{
|
||||
"func": "cp",
|
||||
"from": "./target/sydapi4j.jar",
|
||||
"to": "%out_path%/jar/sydapi4j.jar"
|
||||
},
|
||||
{
|
||||
"func": "cp",
|
||||
"from": "./target/sydapi4j-jar-with-dependencies.jar",
|
||||
"to": "%out_path%/jar/sydapi4j-jar-with-dependencies.jar"
|
||||
},
|
||||
|
||||
{
|
||||
"shell" : "mvn dependency:copy-dependencies -DincludeScope=runtime -DoutputDirectory=%out_path%/jar/dependences",
|
||||
"check": "BUILD SUCCESS"
|
||||
},
|
||||
{
|
||||
"func": "cp",
|
||||
"from": "./doc",
|
||||
"to": "%out_path%/doc"
|
||||
},
|
||||
{
|
||||
"func": "cp",
|
||||
"from": "./src/test/java/sample",
|
||||
"to": "%out_path%/sample"
|
||||
},
|
||||
{
|
||||
"func": "cp",
|
||||
"from": "./说明.txt",
|
||||
"to": "%out_path%/说明.txt"
|
||||
},
|
||||
{
|
||||
"func": "zip",
|
||||
"from": "%out_path%/*",
|
||||
"to": "%out_path%/sydapi-%version%.%project.git_commit_hash%.zip"
|
||||
},
|
||||
{
|
||||
"func": "open",
|
||||
"path": "%out_path%/"
|
||||
}
|
||||
]
|
||||
|
||||
}
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
<groupId>com.sunyard</groupId>
|
||||
<artifactId>sydapi4j</artifactId>
|
||||
<version>1.0.20220402</version>
|
||||
<version>1.0.20220712</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>sydapi4j</name>
|
||||
@@ -72,7 +72,7 @@
|
||||
<dependency>
|
||||
<groupId>com.sunyard</groupId>
|
||||
<artifactId>proto</artifactId>
|
||||
<version>1.0-20220412</version>
|
||||
<version>1.0-20220804</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
package com.sunyard;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 管理多线程间对象
|
||||
* 类似 ThreadLocal
|
||||
* @param <T>
|
||||
*/
|
||||
public class MutiThreadHander<T> {
|
||||
|
||||
private static class Handler<T> {
|
||||
private T obj = null;
|
||||
private long creatAt = System.currentTimeMillis();
|
||||
private long lastGetAt = System.currentTimeMillis();
|
||||
|
||||
public Handler(T obj) {
|
||||
this.obj = obj;
|
||||
}
|
||||
|
||||
public T getObj() {
|
||||
return obj;
|
||||
}
|
||||
|
||||
public long getCreatAt() {
|
||||
return creatAt;
|
||||
}
|
||||
|
||||
public long getLastGetAt() {
|
||||
return lastGetAt;
|
||||
}
|
||||
}
|
||||
private final Map<Thread, Handler> handers = new HashMap<Thread, Handler>();
|
||||
private ScheduledExecutorService gcThread = Executors.newScheduledThreadPool(1);
|
||||
|
||||
private long maxLiveMillis = 8 * 3600 * 1000;
|
||||
private long maxIdleMillis = 3600 * 1000;
|
||||
private long checkEveryMillis = 5 * 60 * 1000;
|
||||
|
||||
public MutiThreadHander() {
|
||||
gcThread.scheduleWithFixedDelay(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
if ( handers.size() <= 0 ) {
|
||||
return;
|
||||
}
|
||||
|
||||
long now = System.currentTimeMillis();
|
||||
Iterator<Map.Entry<Thread, Handler>>it = handers.entrySet().iterator();
|
||||
while ( it.hasNext() ){
|
||||
Map.Entry<Thread, Handler> entry = it.next();
|
||||
|
||||
// check maxLiveMillis
|
||||
if ( entry.getValue().getCreatAt() - now > maxLiveMillis ){
|
||||
synchronized ( handers ){
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
|
||||
// check maxIdleMillis
|
||||
if ( entry.getValue().getLastGetAt() - now > maxIdleMillis ){
|
||||
synchronized ( handers ){
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch ( Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}, checkEveryMillis, checkEveryMillis, TimeUnit.MILLISECONDS );
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected T initialValue() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void clear(){
|
||||
synchronized ( handers ) {
|
||||
handers.remove( Thread.currentThread() );
|
||||
}
|
||||
}
|
||||
|
||||
public void set(T obj){
|
||||
synchronized ( handers ) {
|
||||
handers.put( Thread.currentThread() , new Handler(obj));
|
||||
}
|
||||
}
|
||||
|
||||
public T get(){
|
||||
synchronized ( handers ) {
|
||||
Handler hander = handers.get( Thread.currentThread() );
|
||||
if ( null == hander ) {
|
||||
T o = initialValue();
|
||||
hander = new Handler( o );
|
||||
handers.put( Thread.currentThread(), hander );
|
||||
}
|
||||
return (T) hander.getObj();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.sunyard.inf;
|
||||
import com.sunyard.RetWrap;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 对称加解密相关接口
|
||||
@@ -273,6 +274,9 @@ interface SydSYMApi {
|
||||
*/
|
||||
byte[] SYMEncryptData(int nAlg, String seesionKey, int fullmode, byte[] pINdata);
|
||||
|
||||
|
||||
List<byte[]> SYMEncryptDecryptBatchData(int nAlg, String[] seesionKey, int fullmode, List<byte[]> pINdata);
|
||||
|
||||
/**
|
||||
* 用输入的会话密钥,对指定报文数据进行加密处理。
|
||||
*
|
||||
|
||||
@@ -60,8 +60,8 @@ interface SydAttachedSVApi {
|
||||
* @param orgData 要签名的信息
|
||||
* @return 签名结果
|
||||
*/
|
||||
String SM2SignC(int flag, String privateKey, byte[] publicKey, byte[] orgData);
|
||||
String SM2SignC(int flag, String pUserID, String privateKey, byte[] publicKey, byte[] orgData);
|
||||
byte[] SM2SignC(int flag, String privateKey, byte[] publicKey, byte[] orgData);
|
||||
byte[] SM2SignC(int flag, String pUserID, String privateKey, byte[] publicKey, byte[] orgData);
|
||||
/**
|
||||
* SM2 私钥签名
|
||||
* @param pUserID 使用者的 ID,最大长度为 16 位数字,右对齐。
|
||||
@@ -142,7 +142,7 @@ interface SydAttachedSVApi {
|
||||
int nOrgDataType,
|
||||
byte[] publicKey,
|
||||
byte[] orgData,
|
||||
String sign
|
||||
byte[] sign
|
||||
);
|
||||
|
||||
|
||||
|
||||
@@ -117,13 +117,13 @@ public interface SydHAApi {
|
||||
* @param nOrgDataType 签名数据类型,SydApi.DATA_ORIGIN(1) 原始数据 SydApi.DATA_HASH(0) : hash 后的类型
|
||||
* @param publicKey 公钥串,DER编码,HEX格式
|
||||
* @param orgData 待签名的原始数据,“签名数据类型”为 0 时:HASH 值。“签名数据类型”为 1 时:要签名的信息
|
||||
* @param base64Sign 签名数据 (DER 编码 + Base64 编码)
|
||||
* @param sign S+R
|
||||
* @return 验签是否通过
|
||||
*/
|
||||
public boolean SM2Verify(
|
||||
int nOrgDataType,
|
||||
byte[] publicKey,
|
||||
byte[] orgData,
|
||||
String base64Sign
|
||||
byte[] sign
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2073,7 +2073,119 @@ public class SydApi4j implements SydApi {
|
||||
data.put("msg", pInData);
|
||||
packet.put("data", data);
|
||||
|
||||
return repackEncrypt(packet);
|
||||
return repackEncrypt( packet );
|
||||
}
|
||||
|
||||
|
||||
public List<byte[]> SYMEncryptDecryptBatchData(int nAlg, String[] seesionKey, int fullmode, List<byte[]> pINdata) {
|
||||
|
||||
int batchSize = pINdata.size();
|
||||
if ( batchSize < 1 || batchSize > 10 ) {
|
||||
throw new IllegalArgumentException("批量数量错误");
|
||||
}
|
||||
|
||||
if ( seesionKey.length != pINdata.size() ) {
|
||||
throw new IllegalArgumentException("密钥和数据数量不匹配");
|
||||
}
|
||||
|
||||
if ( 0x0 != (byte) (nAlg & 0x1) ){
|
||||
// 解密
|
||||
for ( byte[]d : pINdata ) {
|
||||
if ( d.length > 1040 ) {
|
||||
throw new IllegalArgumentException("批量单数数据超长 1040");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 加密
|
||||
for ( byte[]d : pINdata ) {
|
||||
if ( d.length > 1024 ) {
|
||||
throw new IllegalArgumentException("批量单数数据超长 1024");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ByteBuffer bb = ByteBuffer.allocate(10 * 1024);
|
||||
PacketSN sn = PacketSN.gen();
|
||||
bb.put( new byte[2] );
|
||||
bb.put( sn.getSn().array() );
|
||||
bb.put( "E0".getBytes() );
|
||||
bb.put( (byte) 0x34 ); // 批量加解密
|
||||
bb.put( (byte) batchSize ); // 批量数
|
||||
bb.put( (byte) ( 0x30 + nAlg) ); // 密钥模式
|
||||
bb.put( (byte) ( 0x31) ); // ECB
|
||||
bb.put( (byte) ( 0x30) ); // ZEK
|
||||
|
||||
// 填充多个密钥
|
||||
for ( String key : seesionKey ) {
|
||||
bb.put( key.getBytes() );
|
||||
}
|
||||
|
||||
bb.put( (byte) ( 0x30) ); // 输入消息类型二进制
|
||||
bb.put( (byte) ( 0x30) ); // 输出消息类型二进制
|
||||
|
||||
bb.put( (byte) ( 0x30 + fullmode) ); // 填充模式
|
||||
bb.put( "0000".getBytes() ); // 填充字符
|
||||
bb.put( (byte) ( 0x32) ); // 填充类型
|
||||
|
||||
// iv 暂未支持
|
||||
// P 长度暂未支持
|
||||
|
||||
|
||||
// 填充多个消息数据长度
|
||||
for ( byte[] data : pINdata ) {
|
||||
bb.put( String.format("%03X", data.length).getBytes() );
|
||||
}
|
||||
|
||||
// 填充多个消息数据
|
||||
for ( byte[] data : pINdata ) {
|
||||
bb.put( data );
|
||||
}
|
||||
|
||||
// 重新计算长度
|
||||
int len = bb.position();
|
||||
len -= 2;
|
||||
byte[] lenArray = new byte[ 2 ];
|
||||
lenArray[0] = (byte)((len >> 8) & 0xFF);
|
||||
lenArray[1] = (byte)( len & 0xFF);
|
||||
|
||||
bb.put( 0, (byte)((len >> 8) & 0xFF) );
|
||||
bb.put( 1, (byte)( len & 0xFF) );
|
||||
|
||||
// 通信
|
||||
synchronized (this) {
|
||||
// 响应解析
|
||||
bb = syncRead(syncSend(bb));
|
||||
}
|
||||
|
||||
bb.flip();
|
||||
bb.position( 12 );
|
||||
byte[] code = new byte[2];
|
||||
bb.get( code );
|
||||
|
||||
int retCode = Integer.valueOf( new String( code ) );
|
||||
if ( 0 != retCode ) {
|
||||
throw new SydApiException( retCode );
|
||||
}
|
||||
|
||||
// 跳过输出模式
|
||||
bb.get();
|
||||
|
||||
// 获取 N 个长度
|
||||
int[] lens = new int[ batchSize ];
|
||||
for ( int i = 0; i < batchSize; i++ ){
|
||||
byte[] iLenArray = new byte[3];
|
||||
bb.get( iLenArray );
|
||||
lens[ i ] = Integer.valueOf( new String( iLenArray ), 16 );
|
||||
}
|
||||
|
||||
List<byte[]>ret = new ArrayList<byte[]>();
|
||||
// 获取 N 个长度的数据
|
||||
for ( int l : lens ) {
|
||||
byte[] d = new byte[ l ];
|
||||
bb.get(d);
|
||||
ret.add( d );
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
@@ -3626,6 +3738,13 @@ public class SydApi4j implements SydApi {
|
||||
// 测试通过
|
||||
public String SM2GetPrivateKeyC(String privateKeyTag) {
|
||||
|
||||
if ( privateKeyTag.length() < 16 ) {
|
||||
privateKeyTag = "0000000000000000" + privateKeyTag;
|
||||
privateKeyTag = privateKeyTag.substring( privateKeyTag.length() -16 );
|
||||
privateKeyTag += "00000000001";
|
||||
}
|
||||
|
||||
|
||||
ProtoQP proto = new ProtoQP();
|
||||
HashMap<String, Object> packet = new HashMap<String, Object>();
|
||||
HashMap<String, Object> data = new HashMap<String, Object>();
|
||||
@@ -3878,7 +3997,7 @@ public class SydApi4j implements SydApi {
|
||||
|
||||
// 测试通过
|
||||
@Override
|
||||
public String SM2SignC(int flag, String uid, String privateKey, byte[] publicKey, byte[] orgData) {
|
||||
public byte[] SM2SignC(int flag, String uid, String privateKey, byte[] publicKey, byte[] orgData) {
|
||||
|
||||
Proto74 proto = new Proto74();
|
||||
|
||||
@@ -3919,7 +4038,7 @@ public class SydApi4j implements SydApi {
|
||||
byte[] s = new byte[a];
|
||||
bb.get(s, 0, a);
|
||||
|
||||
return SYMUtil.derSign2Base64Sign(s);
|
||||
return Util.decodeBase64( SYMUtil.derSign2Base64Sign(s) );
|
||||
|
||||
}
|
||||
|
||||
@@ -4197,7 +4316,7 @@ public class SydApi4j implements SydApi {
|
||||
|
||||
// 测试通过
|
||||
@Override
|
||||
public boolean SM2Verify(String certNo, int nOrgDataType, byte[] publicKey, byte[] orgData, String base64Sign) {
|
||||
public boolean SM2Verify(String certNo, int nOrgDataType, byte[] publicKey, byte[] orgData, byte[] sign) {
|
||||
|
||||
if (certNo.length() > 32) {
|
||||
certNo = certNo.substring(0, 32);
|
||||
@@ -4205,7 +4324,7 @@ public class SydApi4j implements SydApi {
|
||||
|
||||
Proto75 proto = new Proto75(); //pset.getProtocolReqRender("75");
|
||||
|
||||
byte[] sign = SYMUtil.base64Sign2DerSign(base64Sign);
|
||||
sign = SYMUtil.base64Sign2DerSign( Util.encodeBase64(sign) );
|
||||
|
||||
// 填充数据
|
||||
HashMap<String, Object> packet = new HashMap<String, Object>();
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.sunyard.sydapi.loadrunner.raw;
|
||||
|
||||
/*
|
||||
* LoadRunner Java script. (Build: _build_number_)
|
||||
*
|
||||
* Script Description:
|
||||
*
|
||||
*/
|
||||
|
||||
import lrapi.lr;
|
||||
import racal.sunyard.main.SydApi4j;
|
||||
import racal.sunyard.main.SydApiBuilder;
|
||||
import com.sunyard.proto.Util;
|
||||
import com.sunyard.SydApi;
|
||||
|
||||
public class Actions
|
||||
{
|
||||
|
||||
private static final String inHexData = "67c6697351ff4aec29cdbaabf2fbe346";
|
||||
|
||||
private static String pk ;
|
||||
private static String sk ;
|
||||
private SydApi api;
|
||||
|
||||
|
||||
|
||||
public int init() throws Throwable {
|
||||
SydApiBuilder builder = new SydApiBuilder().setIp("192.168.1.123");
|
||||
api = builder.build();
|
||||
|
||||
this.pk = api.SM2GetPublicKeyC("123", 0 );
|
||||
this.sk = api.SM2GetPrivateKeyC("123");
|
||||
|
||||
return 0;
|
||||
}//end of init
|
||||
|
||||
|
||||
public int action() throws Throwable {
|
||||
lr.start_transaction("auto");//设置事务开始点
|
||||
|
||||
try {
|
||||
|
||||
System.out.println("begin SYD_Sign_P1");
|
||||
byte[] sign = this.api.SM2SignC( 0 , sk, Util.hexString2Bytes(pk), Util.hexString2Bytes(inHexData) );
|
||||
System.out.println("sign=" + Util.bytes2HexString( sign ));
|
||||
|
||||
System.out.println("begin SYD_Verify_P1");
|
||||
boolean pass = api.SM2Verify(1, Util.hexString2Bytes(pk), Util.hexString2Bytes(inHexData), sign );
|
||||
System.out.println("验签结果 " + pass);
|
||||
System.out.println("end SYD_Verify_P1");
|
||||
|
||||
} finally {
|
||||
lr.end_transaction("auto", lr.AUTO);//设置事务结束点
|
||||
}
|
||||
|
||||
return 0;
|
||||
}//end of action
|
||||
|
||||
|
||||
public int end() throws Throwable {
|
||||
return 0;
|
||||
}//end of end
|
||||
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
|
||||
Actions actions = new Actions();
|
||||
actions.init();
|
||||
actions.action();
|
||||
actions.end();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.sunyard.sydapi.loadrunner.raw_uid;
|
||||
|
||||
/*
|
||||
* LoadRunner Java script. (Build: _build_number_)
|
||||
*
|
||||
* Script Description:
|
||||
*
|
||||
*/
|
||||
|
||||
import lrapi.lr;
|
||||
import racal.sunyard.main.SydApi4j;
|
||||
import racal.sunyard.main.SydApiBuilder;
|
||||
import com.sunyard.proto.Util;
|
||||
import com.sunyard.SydApi;
|
||||
|
||||
public class Actions
|
||||
{
|
||||
|
||||
private static final String inHexData = "67c6697351ff4aec29cdbaabf2fbe346";
|
||||
private static final String uid = "8765432187654321";
|
||||
|
||||
private static String pk ;
|
||||
private static String sk ;
|
||||
private SydApi api;
|
||||
|
||||
|
||||
|
||||
public int init() throws Throwable {
|
||||
SydApiBuilder builder = new SydApiBuilder().setIp("192.168.1.123");
|
||||
api = builder.build();
|
||||
|
||||
this.pk = api.SM2GetPublicKeyC("123", 0 );
|
||||
this.sk = api.SM2GetPrivateKeyC("123");
|
||||
|
||||
return 0;
|
||||
}//end of init
|
||||
|
||||
|
||||
public int action() throws Throwable {
|
||||
lr.start_transaction("auto");//设置事务开始点
|
||||
|
||||
try {
|
||||
|
||||
System.out.println("begin SYD_Sign_P1");
|
||||
byte[] sign = this.api.SM2SignC( 0, uid , sk, Util.hexString2Bytes(pk), Util.hexString2Bytes(inHexData) );
|
||||
System.out.println("sign=" + Util.bytes2HexString( sign ));
|
||||
|
||||
System.out.println("begin SYD_Verify_P1");
|
||||
boolean pass = api.SM2Verify( uid, 1, Util.hexString2Bytes(pk), Util.hexString2Bytes(inHexData), sign );
|
||||
System.out.println("验签结果 " + pass);
|
||||
System.out.println("end SYD_Verify_P1");
|
||||
|
||||
} finally {
|
||||
lr.end_transaction("auto", lr.AUTO);//设置事务结束点
|
||||
}
|
||||
|
||||
return 0;
|
||||
}//end of action
|
||||
|
||||
|
||||
public int end() throws Throwable {
|
||||
return 0;
|
||||
}//end of end
|
||||
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
|
||||
Actions actions = new Actions();
|
||||
actions.init();
|
||||
actions.action();
|
||||
actions.end();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,203 +0,0 @@
|
||||
package com.sunyard.sydapi.test;
|
||||
|
||||
import com.sunyard.RetWrap;
|
||||
import com.sunyard.SydApi;
|
||||
import com.sunyard.proto.Util;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import racal.sunyard.main.SydApi4j;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
import static com.sunyard.sydapi.util.PrintUtil.printByteArray;
|
||||
import static com.sunyard.sydapi.util.PrintUtil.printRetWrap;
|
||||
|
||||
/**
|
||||
* 农行北分海淀支行-军队资金集中收付系统
|
||||
*/
|
||||
public class ABCNorthBranch {
|
||||
|
||||
SydApi4j api;
|
||||
|
||||
// SM2密钥索引
|
||||
String SM2PrivateKeyTag = "111111111111115100000000001";
|
||||
// SM2公钥
|
||||
String SM2PublicKey = null;
|
||||
// SM2私钥
|
||||
String SM2PrivateKey = null;
|
||||
|
||||
// SM4密钥索引
|
||||
String SM4PrivateKeyTag = "000000000000000100011111111";
|
||||
// SM4密钥
|
||||
String SM4Key = null;
|
||||
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
||||
api = (SydApi4j) new SydApi4j().connect("61.164.40.82", 8889, null, 5000);
|
||||
|
||||
SM2PublicKey = api.SM2GetPublicKeyC(SM2PrivateKeyTag);
|
||||
SM2PrivateKey = api.SM2GetPrivateKeyC(SM2PrivateKeyTag);
|
||||
|
||||
|
||||
SM4Key = new String((byte[]) api.readKey(SM4PrivateKeyTag).get("key"));
|
||||
|
||||
System.out.println("=========================================密钥获取完成=========================================");
|
||||
}
|
||||
|
||||
@After
|
||||
public void after() {
|
||||
try {
|
||||
this.api.disconnect();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// A4
|
||||
@Test
|
||||
public void testComposeKey() {
|
||||
String[] strs = {"583034303438393445374238444531323942344646344546393838394535364132", "583934443335443836454631463332313344353543343738323246383744333835"};
|
||||
RetWrap rw = api.composeKey("000", strs);
|
||||
|
||||
printRetWrap(rw);
|
||||
}
|
||||
|
||||
|
||||
// 测试通过
|
||||
// 74
|
||||
@Test
|
||||
public void testSM2SignC2() {
|
||||
String thisOrgData = "FFFFFFFFFFFFFFFF";
|
||||
|
||||
String s = api.SM2SignC(1, SM2PrivateKeyTag, Util.hexString2Bytes(SM2PublicKey), thisOrgData.getBytes());
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// 74
|
||||
@Test
|
||||
public void testSM2SignC3() {
|
||||
String thisOrgData = "FFFFFFFFFFFFFFFF";
|
||||
|
||||
String s = api.SM2SignC(0, SM2PrivateKey, Util.hexString2Bytes(SM2PublicKey), thisOrgData.getBytes());
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
// 75
|
||||
@Test
|
||||
public void testSM2SignAndVerify() {
|
||||
String orgData = "11111111111111";
|
||||
|
||||
// 这里,不管是签名还是验签,都需要在外部做一次hash。
|
||||
byte[] hash = api.SM3Hash(SM2PublicKey, orgData.getBytes());
|
||||
|
||||
/**
|
||||
* 用私钥签名(外部私钥)
|
||||
* @param nOrgDataType 签名数据类型,0 : hash 1 : 原数据
|
||||
* @param privateKey LMK34-35 下加密的私钥
|
||||
* @param orgData “签名数据类型”为 0 时:HASH 值。“签名数据类型”为 1 时:要签名的信息
|
||||
* @return 签名的 base64 编码
|
||||
*/
|
||||
String s = api.SM2Sign(SydApi.DATA_HASH, Util.hexString2Bytes(SM2PrivateKey), hash);
|
||||
|
||||
// 这里的签名结果已经符合条件了,不需要做转换
|
||||
System.out.println("签名结果: " + s);
|
||||
System.out.println("decBase64:" + Util.bytes2HexString(Util.decodeBase64(s)));
|
||||
/**
|
||||
* 用指定的公钥对指定的原始数据进行数字签名验证(PKCS#1)
|
||||
* @param nOrgDataType 签名数据类型,SydApi.DATA_ORIGIN(1) 原始数据 SydApi.DATA_HASH(0) : hash 后的类型
|
||||
* @param publicKey 公钥串,DER编码,HEX格式
|
||||
* @param orgData 待签名的原始数据,“签名数据类型”为 0 时:HASH 值。“签名数据类型”为 1 时:要签名的信息
|
||||
* @param base64Sign 签名数据 (DER 编码 + Base64 编码)
|
||||
* @return 验签是否通过
|
||||
*/
|
||||
boolean b = api.SM2Verify(SydApi.DATA_HASH, Util.hexString2Bytes(SM2PublicKey), hash, s);
|
||||
System.out.println("验签结果: " + b);
|
||||
}
|
||||
|
||||
|
||||
// 测试通过
|
||||
// 75
|
||||
@Test
|
||||
public void testSM2Verify3() {
|
||||
|
||||
String thisOrgData = "FFFFFFFFFFFFFFFF";
|
||||
|
||||
String s = api.SM2SignC(1, SM2PrivateKeyTag, Util.hexString2Bytes(SM2PublicKey), thisOrgData.getBytes());
|
||||
|
||||
System.out.println(s);
|
||||
boolean b = api.SM2Verify(Util.hexString2Bytes(SM2PublicKey), thisOrgData.getBytes(), s);
|
||||
System.out.println(b);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// E0
|
||||
@Test
|
||||
public void testSM4EncryptDataCBC() {
|
||||
String msg = "1111111111111111111111";
|
||||
byte[] bytes = api.SM4EncryptDataCBC(0, SM4Key, 0, 0, msg.getBytes());
|
||||
printByteArray(bytes);
|
||||
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// E0
|
||||
@Test
|
||||
public void testSM4DecryptDataCBC() {
|
||||
String msg = "11111111111";
|
||||
byte[] bytes = api.SM4EncryptDataCBC(0, SM4Key, 0, 0, msg.getBytes());
|
||||
|
||||
byte[] bytes1 = api.SM4DecryptDataCBC(0, SM4Key, 0, 0, bytes);
|
||||
|
||||
printByteArray(bytes1, false);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// E0
|
||||
@Test
|
||||
public void testSM4EncryptFileDataCBC() {
|
||||
|
||||
String inFilePath = "C:\\Users\\edgar\\Desktop\\fsdownload\\test\\in1.txt";
|
||||
String outFilePath = "C:\\Users\\edgar\\Desktop\\fsdownload\\test\\in2.txt";
|
||||
boolean b = api.SM4EncryptFileDataCBC(0, SM4Key, 0, 0, inFilePath, outFilePath);
|
||||
System.out.println(b);
|
||||
}
|
||||
|
||||
|
||||
// 测试通过
|
||||
// E0
|
||||
@Test
|
||||
public void testSM4DecryptFileDataCBC() throws UnsupportedEncodingException {
|
||||
|
||||
String outFilePath = "C:\\Users\\edgar\\Desktop\\fsdownload\\test\\in2.txt";
|
||||
String outFilePath2 = "C:\\Users\\edgar\\Desktop\\fsdownload\\test\\out1.txt";
|
||||
boolean b = api.SM4DecryptFileDataCBC(0, SM4Key, 0, 0, outFilePath, outFilePath2);
|
||||
System.out.println(b);
|
||||
}
|
||||
|
||||
// LF
|
||||
@Test
|
||||
public void testReadKey() {
|
||||
RetWrap retWrap = api.readKey(SM4PrivateKeyTag);
|
||||
printRetWrap(retWrap);
|
||||
}
|
||||
|
||||
// QQ
|
||||
@Test
|
||||
public void testSM2GetPublicKeyC3() {
|
||||
|
||||
String s = api.SM2GetPublicKeyC(SM2PrivateKeyTag);
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
// QP
|
||||
@Test
|
||||
public void testSM2GetPrivateKeyC() {
|
||||
String s = api.SM2GetPrivateKeyC(SM2PrivateKeyTag);
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.sunyard.sydapi.test;
|
||||
|
||||
|
||||
import com.sunyard.proto.Util;
|
||||
import racal.sunyard.main.SydApi4j;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class TestBatchData {
|
||||
public static void main(String[] args) throws UnsupportedEncodingException {
|
||||
|
||||
System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
||||
SydApi4j.SYD_Set_Parameter_HA("unkown", "192.168.0.200", 8889, 3000);
|
||||
|
||||
SydApi4j api = (SydApi4j) new SydApi4j();//.connect("wuyong.fullstack.club" , 8889, null,1000);
|
||||
|
||||
|
||||
String[] keys = new String[]{"SK000000000000000000A00000000"};
|
||||
List<byte[]> pINdata = new ArrayList<byte[]>();
|
||||
pINdata.add("12345678".getBytes());
|
||||
|
||||
List<byte[]> datas = api.SYMEncryptDecryptBatchData(0x8, keys, 1, pINdata);
|
||||
|
||||
// boolean pass = api.SM2Verify( 1, Util.hexString2Bytes(pk), Util.hexString2Bytes(inHexData), sign );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
package com.sunyard.sydapi.test;
|
||||
|
||||
|
||||
import com.sunyard.RetWrap;
|
||||
import com.sunyard.SydApi;
|
||||
import com.sunyard.proto.Util;
|
||||
import racal.sunyard.main.SydApi4j;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class TestICBC {
|
||||
public static void main(String[] args) throws UnsupportedEncodingException {
|
||||
|
||||
|
||||
SydApi4j.SYD_Set_Parameter_HA("unkown", "172.16.17.162", 8889 , 1000);
|
||||
|
||||
SydApi4j api = (SydApi4j) new SydApi4j();//.connect("wuyong.fullstack.club" , 8889, null,1000);
|
||||
|
||||
|
||||
RetWrap r = api.SYD_SM2GenKeyPair();
|
||||
System.out.println( r.get("publicKey") );
|
||||
System.out.println( r.get("privateKey") );
|
||||
|
||||
// System.out.println("\n\n----- 获取公钥 () -----");
|
||||
// String pk = api.SYD_SM2_Get_Id_PublicKey_HA("1234", 0);
|
||||
// System.out.print("\r\n公钥 : ");
|
||||
// System.out.println( pk );
|
||||
//
|
||||
//
|
||||
//// pk = api.SYD_SM2_Get_Dn_PublicKey_HA("C=CN,O=Sunyard,OU=8001,CN=8001".getBytes("GBK"), 0);
|
||||
//// System.out.print("\r\n公钥 : ");
|
||||
//// System.out.println( pk );
|
||||
//
|
||||
//
|
||||
// // 签名
|
||||
String data="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>";
|
||||
final String testPubKey = "0342000480C7E165DB8CF019509CE359C4FE06535275F5F63ADDC3955721FA4802BDC8701661554096BBC190916F39073C3763C8BEF6ABDF7A042C9F1D12B6BD6E5E2614";
|
||||
// System.out.println("\n\n----- SM2 算法签名 (原始数据) -----");
|
||||
// String sign = api.SYD_SM2_Sign_HA(
|
||||
// "1234",
|
||||
// SydApi.DATA_ORIGIN,
|
||||
// Util.hexString2Bytes(testPubKey),
|
||||
// data.getBytes()
|
||||
// );
|
||||
// System.out.print("\n\n----- SM2 算法签名 结果: ");
|
||||
// System.out.println( sign );
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// System.out.println("\n\n----- SM2 算法验签 (原始数据) -----");
|
||||
// boolean SM2VerifyRet = api.SYD_SM2_Verify_HA(
|
||||
// SydApi.DATA_ORIGIN,
|
||||
// Util.hexString2Bytes(testPubKey), data.getBytes() , sign);
|
||||
// if (SM2VerifyRet){
|
||||
// System.out.println("验签通过");
|
||||
// }else{
|
||||
// System.err.println("验签失败");
|
||||
// throw new RuntimeException("验签失败");
|
||||
// }
|
||||
|
||||
|
||||
|
||||
// // 签名验签 2
|
||||
// System.out.println("\n\n----- 签名验签 2 -----");
|
||||
// String sign2 = api.SYD_SM2_DetachedSign_HA( data.getBytes() , "C=CN,O=Sunyard,OU=8001,CN=8001");
|
||||
// System.out.println( sign2 );
|
||||
|
||||
|
||||
// String sign2 = "MIICpQYKKoEcz1UGAQQCAqCCApUwggKRAgEBMQ4wDAYIKoEcz1UBgxEFADAMBgoqgRzPVQYBBAIBoIIByTCCAcUwggFooAMCAQICCDyTAAAAAAAlMAwGCCqBHM9VAYN1BQAwJjELMAkGA1UEBhMCQ04xFzAVBgNVBAMMDlN1bnlhcmRTTTJSb290MB4XDTE3MDUxNzAyNTI0NFoXDTIyMDcyNTAyNTI0NFowPTELMAkGA1UEBhMCQ04xEDAOBgNVBAoMB1N1bnlhcmQxDTALBgNVBAsMBDgwMDExDTALBgNVBAMMBDgwMDEwWTATBgcqhkjOPQIBBggqgRzPVQGCLQNCAAQ+2joQe3zDXMAFx/vwu05rQ6B+4U42BgPSLsoqYAstD6AwVdQA0SQDAPVGSLhJ0T9QszD/FDUbVkWxNoaCCmW7o2cwZTAOBgNVHQ8BAf8EBAMCAMAwEwYDVR0lBAwwCgYIKwYBBQUHAwEwHwYDVR0jBBgwFoAU93/arv8hxtk6eq2g+6z+AqVO1cEwHQYDVR0OBBYEFAMWZaY2AwaQTNiFLpsa0dyCzBZ6MAwGCCqBHM9VAYN1BQADSQAwRgIhAIl2ZHGfln5hedcousARzZ2yeUjUbxSFMmhMmr5t6Q6nAiEAvqYX/KpIcvnPyB/Yiil+oq6DFdVM6P3vDxBhU/8taXExgaAwgZ0CAQEwMjAmMQswCQYDVQQGEwJDTjEXMBUGA1UEAwwOU3VueWFyZFNNMlJvb3QCCDyTAAAAAAAlMAwGCCqBHM9VAYMRBQAwDQYJKoEcz1UBgi0BBQAERzBFAiATwLIYiylt7xHmBkH6EclrchClxzLlKWC5guxxzkr58gIhANSw+owu8cyypdJtHGrGqjSiB1BVzGva+irUHelx/GbC";
|
||||
// sign2 = "MIID7gYKKoEcz1UGAQQCAqCCA94wggPaAgEBMQ4wDAYIKoEcz1UBgxEFADAMBgoqgRzPVQYBBAIBoIIC2jCCAtYwggJ6oAMCAQICBUACgAF0MAwGCCqBHM9VAYN1BQAwYTELMAkGA1UEBhMCQ04xMDAuBgNVBAoMJ0NoaW5hIEZpbmFuY2lhbCBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTEgMB4GA1UEAwwXQ0ZDQSBBQ1MgVEVTVCBTTTIgT0NBMzEwHhcNMTcxMTE2MDY1NDQ1WhcNMjIxMTE2MDY1NDQ1WjCBgzELMAkGA1UEBhMCQ04xHDAaBgNVBAoME0NGQ0EgVEVTVCBTTTIgT0NBMzExDjAMBgNVBAsMBVNIQ1BFMRkwFwYDVQQLDBBPcmdhbml6YXRpb25hbC0xMSswKQYDVQQDDCIwNTFA5LiK5rW356Wo5o2u5Lqk5piT5omAQEFBQUFBQUA4MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEYPA9UQRL/ZaThEN0nc3SwkOHw4hPDXG9hZeKiBnNdhgPy1EtX4My2bpNFpYMmQ0nvkJUM4qpJrF7MSqEws6rLaOB+TCB9jA/BggrBgEFBQcBAQQzMDEwLwYIKwYBBQUHMAGGI2h0dHA6Ly9vY3NwdGVzdC5jZmNhLmNvbS5jbjo4MC9vY3NwMB8GA1UdIwQYMBaAFATHvPlZAWk+jDQ2IGIYPN68tbsMMAwGA1UdEwEB/wQCMAAwNgYDVR0fBC8wLTAroCmgJ4YlaHR0cDovLzIxMC43NC40Mi4zL09DQTMxL1NNMi9jcmwxLmNybDAOBgNVHQ8BAf8EBAMCBsAwHQYDVR0OBBYEFKJnS7XYRDWG9eqXtSzUZXK+De2/MB0GA1UdJQQWMBQGCCsGAQUFBwMCBggrBgEFBQcDBDAMBggqgRzPVQGDdQUAA0gAMEUCIGaSRoaUSpoH4lW3at1ABluiNwFDH21jHesHmy0F5gjqAiEA0sUykW9E36F3qQ91YDvasQg8EnOMp1idhgt6pq/NUAYxgdgwgdUCAQEwajBhMQswCQYDVQQGEwJDTjEwMC4GA1UECgwnQ2hpbmEgRmluYW5jaWFsIENlcnRpZmljYXRpb24gQXV0aG9yaXR5MSAwHgYDVQQDDBdDRkNBIEFDUyBURVNUIFNNMiBPQ0EzMQIFQAKAAXQwDAYIKoEcz1UBgxEFADANBgkqgRzPVQGCLQEFAARHMEUCIQDH3ePzq4nYJ8pAodJyqPKqCQuQwmOpyj/rWcDILzFj2gIgUW5c9W6jD7u91opMcDwaw4aNEvEtGX6ks5XDIMIkUjI=";
|
||||
//
|
||||
//
|
||||
//
|
||||
// boolean pass = api.SYD_SM2_DetachedVerify_HA( "<MainBody><MsgId><Id>000000000000000201809181000008627</Id><CreDtTm>2018-09-17T19:06:39</CreDtTm></MsgId><CertNtfctnInf><ChngTp>CC00</ChngTp></CertNtfctnInf></MainBody>".getBytes(), sign2 );
|
||||
// System.out.println( pass );
|
||||
//
|
||||
// // 获取公钥
|
||||
// X509 x = api.SYD_Get_X509_CertInfo_HA( "<MainBody><MsgId><Id>000000000000000201809181000008627</Id><CreDtTm>2018-09-17T19:06:39</CreDtTm></MsgId><CertNtfctnInf><ChngTp>CC00</ChngTp></CertNtfctnInf></MainBody>".getBytes(), sign2 );
|
||||
// System.out.println( Util.bytes2HexString( x.getPubKey() ) );
|
||||
|
||||
byte[]ttt = new byte[40960];
|
||||
Arrays.fill(ttt, (byte) 0x88);
|
||||
String sign2 = api.SYD_SM2_Sign_HA(
|
||||
"1234",
|
||||
SydApi.DATA_HASH,
|
||||
Util.hexString2Bytes("0342000480C7E165DB8CF019509CE359C4FE06535275F5F63ADDC3955721FA4802BDC8701661554096BBC190916F39073C3763C8BEF6ABDF7A042C9F1D12B6BD6E5E2614"),
|
||||
ttt
|
||||
);
|
||||
System.out.print("\n\n----- SM2 算法签名 结果: ");
|
||||
System.out.println( sign2 );
|
||||
|
||||
System.out.println( api.SYD_SM2_Verify_HA(
|
||||
SydApi.DATA_HASH,
|
||||
Util.hexString2Bytes("0342000480C7E165DB8CF019509CE359C4FE06535275F5F63ADDC3955721FA4802BDC8701661554096BBC190916F39073C3763C8BEF6ABDF7A042C9F1D12B6BD6E5E2614"),
|
||||
ttt ,
|
||||
sign2
|
||||
) );
|
||||
|
||||
|
||||
|
||||
String sign3 = api.SYD_SM2_Sign_HA(
|
||||
"1234",
|
||||
SydApi.DATA_ORIGIN,
|
||||
Util.hexString2Bytes("0342000480C7E165DB8CF019509CE359C4FE06535275F5F63ADDC3955721FA4802BDC8701661554096BBC190916F39073C3763C8BEF6ABDF7A042C9F1D12B6BD6E5E2614"),
|
||||
"<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>".getBytes()
|
||||
);
|
||||
System.out.print("\n\n----- SM2 算法签名 结果: ");
|
||||
System.out.println( sign3 );
|
||||
|
||||
System.out.println( api.SYD_SM2_Verify_HA(
|
||||
SydApi.DATA_ORIGIN,
|
||||
Util.hexString2Bytes("0342000480C7E165DB8CF019509CE359C4FE06535275F5F63ADDC3955721FA4802BDC8701661554096BBC190916F39073C3763C8BEF6ABDF7A042C9F1D12B6BD6E5E2614"),
|
||||
"<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>".getBytes() ,
|
||||
sign3
|
||||
) );
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,457 +0,0 @@
|
||||
package com.sunyard.sydapi.test;
|
||||
|
||||
import com.sunyard.RetWrap;
|
||||
import com.sunyard.SydApi;
|
||||
import com.sunyard.cert.X509;
|
||||
import com.sunyard.proto.Util;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import racal.sunyard.main.SydApi4j;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 新疆联通政务云
|
||||
*/
|
||||
public class TestXJ {
|
||||
|
||||
SydApi4j api;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
// 通过设置debug的属性开启debug,如果不设置则默认不打开
|
||||
System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
||||
api = (SydApi4j) new SydApi4j().connect("192.168.0.100", 8889, null, 1000);
|
||||
}
|
||||
|
||||
@After
|
||||
public void after() {
|
||||
try {
|
||||
this.api.disconnect();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// 8003
|
||||
@Test
|
||||
public void testSYD_NakedSign() {
|
||||
String dn = "C=CN,O=Sunyard,OU=8001,CN=8001";
|
||||
byte[] bytes = api.SYD_NakedSign("111".getBytes(), dn);
|
||||
System.out.println(Util.bytes2HexString(bytes));
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// 8004
|
||||
@Test
|
||||
public void testSYD_NakedVerify() {
|
||||
String dn = "C=CN,O=Sunyard,OU=8001,CN=8001";
|
||||
String orgData = "111";
|
||||
byte[] bytes = api.SYD_NakedSign(orgData.getBytes(), dn);
|
||||
String sign = Util.bytes2HexString(bytes);
|
||||
System.out.println(sign);
|
||||
|
||||
boolean b = api.SYD_NakedVerify(orgData.getBytes(), bytes, dn);
|
||||
System.out.println(b);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// 6C
|
||||
@Test
|
||||
public void testNetSign() {
|
||||
String sn = "1004282451";
|
||||
String s = api.netSign(sn, "111".getBytes());
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// 6D
|
||||
@Test
|
||||
public void testNetVerify() {
|
||||
String sn = "1004282451";
|
||||
String data = "111";
|
||||
String s = api.netSign(sn, data.getBytes());
|
||||
|
||||
boolean b = api.netVerify(sn, data.getBytes(), s);
|
||||
System.out.println(b);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// 80
|
||||
@Test
|
||||
public void testSM2SignCustomized() {
|
||||
String sn = "1004282452";
|
||||
String s = api.SM2SignCustomized(sn, "111".getBytes());
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// 81
|
||||
@Test
|
||||
public void testSM2VerifyCustomized() {
|
||||
String sn = "1004282451";
|
||||
String data = "111";
|
||||
String s = api.SM2SignCustomized(sn, data.getBytes());
|
||||
|
||||
boolean b = api.SM2VerifyCustomized(sn, data.getBytes(), s);
|
||||
System.out.println(b);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// 74
|
||||
@Test
|
||||
public void testSM2SignC() {
|
||||
String userId = "1111111111111113";
|
||||
String certNo = "1234567812345678";
|
||||
String thisPublicKey = "03420004549B6A1A09A5D0D9432C6829D443F85ED8785340FC1AB14118BB7637830C808F7A888DF0C4F988EA6FF2EE6C76ECD00C4B7CEC92CC8EA365F8EA291041CAB231";
|
||||
String thisOrgData = "FFFFFFFFFFFFFFFF";
|
||||
|
||||
String s = api.SM2SignC(userId, certNo, 1, Util.hexString2Bytes(thisPublicKey), thisOrgData.getBytes());
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// 75
|
||||
@Test
|
||||
public void testSM2Verify() {
|
||||
|
||||
String userId = "1111111111111113";
|
||||
String certNo = "1234567812345678";
|
||||
String thisPublicKey = "03420004549B6A1A09A5D0D9432C6829D443F85ED8785340FC1AB14118BB7637830C808F7A888DF0C4F988EA6FF2EE6C76ECD00C4B7CEC92CC8EA365F8EA291041CAB231";
|
||||
String thisOrgData = "FFFFFFFFFFFFFFFF";
|
||||
|
||||
String s = api.SM2SignC(userId, certNo, 1, Util.hexString2Bytes(thisPublicKey), thisOrgData.getBytes());
|
||||
|
||||
System.out.println(s);
|
||||
boolean b = api.SM2Verify(certNo, 1, Util.hexString2Bytes(thisPublicKey), thisOrgData.getBytes(), s);
|
||||
System.out.println(b);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// 8005
|
||||
@Test
|
||||
public void testSYD_DetachedSign() {
|
||||
|
||||
String orgData = "3214312341243214";
|
||||
String DN = "C=CN,O=Sunyard,OU=8001,CN=8001";
|
||||
String s = api.SYD_DetachedSign(orgData.getBytes(), DN);
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// 8006
|
||||
@Test
|
||||
public void testSYD_DetachedVerify() {
|
||||
String orgData = "3214312341243214";
|
||||
String DN = "C=CN,O=Sunyard,OU=8001,CN=8001";
|
||||
String sign = api.SYD_DetachedSign(orgData.getBytes(), DN);
|
||||
System.out.println("签名结果" + sign);
|
||||
|
||||
boolean b = api.SYD_DetachedVerify(orgData.getBytes(), sign);
|
||||
System.out.println("验签结果:" + b);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// 8005
|
||||
@Test
|
||||
public void testSm3AndDetachedSign() {
|
||||
String publicKey = "0342000469DE3847DD6A66DA96DB8F2DCFA32E10E680FF2EF4A592079E7C3A44320BB15084E7CA9782A39B6E28F07C04FE00D68958A2605921E71712D56C1E4435A39FA5";
|
||||
String orgData = "FFFFFFFFFFFFFFFF";
|
||||
String testCertNo = "C=CN,O=Sunyard,OU=8001,CN=8001";
|
||||
|
||||
String sign = api.sm3AndDetachedSign(orgData.getBytes(), publicKey, testCertNo);
|
||||
System.out.println("签名结果: " + sign);
|
||||
|
||||
boolean b = api.sm3AndDetachedVerify(orgData.getBytes(), publicKey, sign);
|
||||
System.out.println("验签结果:" + b);
|
||||
}
|
||||
|
||||
|
||||
// 测试通过
|
||||
// 8005
|
||||
@Test
|
||||
public void testDetachedSign() {
|
||||
String publicKey = "0342000469DE3847DD6A66DA96DB8F2DCFA32E10E680FF2EF4A592079E7C3A44320BB15084E7CA9782A39B6E28F07C04FE00D68958A2605921E71712D56C1E4435A39FA5";
|
||||
String orgData = "FFFFFFFFFFFFFFFF";
|
||||
String testCertNo = "C=CN,O=Sunyard,OU=8001,CN=8001";
|
||||
|
||||
byte[] hash = api.SM3Hash(publicKey, orgData.getBytes());
|
||||
|
||||
String sign = api.detachedSign(
|
||||
SydApi.DATA_HASH,
|
||||
hash,
|
||||
testCertNo
|
||||
);
|
||||
|
||||
System.out.println("\t签名结果: " + sign);
|
||||
|
||||
boolean verify = api.detachedVerify(
|
||||
SydApi.DATA_HASH,
|
||||
hash,
|
||||
sign
|
||||
);
|
||||
|
||||
System.out.println("\t验签结果: " + verify);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// 72
|
||||
@Test
|
||||
public void testSM2GenSessionKey() {
|
||||
|
||||
String pk="034200041CD47354867E12B9D138F098413C47D42EDEB2AD402EAB431C0C7AC2FE2D6DE9C67273C643C709F2A749188941B7FA7F9FF14F8E7D19316936DB4D4954FF3819";
|
||||
|
||||
RetWrap retWrap = api.SM2GenSessionKey(pk);
|
||||
printRetWrap(retWrap);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// 73
|
||||
@Test
|
||||
public void testSM2ConfirmSessionKeyC() {
|
||||
|
||||
String userId = "1111111111111147";
|
||||
|
||||
String pk="0342000422C4FC907E0BEC600C2070D675EFB65EAE3A628260FF5461DD5F8133CF7CF98530EF392D016B6487BA6148E9AC27BDD4E8D7404A310CB09335F50AEF00ADB068";
|
||||
|
||||
RetWrap retWrap = api.SM2GenSessionKey(pk);
|
||||
printRetWrap(retWrap);
|
||||
|
||||
String cipherKey = (String) retWrap.get("CipherKey");
|
||||
|
||||
RetWrap retWrap1 = api.SM2ConfirmSessionKeyC(userId, cipherKey);
|
||||
printRetWrap(retWrap1);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// E0
|
||||
@Test
|
||||
public void testSYMEncryptData() {
|
||||
String sessionKey = "XF356BCBA3204F6E6F356BCBA3204F6E6";
|
||||
String pInData = "1111111111111111";
|
||||
|
||||
byte[] bytes = api.SYMEncryptData(3,sessionKey, pInData.getBytes());
|
||||
byte[] bytes1 = api.SYMDecryptData(3, sessionKey, bytes);
|
||||
System.out.println(new String(bytes1));
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// E0
|
||||
@Test
|
||||
public void testEncDecData() {
|
||||
|
||||
String msg = "1111111111111111111111";
|
||||
|
||||
String key = "X3C9003C47A13379011733681ABB4A4D0";
|
||||
|
||||
byte[] bytes = api.SYMEncryptDataCBC(1, key, msg.getBytes());
|
||||
|
||||
System.out.println("输出为: " + Util.bytes2HexString(bytes));
|
||||
|
||||
byte[] bytes1 = api.SYMDecryptDataCBC(1, key, bytes);
|
||||
System.out.println("明文:" + new String(bytes1));
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// 6B
|
||||
@Test
|
||||
public void testDecryptDE() {
|
||||
String sn = "1004282452";
|
||||
RetWrap retWrap = api.generateDE(sn, (byte) 0x30);
|
||||
String sessionKey = (String) retWrap.get("SessionKey");
|
||||
String CipherKey = (String) retWrap.get("CipherKey");
|
||||
String KCV = (String) retWrap.get("KCV");
|
||||
|
||||
RetWrap retWrap1 = api.decryptDE(sn, CipherKey);
|
||||
printRetWrap(retWrap1);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// 7E
|
||||
@Test
|
||||
public void testSYD_SM2_Get_Dn_PublicKey_HA() {
|
||||
String dn = "C=CN,O=Sunyard,OU=8001,CN=8001";
|
||||
String s = api.SYD_SM2_Get_Dn_PublicKey_HA(dn.getBytes(), 0);
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// 6E
|
||||
@Test
|
||||
public void testGetX509SN() {
|
||||
String dn = "C=CN,ST=ZJ,L=HZ,O=SUNYARD,OU=123456,CN=111";
|
||||
String s = api.getX509SN(0, dn.getBytes());
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// 7E
|
||||
@Test
|
||||
public void testGetDNPublicKey() {
|
||||
String dn = "C=CN,O=Sunyard,OU=8001,CN=8001";
|
||||
String s = api.GetDNPublicKey(0, dn.getBytes());
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// QQ
|
||||
@Test
|
||||
public void testSM2GetPublicKeyC() {
|
||||
String userId = "1111111111111117";
|
||||
|
||||
String s = api.SM2GetPublicKeyC(userId, 0);
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// 8016
|
||||
@Test
|
||||
public void testGetX509CertInfo() {
|
||||
|
||||
String pCert = "-----BEGIN CERTIFICATE-----\u0007MIIC1jCCAnqgAwIBAgIFQAKAAXQwDAYIKoEcz1UBg3UFADBhMQswCQYDVQQGEwJD\u0007TjEwMC4GA1UECgwnQ2hpbmEgRmluYW5jaWFsIENlcnRpZmljYXRpb24gQXV0aG9y\u0007aXR5MSAwHgYDVQQDDBdDRkNBIEFDUyBURVNUIFNNMiBPQ0EzMTAeFw0xNzExMTYw\u0007NjU0NDVaFw0yMjExMTYwNjU0NDVaMIGDMQswCQYDVQQGEwJDTjEcMBoGA1UECgwT\u0007Q0ZDQSBURVNUIFNNMiBPQ0EzMTEOMAwGA1UECwwFU0hDUEUxGTAXBgNVBAsMEE9y\u0007Z2FuaXphdGlvbmFsLTExKzApBgNVBAMMIjA1MUDkuIrmtbfnpajmja7kuqTmmJPm\u0007iYBAQUFBQUFBQDgwWTATBgcqhkjOPQIBBggqgRzPVQGCLQNCAARg8D1RBEv9lpOE\u0007Q3SdzdLCQ4fDiE8Ncb2Fl4qIGc12GA/LUS1fgzLZuk0WlgyZDSe+QlQziqkmsXsx\u0007KoTCzqsto4H5MIH2MD8GCCsGAQUFBwEBBDMwMTAvBggrBgEFBQcwAYYjaHR0cDov\u0007L29jc3B0ZXN0LmNmY2EuY29tLmNuOjgwL29jc3AwHwYDVR0jBBgwFoAUBMe8+VkB\u0007aT6MNDYgYhg83ry1uwwwDAYDVR0TAQH/BAIwADA2BgNVHR8ELzAtMCugKaAnhiVo\u0007dHRwOi8vMjEwLjc0LjQyLjMvT0NBMzEvU00yL2NybDEuY3JsMA4GA1UdDwEB/wQE\u0007AwIGwDAdBgNVHQ4EFgQUomdLtdhENYb16pe1LNRlcr4N7b8wHQYDVR0lBBYwFAYI\u0007KwYBBQUHAwIGCCsGAQUFBwMEMAwGCCqBHM9VAYN1BQADSAAwRQIgZpJGhpRKmgfi\u0007Vbdq3UAGW6I3AUMfbWMd6webLQXmCOoCIQDSxTKRb0TfoXepD3VgO9qxCDwSc4yn\u0007WJ2GC3qmr81QBg==\u0007-----END CERTIFICATE-----\u0007";
|
||||
X509 x509 = api.GetX509CertInfo(pCert);
|
||||
|
||||
String path = "E:\\work\\sunyard\\证书\\sm2.q\\sm2.q.enc.crt.pem";
|
||||
X509 x509_1 = api.GetX509CertInfo(getCertStr(path));
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// 8017
|
||||
@Test
|
||||
public void testGetX509CertFileC() {
|
||||
String userId = "8001";
|
||||
|
||||
String s = api.GetX509CertFileC(userId, 0);
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// E2
|
||||
@Test
|
||||
public void testSM2GenKeyPair() {
|
||||
RetWrap retWrap = api.SM2GenKeyPair();
|
||||
printRetWrap(retWrap, true);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// QN
|
||||
@Test
|
||||
public void testSYD_SM2GenKeyPair() {
|
||||
RetWrap retWrap = api.SYD_SM2GenKeyPair();
|
||||
printRetWrap(retWrap);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// 77
|
||||
@Test
|
||||
public void testSM2Encrypt() {
|
||||
String publicKey = "03420004C51A40A094B6A14500B2980603D68CA263FF1BA238AFB17BB8B791C5F444465A749B68B4584C83E7BEF66DE2446177E14DBE0F32691F54B1636CC73334A1A94D";
|
||||
String orgData = "FFFFFFFFFFFFFFFF";
|
||||
String s = api.SM2Encrypt(Util.hexString2Bytes(publicKey), orgData.getBytes());
|
||||
|
||||
System.out.println("encData: " + s);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// 78
|
||||
@Test
|
||||
public void testSM2DecryptC() {
|
||||
String certId = "1111111111111122";
|
||||
String publicKey = "0342000469DE3847DD6A66DA96DB8F2DCFA32E10E680FF2EF4A592079E7C3A44320BB15084E7CA9782A39B6E28F07C04FE00D68958A2605921E71712D56C1E4435A39FA5";
|
||||
String orgData = "FFFFFFFFFFFFFFFF";
|
||||
String s = api.SM2Encrypt(Util.hexString2Bytes(publicKey), orgData.getBytes());
|
||||
|
||||
byte[] bytes = api.SM2DecryptC(certId, s);
|
||||
|
||||
System.out.println("decData:" + new String(bytes));
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// 76
|
||||
// 带公钥
|
||||
@Test
|
||||
public void testSM3Hash() {
|
||||
String publicKey = "0342000469DE3847DD6A66DA96DB8F2DCFA32E10E680FF2EF4A592079E7C3A44320BB15084E7CA9782A39B6E28F07C04FE00D68958A2605921E71712D56C1E4435A39FA5";
|
||||
String orgData = "FFFFFFFFFFFFFFFF";
|
||||
|
||||
byte[] hash = api.SM3Hash(publicKey, orgData.getBytes());
|
||||
|
||||
System.out.println("hash:" + Util.bytes2HexString(hash));
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// 76
|
||||
// 不带公钥
|
||||
@Test
|
||||
public void testSM3Hash2() {
|
||||
String orgData = "FFFFFFFFFFFFFFFF";
|
||||
|
||||
byte[] hash = api.SM3Hash(orgData.getBytes());
|
||||
|
||||
System.out.println("hash:" + Util.bytes2HexString(hash));
|
||||
}
|
||||
|
||||
// 工具方法
|
||||
// ==========================================
|
||||
|
||||
public static void printRetWrap(RetWrap rw) {
|
||||
printRetWrap(rw, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出
|
||||
* @param rw
|
||||
*/
|
||||
public static void printRetWrap(RetWrap rw, boolean flag) {
|
||||
for (String s : rw.keySet()) {
|
||||
|
||||
Object o = rw.get(s);
|
||||
if (o instanceof String) {
|
||||
System.out.println(s + ":" + o);
|
||||
} else if (o instanceof byte[]) {
|
||||
if (flag) {
|
||||
System.out.println(s + ": " + Util.bytes2HexString((byte[]) o));
|
||||
} else {
|
||||
System.out.println(s + ": " + new String(Util.hexString2Bytes(Util.bytes2HexString((byte[]) o))));
|
||||
}
|
||||
} else {
|
||||
System.out.println(s + ":" + o);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取证书
|
||||
public String getCertStr(String path) {
|
||||
|
||||
BufferedInputStream bis = null;
|
||||
try {
|
||||
bis = new BufferedInputStream(new FileInputStream(path));
|
||||
|
||||
StringBuilder str = new StringBuilder();
|
||||
|
||||
int len = 0;
|
||||
|
||||
while ((len = bis.read()) != -1) {
|
||||
str.append((char) len);
|
||||
}
|
||||
|
||||
return str.toString();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (bis != null) {
|
||||
try {
|
||||
bis.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package lrapi;
|
||||
|
||||
public class lr {
|
||||
|
||||
public static String AUTO = "AUTO";
|
||||
|
||||
public static void start_transaction(String t) {
|
||||
System.out.println("start_transaction" + t);
|
||||
}
|
||||
|
||||
public static void end_transaction(String t, String type) {
|
||||
System.out.println("end_transaction" + t);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user