1、添加QP指令对应的接口,修改E0,74,QQ等指令相关的接口。2、添加农行北分海淀支行-军队资金集中收付系统测试类。3、添加打印工具类及文件读取写入工具类
This commit is contained in:
@@ -0,0 +1,179 @@
|
||||
package com.sunyard.sydapi.example;
|
||||
|
||||
import com.sunyard.RetWrap;
|
||||
import com.sunyard.proto.Util;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import racal.sunyard.main.SydApi4j;
|
||||
|
||||
import static com.sunyard.sydapi.util.PrintUtil.printByteArray;
|
||||
import static com.sunyard.sydapi.util.PrintUtil.printRetWrap;
|
||||
|
||||
/**
|
||||
* 农行北分海淀支行-军队资金集中收付系统
|
||||
*/
|
||||
public class ABCNorthBranch {
|
||||
|
||||
SydApi4j api;
|
||||
|
||||
// SM2密钥索引
|
||||
String SM2PrivateKeyTag = "111111111111111800000000001";
|
||||
// SM2公钥
|
||||
String SM2PublicKey = null;
|
||||
// SM2私钥
|
||||
String SM2PrivateKey = null;
|
||||
|
||||
// SM4密钥索引
|
||||
String SM4PrivateKeyTag = "333333333333333300A00000001";
|
||||
// SM4密钥
|
||||
String SM4Key = null;
|
||||
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
||||
api = (SydApi4j) new SydApi4j().connect("192.168.1.7", 8889, null, 5000);
|
||||
|
||||
SM2PublicKey = api.SM2GetPublicKeyC(SM2PrivateKeyTag);
|
||||
SM2PrivateKey = api.SM2GetPrivateKeyC(SM2PrivateKeyTag);
|
||||
SM4Key = new String(Util.hexString2Bytes(Util.bytes2HexString((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 testSM2Verify2() {
|
||||
String thisOrgData = "FFFFFFFFFFFFFFFF";
|
||||
|
||||
String s = api.SM2SignC(0, SM2PrivateKey, Util.hexString2Bytes(SM2PublicKey), thisOrgData.getBytes());
|
||||
|
||||
System.out.println(s);
|
||||
boolean b = api.SM2Verify( Util.hexString2Bytes(SM2PublicKey), thisOrgData.getBytes(), 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() {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import com.sunyard.RetWrap;
|
||||
import com.sunyard.SydApi;
|
||||
import com.sunyard.cert.X509;
|
||||
import com.sunyard.proto.Util;
|
||||
import com.sunyard.sydapi.util.PrintUtil;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -11,6 +12,9 @@ import racal.sunyard.main.SydApi4j;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
import static com.sunyard.sydapi.util.PrintUtil.printByteArray;
|
||||
import static com.sunyard.sydapi.util.PrintUtil.printRetWrap;
|
||||
|
||||
public class TestAllMethods {
|
||||
|
||||
SydApi4j api;
|
||||
@@ -34,32 +38,6 @@ public class TestAllMethods {
|
||||
}
|
||||
}
|
||||
|
||||
public void printRetWrap(RetWrap rw) {
|
||||
printRetWrap(rw, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出
|
||||
* @param rw
|
||||
*/
|
||||
public 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) {
|
||||
|
||||
@@ -127,13 +105,14 @@ public class TestAllMethods {
|
||||
*/
|
||||
public byte[] inputStream2ByteArray(String filePath) {
|
||||
|
||||
InputStream in = null;
|
||||
BufferedInputStream in = null;
|
||||
ByteArrayOutputStream out = null;
|
||||
byte[] data = null;
|
||||
try {
|
||||
in = new FileInputStream(filePath);
|
||||
in = new BufferedInputStream(new FileInputStream(filePath));
|
||||
|
||||
out = new ByteArrayOutputStream();
|
||||
|
||||
byte[] buffer = new byte[1024 * 4];
|
||||
int n = 0;
|
||||
while ((n = in.read(buffer)) != -1) {
|
||||
@@ -616,6 +595,82 @@ public class TestAllMethods {
|
||||
printRetWrap(retWrap, true);
|
||||
}
|
||||
|
||||
// 00650000000000000000
|
||||
// 4530
|
||||
// 30
|
||||
// 38
|
||||
// 3230583643454537343433323934393942343236434545373434333239343939423432303031303030303230303030303030303030303030303030303030303030303030303030303030303030423131313131313131313131
|
||||
|
||||
// 测试通过
|
||||
// E0
|
||||
@Test
|
||||
public void testSM4EncryptDataCBC() {
|
||||
String key = "X6CEE744329499B426CEE744329499B42";
|
||||
String msg = "1111111111111111111111";
|
||||
byte[] bytes = api.SM4EncryptDataCBC(0, key, 0, 0 , msg.getBytes());
|
||||
printByteArray(bytes);
|
||||
|
||||
}
|
||||
|
||||
// 00200000000000000000
|
||||
// 4531
|
||||
// 303030
|
||||
// 303130
|
||||
// FF045D9DFA5DBE95CBC4285499719EF1
|
||||
|
||||
// 006A000000000000000245303039323058364345453734343332393439394234323643454537343433323934393942343230303130303030323030303030303030303030303030303030303030303030303030303030303030303130FF045D9DFA5DBE95CBC4285499719EF1
|
||||
// 测试通过
|
||||
// E0
|
||||
@Test
|
||||
public void testSM4DecryptDataCBC() {
|
||||
String key = "X6CEE744329499B426CEE744329499B42";
|
||||
String msg = "11111111111";
|
||||
byte[] bytes = api.SM4EncryptDataCBC(0, key, 0, 0 , msg.getBytes());
|
||||
|
||||
byte[] bytes1 = api.SM4DecryptDataCBC(0, key, 0, 0, bytes);
|
||||
|
||||
printByteArray(bytes1, false);
|
||||
}
|
||||
|
||||
// 00850000000000000000
|
||||
// 4530
|
||||
// 30
|
||||
// 38
|
||||
// 32
|
||||
// 30
|
||||
// 583643454537343433323934393942343236434545373434333239343939423432
|
||||
// 30
|
||||
// 30
|
||||
// 31
|
||||
// 30303030
|
||||
// 32
|
||||
// 3030303030303030303030303030303030303030303030303030303030303030
|
||||
// 303242
|
||||
// 31303130313131313131313131313131313131313131313030303030303030303031313131313131313131
|
||||
|
||||
// 测试通过
|
||||
// E0
|
||||
@Test
|
||||
public void testSM4EncryptFileDataCBC() {
|
||||
String key = "X6CEE744329499B426CEE744329499B42";
|
||||
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, key, 0, 0, inFilePath, outFilePath);
|
||||
System.out.println(b);
|
||||
}
|
||||
|
||||
|
||||
// 测试通过
|
||||
// E0
|
||||
@Test
|
||||
public void testSM4DecryptFileDataCBC() {
|
||||
String key = "X6CEE744329499B426CEE744329499B42";
|
||||
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, key, 0, 0, outFilePath, outFilePath2);
|
||||
System.out.println(b);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// Y0
|
||||
@Test
|
||||
@@ -642,7 +697,7 @@ public class TestAllMethods {
|
||||
// LF
|
||||
@Test
|
||||
public void testReadKey() {
|
||||
String index = "000000000000000300000000003";
|
||||
String index = "333333333333333300A00000001";
|
||||
RetWrap retWrap = api.readKey(index);
|
||||
printRetWrap(retWrap);
|
||||
}
|
||||
@@ -784,13 +839,13 @@ public class TestAllMethods {
|
||||
// E2
|
||||
@Test
|
||||
public void testSM2OrRSAGenKeyPair() {
|
||||
String index = "1111111111111115";
|
||||
String index = "1111111111111118";
|
||||
|
||||
// SM2
|
||||
// RetWrap retWrap = api.SM2OrRSAGenKeyPair(1, index, 0);
|
||||
RetWrap retWrap = api.SM2OrRSAGenKeyPair(1, index, 0);
|
||||
|
||||
// RSA
|
||||
RetWrap retWrap = api.SM2OrRSAGenKeyPair(0, index, 1024);
|
||||
// RetWrap retWrap = api.SM2OrRSAGenKeyPair(0, index, 1024);
|
||||
printRetWrap(retWrap, true);
|
||||
}
|
||||
|
||||
@@ -868,6 +923,7 @@ public class TestAllMethods {
|
||||
printRetWrap(retWrap, true);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// 60
|
||||
@Test
|
||||
public void testGetTransmissionKey() {
|
||||
@@ -1083,6 +1139,35 @@ public class TestAllMethods {
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// QQ
|
||||
@Test
|
||||
public void testSM2GetPublicKeyC2() {
|
||||
String userId = "1111111111111117";
|
||||
|
||||
String s = api.SM2GetPublicKeyC(userId, 0);
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// QQ
|
||||
@Test
|
||||
public void testSM2GetPublicKeyC3() {
|
||||
|
||||
String tag = "111111111111111800000000001";
|
||||
String s = api.SM2GetPublicKeyC(tag);
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// QP
|
||||
@Test
|
||||
public void testSM2GetPrivateKeyC() {
|
||||
String tag = "111111111111111800000000001";
|
||||
String s = api.SM2GetPrivateKeyC(tag);
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// HI
|
||||
@Test
|
||||
@@ -1175,6 +1260,71 @@ public class TestAllMethods {
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// 74
|
||||
@Test
|
||||
public void testSM2SignC2() {
|
||||
String thisPublicKey = "03420004549B6A1A09A5D0D9432C6829D443F85ED8785340FC1AB14118BB7637830C808F7A888DF0C4F988EA6FF2EE6C76ECD00C4B7CEC92CC8EA365F8EA291041CAB231";
|
||||
String thisOrgData = "FFFFFFFFFFFFFFFF";
|
||||
|
||||
String s = api.SM2SignC(1, "111111111111111300000000001", Util.hexString2Bytes(thisPublicKey), thisOrgData.getBytes());
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// 74
|
||||
@Test
|
||||
public void testSM2SignC3() {
|
||||
String privateKey = "00010000D7DBFD26A371D3B5BA58D3ECA98EAEFC1BCB65FA0DBBCFBC551E4315B8B2A8700000000000000000000000000000000000000000000000000000000000000000";
|
||||
String thisPublicKey = "034200042E624577E3BF1A510804346D1054B9205D97B4579D6528C25CAE6B74E5CDD19774AFA17CD9017E6BCBD8D9F3903492CD5288932941583E113C804537B69CDD46";
|
||||
String thisOrgData = "FFFFFFFFFFFFFFFF";
|
||||
|
||||
String s = api.SM2SignC(0, privateKey, Util.hexString2Bytes(thisPublicKey), thisOrgData.getBytes());
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
// 00C40000000000000001
|
||||
// 3735
|
||||
// 30303731
|
||||
// 3045022100E5C51AE465EB085AC0A864414617D50776F7E93F9026F5E88FD8C1DC9DB38E9F022048A6E3CBE58837012FFF4281C8B53B98DCFBD6C250F0CD153C640C53FF8D78FB
|
||||
// 31
|
||||
// 3136
|
||||
// 31323334353637383132333435363738
|
||||
// 30303136
|
||||
// 46464646464646464646464646464646
|
||||
// 30303638
|
||||
// 03420004549B6A1A09A5D0D9432C6829D443F85ED8785340FC1AB14118BB7637830C808F7A888DF0C4F988EA6FF2EE6C76ECD00C4B7CEC92CC8EA365F8EA291041CAB231
|
||||
|
||||
// 测试通过
|
||||
// 75
|
||||
@Test
|
||||
public void testSM2Verify2() {
|
||||
String privateKey = "00010000D7DBFD26A371D3B5BA58D3ECA98EAEFC1BCB65FA0DBBCFBC551E4315B8B2A8700000000000000000000000000000000000000000000000000000000000000000";
|
||||
String thisPublicKey = "034200042E624577E3BF1A510804346D1054B9205D97B4579D6528C25CAE6B74E5CDD19774AFA17CD9017E6BCBD8D9F3903492CD5288932941583E113C804537B69CDD46";
|
||||
String thisOrgData = "FFFFFFFFFFFFFFFF";
|
||||
|
||||
String s = api.SM2SignC(0, privateKey, Util.hexString2Bytes(thisPublicKey), thisOrgData.getBytes());
|
||||
|
||||
System.out.println(s);
|
||||
boolean b = api.SM2Verify( Util.hexString2Bytes(thisPublicKey), thisOrgData.getBytes(), s);
|
||||
System.out.println(b);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// 75
|
||||
@Test
|
||||
public void testSM2Verify3() {
|
||||
|
||||
String thisPublicKey = "034200045AC5014ED6A2CA0F9F79F3A34ADF4B8B92383F09E2D2F2F5B56F3D6C05B3EE43A3D65E57D3979C6CE9F6CEC5E0E30DC564416BFCB9A3E2D071559098B6B6BEF6";
|
||||
String thisOrgData = "FFFFFFFFFFFFFFFF";
|
||||
|
||||
String s = api.SM2SignC(1, "111111111111111700000000001", Util.hexString2Bytes(thisPublicKey), thisOrgData.getBytes());
|
||||
|
||||
System.out.println(s);
|
||||
boolean b = api.SM2Verify( Util.hexString2Bytes(thisPublicKey), thisOrgData.getBytes(), s);
|
||||
System.out.println(b);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// 75
|
||||
@Test
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.sunyard.sydapi.util;
|
||||
|
||||
import com.sunyard.RetWrap;
|
||||
import com.sunyard.proto.Util;
|
||||
|
||||
public class PrintUtil {
|
||||
|
||||
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 static void printByteArray(byte[] bytes) {
|
||||
printByteArray(bytes, true);
|
||||
}
|
||||
|
||||
public static void printByteArray(byte[] bytes, boolean flag) {
|
||||
if (flag) {
|
||||
System.out.println("输出为: " + Util.bytes2HexString(bytes));
|
||||
} else {
|
||||
System.out.println("输出为: " + new String(Util.hexString2Bytes(Util.bytes2HexString(bytes))));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user