移除旧的 sample
This commit is contained in:
@@ -1,86 +0,0 @@
|
||||
package com.sunyard.sydapi.sample;
|
||||
|
||||
import com.sunyard.RetWrap;
|
||||
import com.sunyard.proto.Util;
|
||||
import racal.sunyard.main.SydApi4j;
|
||||
import racal.sunyard.main.SydApiBuilder;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class ASYMEnDeTest {
|
||||
|
||||
public static void main(String[] args) throws UnsupportedEncodingException, IllegalAccessException, InstantiationException, ClassNotFoundException {
|
||||
|
||||
System.out.println("非对称密钥加解密");
|
||||
byte[] testData = new byte[100];
|
||||
Arrays.fill(testData, (byte) 0x88);
|
||||
|
||||
SydApi4j api = (SydApi4j) new SydApiBuilder()
|
||||
.setIp("172.16.17.161")
|
||||
.useDebugMode()
|
||||
.build();
|
||||
|
||||
|
||||
RetWrap ret;
|
||||
try {
|
||||
|
||||
String rsaPk = "308189028181009AF6135A23B634E6DFE6AC1BC5E18FAAC5A4287D433C81CB520615DBD386822A20328EF064D16D4AA0ED97056BF6F387D2922834C9DAFF3BFDC5666F280EF187102A47A2CDB45E5BBE66E1F28E88335B880A7BBC5E56FCA9F460C4F2EE034393ED5258D801DC252268FE0A7A36DDFA823C133148D634E560FA3C526E18093B3D0203010001";
|
||||
String rsaSk = "0004000096E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D6429CED6878A26E7DA37C6D6E3CC99DF92CFB6208B239A6F6D452F70961413C86ABABBB97905F4D8416125D6705006E0CBBE2A55C21C5021D6D4BA6357574DAEA872F744343497FC301A0072DCB5AFFADE4E38618CF193B417465D51F55E7A05143EF22ED52B15C3D7EAB07A83442F7AA0FE0864D6FA656D35A3F8F78EE76E0623396E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D6424759813A8629F2E496E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D6429C90258F09582794C56F6F811DD07E128A0D8EF28DD2A159CBC0F638D0D1ED316BF916A04BC5AB62B95826F2387D1342EA33779526A1909FCAEB67DB6C8F1BB8550544087407462997852081DDAA22AB072EBB204A911654DC8EE988BCDAA8D36433320A1FCBB1E02932119CD6B77FFD2459B1EF170FF35FF38BA957259FBB5396E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D642F13FEACF9F953255F45DB33FFA13DABC74A15EFDC5BB956C94590170BB2473B1F381564524E001284FF13325E0E123E68C5ACC3ACF396B15DFF3CF22C24D719696E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D642E8A95E58CBA24739343ECA89FDAB1C0D16C5353ECD90C364667B1B7E3B3D8698F3E0EE4874004D7100AA57E7F2778C76DE4D0E2F79B337C06DD2E407B80351DC96E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D642ECA9E92F835FAEA4AE5B73605852635FBE67D5F472BA7BE41DF94C44F472D49FAE23C3649A842C685B5CC1A5D475D8D3B46854FD28EBE9BD72921388A98BF2F196E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D642DE755DEFD7ABBDC9A95A90262084CA8E9C69123CEBF24BFC9A82114EB8E62673EA2A2BD72D321F795A2A22A4ADB5BAA42D55FC561F09D6BB0AA3518EE7BC0B8C96E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D64296E684B99F21D642487F135CC93AA0E8A97C1DA44B6706EBA95A3CCB24D9EB2E643D6B339FF7DE3983DC16C03AF3C4A22878EF976D9D2AF4549D1D9933EAC824CB32801CC4136EDF";
|
||||
|
||||
String sm2Pk = "03420004571DE0574BACC945BE003FBA94EB3FF4000BD2C64C5D81D4463099177A5E387E583DEEF7087E988E5C84E4FE19B24566A9A130D7159CE13DC8527B8341CEA3F3";
|
||||
|
||||
String sm2Sk = "00010000EBCA3A20376AAEDA2A0B607F14ADD89CE62DFF26A115FB4C987FDDDE75E78CBB0000000000000000000000000000000000000000000000000000000000000000";
|
||||
|
||||
// ret = api.SM2OrRSAGenKeyPair(1, "", 1024);
|
||||
//
|
||||
// System.out.println( ret );
|
||||
// System.out.println( Util.bytes2HexString((byte[]) ret.get("PrivateKey")) );
|
||||
// System.out.println( Util.bytes2HexString((byte[]) ret.get("PublicKey")) );
|
||||
|
||||
|
||||
byte[] enData1 = api.encryptByPk(1, sm2Pk, testData);
|
||||
System.out.println( new String(enData1) );
|
||||
|
||||
byte[] deData1 = api.decryptBySk(1, sm2Sk, enData1);
|
||||
|
||||
// deData == testData
|
||||
assert Arrays.equals( enData1, deData1 );
|
||||
System.out.println("SM2 公钥加密/私钥解密 测试通过");
|
||||
//
|
||||
//
|
||||
// byte[] enData = api.encryptByPk(0, rsaPk, testData);
|
||||
// byte[] deData = api.decryptBySk(0, rsaSk, enData);
|
||||
//
|
||||
// // deData == testData
|
||||
// assert Arrays.equals( enData, deData );
|
||||
// System.out.println("RSA 公钥加密/私钥解密 测试通过");
|
||||
|
||||
|
||||
|
||||
// byte[] enData1 = api.encryptBySk(1, sm2Sk, testData);
|
||||
// System.out.println( new String(enData1) );
|
||||
//
|
||||
// byte[] deData1 = api.decryptByPk(1, sm2Pk, enData1);
|
||||
//
|
||||
// // deData == testData
|
||||
// assert Arrays.equals( enData1, deData1 );
|
||||
// System.out.println("SM2 私钥加密/公钥解密 测试通过");
|
||||
//
|
||||
//
|
||||
// byte[] enData = api.encryptBySk(0, rsaSk, testData);
|
||||
// byte[] deData = api.decryptByPk(0, rsaPk, enData);
|
||||
//
|
||||
// // deData == testData
|
||||
// assert Arrays.equals( enData, deData );
|
||||
// System.out.println("RSA 私钥加密/公钥解密 测试通过");
|
||||
|
||||
|
||||
} finally {
|
||||
api.disconnect();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package com.sunyard.sydapi.sample;
|
||||
|
||||
public class Build {
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
package com.sunyard.sydapi.sample;
|
||||
|
||||
import com.sunyard.RetWrap;
|
||||
import com.sunyard.SydApi;
|
||||
import com.sunyard.cert.X509;
|
||||
import racal.sunyard.main.SydApiBuilder;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
/**
|
||||
* 密钥导入导出功能测试
|
||||
* 其实就是做 lmk2zmk(A8) 转加密和 zmk2lmk(A6) 转加密
|
||||
*/
|
||||
public class InputOutputKeyTest
|
||||
{
|
||||
|
||||
public static void main(String[] args) throws UnsupportedEncodingException, IllegalAccessException, InstantiationException, ClassNotFoundException {
|
||||
|
||||
System.out.println("密钥导入导出功能测试");
|
||||
|
||||
|
||||
SydApi api = new SydApiBuilder()
|
||||
.setIp("172.16.17.161")
|
||||
// .useDebugMode()
|
||||
// .useEBCDMode()
|
||||
// .useETEncrypt()
|
||||
.build();
|
||||
|
||||
try {
|
||||
|
||||
String keyType = "000";
|
||||
char sydSln = 'X';
|
||||
|
||||
|
||||
// 生成一个测试用的 zmk
|
||||
RetWrap ret = api.genSymmetricKey(
|
||||
"000",
|
||||
'Z'
|
||||
);
|
||||
|
||||
String zmk = new String((byte[]) ret.get("lmkKey"));
|
||||
System.out.println( "zmk=" + zmk);
|
||||
|
||||
|
||||
// 生成一个测试用的 key
|
||||
ret = api.genSymmetricKey(
|
||||
keyType,
|
||||
sydSln
|
||||
);
|
||||
|
||||
String key = new String((byte[]) ret.get("lmkKey"));
|
||||
System.out.println( "lmkKek=" + key);
|
||||
|
||||
|
||||
// 将 key 转加密输出
|
||||
ret = api.outputKey(keyType, sydSln, zmk, key);
|
||||
String key2 = new String((byte[]) ret.get("key"));
|
||||
System.out.println( "zmkKek=" + key2);
|
||||
|
||||
|
||||
// 将 key 重新导入
|
||||
ret = api.inputKey(keyType, sydSln, zmk, key2);
|
||||
String key3 = new String((byte[]) ret.get("key"));
|
||||
System.out.println( "*lmkKek=" + key3);
|
||||
|
||||
|
||||
}finally {
|
||||
api.disconnect();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package com.sunyard.sydapi.sample;
|
||||
|
||||
import com.sunyard.SydApi;
|
||||
import racal.sunyard.main.SydApiBuilder;
|
||||
|
||||
public class MACTest {
|
||||
|
||||
public static void main(String[] args) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
|
||||
|
||||
SydApi api = new SydApiBuilder()
|
||||
.setIp("172.16.17.162")
|
||||
// .useEBCDMode()
|
||||
.useDebugMode()
|
||||
// .useETEncrypt()
|
||||
.build();
|
||||
|
||||
|
||||
byte[] data = new byte[10];
|
||||
byte[] mac = api.MACCalc(0, 1, 0, 2, "X12345678123456781234567812345678", data);
|
||||
|
||||
System.out.println( new String( mac ) );
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,156 +0,0 @@
|
||||
package com.sunyard.sydapi.sample;
|
||||
|
||||
import com.sunyard.cert.X509;
|
||||
import com.sunyard.proto.Util;
|
||||
import racal.sunyard.main.SydApi4j;
|
||||
import racal.sunyard.main.SydApiBuilder;
|
||||
import sun.security.provider.X509Factory;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Base64;
|
||||
|
||||
public class RSAP1SignVerify {
|
||||
private static byte[] createBlock(byte type, byte[] in) {
|
||||
if (2 == type && (255 & in[0]) >= 128) {
|
||||
byte[] nin = new byte[in.length + 1];
|
||||
nin[0] = 0;
|
||||
System.arraycopy(in, 0, nin, 1, in.length);
|
||||
in = nin;
|
||||
}
|
||||
|
||||
int len = in.length;
|
||||
ByteBuffer bb;
|
||||
if (len > 128) {
|
||||
int lenLen = len / 256 + 1;
|
||||
bb = ByteBuffer.allocate(len + 1 + 1 + lenLen);
|
||||
bb.put(type);
|
||||
bb.put((byte) (128 + lenLen));
|
||||
|
||||
for (int i = 0; i < lenLen; ++i) {
|
||||
bb.put((byte) (255 & len >>> 8 * (lenLen - 1 - i)));
|
||||
}
|
||||
} else {
|
||||
bb = ByteBuffer.allocate(len + 1 + 1);
|
||||
bb.put(type);
|
||||
bb.put((byte) (255 & len));
|
||||
}
|
||||
|
||||
bb.put(in);
|
||||
return bb.array();
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
|
||||
SydApi4j api = (SydApi4j) new SydApiBuilder()
|
||||
.setIp("192.168.1.226")
|
||||
.useDebugMode()
|
||||
.build();
|
||||
|
||||
|
||||
try {
|
||||
//model 0:通过证书序列号去签名,以及验签
|
||||
//model 1:通过证书DN去签名,以及验签
|
||||
//model 2:通过证书实体的机构号获取到公钥,通过机构号以及公钥去签名,公钥去验签
|
||||
byte model = 2;
|
||||
switch (model) {
|
||||
case 2: {
|
||||
String cert = "-----BEGIN CERTIFICATE-----\n" +
|
||||
"MIIEQjCCAyqgAwIBAgIFQAeZAyIwDQYJKoZIhvcNAQELBQAwXTELMAkGA1UEBhMC\n" +
|
||||
"Q04xMDAuBgNVBAoMJ0NoaW5hIEZpbmFuY2lhbCBDZXJ0aWZpY2F0aW9uIEF1dGhv\n" +
|
||||
"cml0eTEcMBoGA1UEAwwTQ0ZDQSBBQ1MgVEVTVCBPQ0EzMTAeFw0xOTEwMjgwODU4\n" +
|
||||
"NTRaFw0yNDEwMjgwODU4NTRaMGsxCzAJBgNVBAYTAkNOMRYwFAYDVQQKDA1DRkNB\n" +
|
||||
"IE9DQTMxIENBMQ4wDAYDVQQLDAVneW5zaDEZMBcGA1UECwwQT3JnYW5pemF0aW9u\n" +
|
||||
"YWwtMzEZMBcGA1UEAwwQZ3luc2g5ODEwMDAwMTE1MDCCASIwDQYJKoZIhvcNAQEB\n" +
|
||||
"BQADggEPADCCAQoCggEBANZhyVUufcUclty+zfbbrCsjePrTRlvvXjL0gprrSK6U\n" +
|
||||
"ynyzs4h76+s3MBewAvmDcemOopddC+uvX/zfKgTzyEeyrmnee936xKXHyv1SHblW\n" +
|
||||
"a4oMPR458X2o/Ej3hHzEXqj/WdUEZdx83HeaYcdhCgoHNaKcEpiqkpIjhXtIU4Ah\n" +
|
||||
"L3ciTF7Y+0NWTi17dDI17i0C681Tjya5p0Srpw4pRy1Rb0dx6zkTGGq52RLCQk4g\n" +
|
||||
"GVWvYEn4ST4DhWZXcgTOkVBMPMNS+qXxHNy6KLi3ky6x9U+10ldg1rqG7jOtWn1E\n" +
|
||||
"HqlzcIxqdqr7iO95h5a/K0bFKiVywEq/YdlxgAbH/KUCAwEAAaOB+jCB9zA/Bggr\n" +
|
||||
"BgEFBQcBAQQzMDEwLwYIKwYBBQUHMAGGI2h0dHA6Ly9vY3NwdGVzdC5jZmNhLmNv\n" +
|
||||
"bS5jbjo4MC9vY3NwMB8GA1UdIwQYMBaAFJo9tK5lWPvOWgV4JqBtKwSGusbsMAwG\n" +
|
||||
"A1UdEwEB/wQCMAAwNwYDVR0fBDAwLjAsoCqgKIYmaHR0cDovLzIxMC43NC40Mi4z\n" +
|
||||
"L09DQTMxL1JTQS9jcmw2NC5jcmwwDgYDVR0PAQH/BAQDAgbAMB0GA1UdDgQWBBTa\n" +
|
||||
"pk33a/mKcAoX27Prs1miHQgqPTAdBgNVHSUEFjAUBggrBgEFBQcDAgYIKwYBBQUH\n" +
|
||||
"AwQwDQYJKoZIhvcNAQELBQADggEBAK7B11CFYLThIEEIIWG4A3BDvMiQhkeJ2jDd\n" +
|
||||
"xvc/pIH6SmIRJyOTWYxKhkBrI1AaKhOYfzsU973sK2gyLURjkatg1pTxg5v37sIo\n" +
|
||||
"BGqMe05aUF1MQlXsx+R+mes1GJcurPXeZ+Ho3DCBPo/O1uU5QHy5OU4iVOwErrOK\n" +
|
||||
"ExslrbDPFizZSes7I+y/dQcl4WkhjcSA37G/Zay6RsQuiPkHpt7dHzUGnMqF9CvW\n" +
|
||||
"6SiEvhdtSRTyqvjbJFL2bP3prUBwDD6Yi3oe5uclm/iPwy/7HLbG1oGBz0sHn+80\n" +
|
||||
"ac8jKPehg78CEgMaq1FKcl2zvorIixrjoLGYOSVthz6oABZ5MHk=\n" +
|
||||
"-----END CERTIFICATE-----";
|
||||
|
||||
|
||||
String signCert = "MIID6DCCAtCgAwIBAgIFJnVRuPUwDQYJKoZIhvcNAQELBQAwPTELMAkGA1UEBhMCQ04xFDAS\n" +
|
||||
"BgNVBAoMC05YWTIwNDhURVNUMRgwFgYDVQQDDA9OWFkyMDQ4Uk9PVFRFU1QwHhcNMTkwNTMw\n" +
|
||||
"MDAxNjI2WhcNMjQwNTI4MDAxNjI2WjB6MQswCQYDVQQGEwJDTjE3MDUGA1UECgwuUnVyYWwg\n" +
|
||||
"Q3JlZGl0IEJhbmtzIEZ1bmRzIENsZWFyaW5nIENlbnRlcihURVNUKTEUMBIGA1UECwwLRW50\n" +
|
||||
"ZXJwcmlzZXMxHDAaBgNVBAMMEzA2M0A2NzMzMjcwOTk5OTkwMDQwggEiMA0GCSqGSIb3DQEB\n" +
|
||||
"AQUAA4IBDwAwggEKAoIBAQCEEMIed7o5R2ZCPKKZrJjQ7x7hIYsAaBd5vhAhj+HyXyCPF6O6\n" +
|
||||
"3N9znR8uv+BlG2va4jIdye/rLgTGnBuuRZPEYNGvJvoTy01lFtixuInkNW1VkgEQ1CDlwW47\n" +
|
||||
"TOC5dFPYHENkSMMSiDM0BLBQsZH1p4oQzSyGVtAs9YTWQhb/nWSSXgpiz5+qmlvSrsEU+Ffy\n" +
|
||||
"1fYsN6RadZWL2Pegk8NTS/KTfbo9NCgE6jKFoPNzJ20fdK5aUR5ePI5JdnzxfrabkI9F9964\n" +
|
||||
"tlsynsOaoWY60FMmfB+HcvJoSgSI65iNRFRwkzic8mtnmdq9EIIcPgZVz7GCEp9v3Vk30T9e\n" +
|
||||
"M2/ZAgMBAAGjgbEwga4wHwYDVR0jBBgwFoAUZaX9myCvxP1gs80NCsJZtGtypMEwCQYDVR0T\n" +
|
||||
"BAIwADBUBgNVHR8ETTBLMEmgR6BFpEMwQTEOMAwGA1UEAwwFY3JsMjExDDAKBgNVBAsMA2Ny\n" +
|
||||
"bDEUMBIGA1UECgwLTlhZMjA0OFRFU1QxCzAJBgNVBAYTAkNOMAsGA1UdDwQEAwIHgDAdBgNV\n" +
|
||||
"HQ4EFgQUdhrTivJUnKI5oZ0T2OZkSEaDkIUwDQYJKoZIhvcNAQELBQADggEBACgf34fsfPvN\n" +
|
||||
"ln1Ch2Cid7jrJEw5o1AQlahxWtnJH4fzxgYldqgNpoZD3p9XOmFEIvTBHAt8wANQoCQEFJj2\n" +
|
||||
"3PZvwG4T6VXwRn05/u25fMDF4R7Z9IYlNVY5hpxtisC7u8aq7VCqHwzXsjaItuCPQbOkhTCb\n" +
|
||||
"2m4TVGbaMYXVWhEJiss31XrNZgAbKfrFEAmG6+KO5cRdfjXHooaY44sgeEd+Mi5aPyY+QNR1\n" +
|
||||
"uio39G+ur/BjHqOCIujUEI8f3g8wAQaIFyA97rAjJW1W/BVZispRg4niG0mLQ356cTGj+kkJ\n" +
|
||||
"imdxvNyhfwmMNweP6fIUnajMOtJiMomjSAdIuqdxDIw=";
|
||||
String source = "<?xml version=\"1.0\" encoding=\"utf-8\"?><T><D><M><k>操作员:</k><v>800000783001</v></M></D><E><M><k>CifNo</k><v>800000783</v></M><M><k>LoginId</k><v>800000783001</v></M><M><k>LoginType</k><v>C</v></M><M><k>transaction</k><v>eibs.login</v></M></E></T>";
|
||||
String signatureBase64 = "CC0uWxM/YqFrPzRKqvpEDBKz5AZC2+Hp2eEWhgR+z0XR9nirrtovb8e61ahLeYywsZ5bBZgCL5lKI/YJ/mI2Nh3ntWQCXSAf8DpOD3GWkHRMfDgeiGj0F3qyTOJi7iY+wAZOFBLiOEe2P2YFCl0Luny6dfRFoBkvoxVbrrKui+8d5pusciuuwQcL+P7K0FeIPb2yjdZfm+oz+FMlq4dvSHJrnDZ3tUcS7ZzfXSHYwAm1aA5SAWNIS6ixP1pT4nwLM0W+tUdvtP1yx6q7UyZPeDaWDsTqZbLvd3Ond38jQIrPutK/ACBsg92VS2SmQDVj0C+ZrJkvjbXbRj87dn0CGw==";
|
||||
byte[] bSource = source.getBytes();
|
||||
|
||||
byte[] derCert = Util.decodeBase64(signCert);
|
||||
Base64.Encoder encoder = Base64.getEncoder();
|
||||
cert = X509Factory.BEGIN_CERT + "\n" + encoder.encodeToString(derCert) + X509Factory.END_CERT;
|
||||
X509 X509Cert = api.GetX509CertInfo(cert);
|
||||
|
||||
byte[] signature = Util.decodeBase64(signatureBase64);
|
||||
byte[] signatureDer = createBlock((byte) 4, signature);
|
||||
|
||||
|
||||
// 通过传入公钥P1验签
|
||||
Boolean bRet = api.RSAVerify(0, api.DATA_ORIGIN, X509Cert.getPubKey(), bSource, signatureDer);
|
||||
System.out.println("P1验签结果:" + bRet);
|
||||
|
||||
|
||||
/*
|
||||
|
||||
String userID = "2";
|
||||
byte[] testData = {1,2,3,45,1,2,3,45};
|
||||
// 机构号获取公钥
|
||||
String pk=api.RSAGetPublicKeyC(userID,0);
|
||||
System.out.println("PK: " + pk);
|
||||
|
||||
// 通过机构号获取私钥P1签名
|
||||
byte[] sign = api.RSASignC(userID,1,api.DATA_ORIGIN,testData);
|
||||
System.out.println( "P1签名结果: " + Util.bytes2HexString(sign) );
|
||||
|
||||
// 通过传入公钥P1验签
|
||||
boolean bRet = api.RSAVerify(1,api.DATA_ORIGIN,Util.hexString2Bytes(pk),testData, sign );
|
||||
System.out.println("P1验签结果:"+bRet);*/
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
|
||||
if (null != api) {
|
||||
api.disconnect();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
package com.sunyard.sydapi.sample;
|
||||
|
||||
import com.sunyard.RetWrap;
|
||||
import com.sunyard.SydApi;
|
||||
import com.sunyard.cert.X509;
|
||||
import com.sunyard.proto.Util;
|
||||
import racal.sunyard.main.SydApiBuilder;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
public class Test
|
||||
{
|
||||
|
||||
public static void main(String[] args) throws UnsupportedEncodingException, IllegalAccessException, InstantiationException, ClassNotFoundException {
|
||||
|
||||
System.out.println("工银亚洲测试功能测试");
|
||||
byte[] testData = new byte[17000];
|
||||
Arrays.fill(testData, (byte) 0x88);
|
||||
|
||||
SydApi api = new SydApiBuilder()
|
||||
.setIp("192.168.1.226")
|
||||
// .useEBCDMode()
|
||||
.useDebugMode()
|
||||
// .useETEncrypt()
|
||||
.build();
|
||||
|
||||
// SydApi api = new SydApi4j().connect("192.168.0.222", 8889, null, 3000);
|
||||
|
||||
try {
|
||||
|
||||
//RetWrap ret = api.genComposeKey(new String[]{"D5B45BA5E80B4520", "187B1BFB7629DCD7"});
|
||||
//RetWrap ret = api.genSymmetricKey("000", 'X');
|
||||
//System.out.println( ret );
|
||||
|
||||
String cert2 = "-----BEGIN CERTIFICATE-----\n" +
|
||||
"MIID6DCCAtCgAwIBAgIFJnVRuPUwDQYJKoZIhvcNAQELBQAwPTELMAkGA1UEBhMCQ04xFDAS\n" +
|
||||
"BgNVBAoMC05YWTIwNDhURVNUMRgwFgYDVQQDDA9OWFkyMDQ4Uk9PVFRFU1QwHhcNMTkwNTMw\n" +
|
||||
"MDAxNjI2WhcNMjQwNTI4MDAxNjI2WjB6MQswCQYDVQQGEwJDTjE3MDUGA1UECgwuUnVyYWwg\n" +
|
||||
"Q3JlZGl0IEJhbmtzIEZ1bmRzIENsZWFyaW5nIENlbnRlcihURVNUKTEUMBIGA1UECwwLRW50\n" +
|
||||
"ZXJwcmlzZXMxHDAaBgNVBAMMEzA2M0A2NzMzMjcwOTk5OTkwMDQwggEiMA0GCSqGSIb3DQEB\n" +
|
||||
"AQUAA4IBDwAwggEKAoIBAQCEEMIed7o5R2ZCPKKZrJjQ7x7hIYsAaBd5vhAhj+HyXyCPF6O6\n" +
|
||||
"3N9znR8uv+BlG2va4jIdye/rLgTGnBuuRZPEYNGvJvoTy01lFtixuInkNW1VkgEQ1CDlwW47\n" +
|
||||
"TOC5dFPYHENkSMMSiDM0BLBQsZH1p4oQzSyGVtAs9YTWQhb/nWSSXgpiz5+qmlvSrsEU+Ffy\n" +
|
||||
"1fYsN6RadZWL2Pegk8NTS/KTfbo9NCgE6jKFoPNzJ20fdK5aUR5ePI5JdnzxfrabkI9F9964\n" +
|
||||
"tlsynsOaoWY60FMmfB+HcvJoSgSI65iNRFRwkzic8mtnmdq9EIIcPgZVz7GCEp9v3Vk30T9e\n" +
|
||||
"M2/ZAgMBAAGjgbEwga4wHwYDVR0jBBgwFoAUZaX9myCvxP1gs80NCsJZtGtypMEwCQYDVR0T\n" +
|
||||
"BAIwADBUBgNVHR8ETTBLMEmgR6BFpEMwQTEOMAwGA1UEAwwFY3JsMjExDDAKBgNVBAsMA2Ny\n" +
|
||||
"bDEUMBIGA1UECgwLTlhZMjA0OFRFU1QxCzAJBgNVBAYTAkNOMAsGA1UdDwQEAwIHgDAdBgNV\n" +
|
||||
"HQ4EFgQUdhrTivJUnKI5oZ0T2OZkSEaDkIUwDQYJKoZIhvcNAQELBQADggEBACgf34fsfPvN\n" +
|
||||
"ln1Ch2Cid7jrJEw5o1AQlahxWtnJH4fzxgYldqgNpoZD3p9XOmFEIvTBHAt8wANQoCQEFJj2\n" +
|
||||
"3PZvwG4T6VXwRn05/u25fMDF4R7Z9IYlNVY5hpxtisC7u8aq7VCqHwzXsjaItuCPQbOkhTCb\n" +
|
||||
"2m4TVGbaMYXVWhEJiss31XrNZgAbKfrFEAmG6+KO5cRdfjXHooaY44sgeEd+Mi5aPyY+QNR1\n" +
|
||||
"uio39G+ur/BjHqOCIujUEI8f3g8wAQaIFyA97rAjJW1W/BVZispRg4niG0mLQ356cTGj+kkJ\n" +
|
||||
"imdxvNyhfwmMNweP6fIUnajMOtJiMomjSAdIuqdxDIw=\n" +
|
||||
"-----END CERTIFICATE-----\n";
|
||||
|
||||
X509 x509 = api.GetX509CertInfo( cert2 );
|
||||
|
||||
System.out.println( x509 );
|
||||
|
||||
|
||||
}finally {
|
||||
api.disconnect();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,126 +0,0 @@
|
||||
package com.sunyard.sydapi.sample;
|
||||
|
||||
import com.sunyard.RetWrap;
|
||||
import com.sunyard.SydApi;
|
||||
import racal.sunyard.main.SydApiBuilder;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
// S
|
||||
// key=53353745304145303232443638383236
|
||||
// kcv=3641364533414331353746454631303730314132374139334446303246313938
|
||||
// s1 = 4AD6F3FCC606182A
|
||||
// s2 = 2F47B4C75439C0F0
|
||||
|
||||
|
||||
/**
|
||||
* S
|
||||
* 53433030443432444141433331394234
|
||||
* 3031463646374141334331453532333945443237423031434136333735383530
|
||||
* <p>
|
||||
* <p>
|
||||
* <p>
|
||||
* 554233453843413345304545453331374130333744353832434544454433363138
|
||||
* 334138463043
|
||||
* <p>
|
||||
* <p>
|
||||
* <p>
|
||||
* SA035CDE407F5249
|
||||
* D99CABFAE8D110CD9269FB34B8CAD265
|
||||
* <p>
|
||||
* <p>
|
||||
* X55D9B6452E7B89BB7B266E2D02042780
|
||||
* 7AD5FB
|
||||
* <p>
|
||||
* X7B266E2D0204278055D9B6452E7B89BB
|
||||
* 5A2ABC60CEFCC568
|
||||
*/
|
||||
// U
|
||||
|
||||
/**
|
||||
554233453843413345304545453331374130333744353832434544454433363138
|
||||
334138463043
|
||||
*/
|
||||
|
||||
// T
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* SA035CDE407F5249
|
||||
* D99CABFAE8D110CD9269FB34B8CAD265
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* X55D9B6452E7B89BB7B266E2D02042780
|
||||
* 7AD5FB
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* X7B266E2D0204278055D9B6452E7B89BB
|
||||
* 5A2ABC60CEFCC568
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* X1C7B9DF464C039F2D031E0465E8F89B9
|
||||
* 10093C
|
||||
*/
|
||||
public class Test2 {
|
||||
|
||||
public static void main(String[] args) throws UnsupportedEncodingException, IllegalAccessException, InstantiationException, ClassNotFoundException {
|
||||
|
||||
System.out.println("工银亚洲测试功能测试");
|
||||
byte[] testData = new byte[17000];
|
||||
Arrays.fill(testData, (byte) 0x88);
|
||||
|
||||
SydApi api = new SydApiBuilder()
|
||||
.setIp("172.16.17.162")
|
||||
.useDebugMode()
|
||||
.build();
|
||||
|
||||
// SydApi api = new SydApi4j().connect("192.168.0.222", 8889, null, 3000);
|
||||
|
||||
|
||||
RetWrap ret;
|
||||
try {
|
||||
// ret = api.genAndPrintSplit(
|
||||
// "000",
|
||||
// 'X'
|
||||
// );
|
||||
//
|
||||
//
|
||||
// System.out.println( Util.bytes2HexString((byte[]) ret.get("lmkKey")) );
|
||||
// System.out.println( Util.bytes2HexString((byte[]) ret.get("keyCheck")) );
|
||||
|
||||
String[] splits = new String[]{"45A2863406F82135", "2314C1B6C97DF5B8"};
|
||||
// byte[] p1 = Util.hexString2Bytes( splits[0] );
|
||||
// byte[] p2 = Util.hexString2Bytes( splits[1] );
|
||||
// byte[] keybytes = new byte[p1.length + p2.length];
|
||||
|
||||
// for ( int i = 0; i < keybytes.length ; i++ ){
|
||||
// keybytes[i] = (byte) (p1[i] ^ p2[i]);
|
||||
// }
|
||||
|
||||
// System.arraycopy( p1, 0, keybytes, 0, p1.length );
|
||||
// System.arraycopy( p2, 0, keybytes, p1.length, p2.length );
|
||||
//
|
||||
ret = api.genComposeKey("000", 'X', splits[0] + splits[1]);
|
||||
|
||||
System.out.println(ret);
|
||||
System.out.println(new String((byte[]) ret.get("lmkKey")));
|
||||
System.out.println(new String((byte[]) ret.get("keyCheck")));
|
||||
|
||||
} finally {
|
||||
api.disconnect();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
package com.sunyard.sydapi.sample;
|
||||
|
||||
import com.sunyard.RetWrap;
|
||||
import com.sunyard.SydApi;
|
||||
import com.sunyard.proto.Util;
|
||||
import racal.sunyard.main.SydApiBuilder;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class TestKeyPair
|
||||
{
|
||||
|
||||
public static void main(String[] args) throws UnsupportedEncodingException, IllegalAccessException, InstantiationException, ClassNotFoundException {
|
||||
|
||||
System.out.println("非对称密钥生成");
|
||||
byte[] testData = new byte[17000];
|
||||
Arrays.fill(testData, (byte) 0x88);
|
||||
|
||||
SydApi api = new SydApiBuilder()
|
||||
.setIp("172.16.17.161")
|
||||
.useDebugMode()
|
||||
.build();
|
||||
|
||||
RetWrap ret;
|
||||
try {
|
||||
|
||||
// SM2
|
||||
ret = api.SM2OrRSAGenKeyPair(1, "", 0);
|
||||
|
||||
System.out.println( ret );
|
||||
System.out.println( Util.bytes2HexString((byte[]) ret.get("PrivateKey")) );
|
||||
System.out.println( Util.bytes2HexString((byte[]) ret.get("PublicKey")) );
|
||||
|
||||
|
||||
// RSA
|
||||
ret = api.SM2OrRSAGenKeyPair(0, "", 1152);
|
||||
|
||||
System.out.println( ret );
|
||||
System.out.println( Util.bytes2HexString((byte[]) ret.get("PrivateKey")) );
|
||||
System.out.println( Util.bytes2HexString((byte[]) ret.get("PublicKey")) );
|
||||
|
||||
}finally {
|
||||
api.disconnect();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,614 +0,0 @@
|
||||
package com.sunyard.sydapi.sample;
|
||||
|
||||
import com.sunyard.RetWrap;
|
||||
import com.sunyard.SydApi;
|
||||
import com.sunyard.cert.X509;
|
||||
import com.sunyard.proto.Util;
|
||||
import racal.sunyard.main.SydApi4j;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* 前置模拟服务
|
||||
*/
|
||||
public class TestXJMain {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String[] inputStrs;
|
||||
|
||||
System.out.println("SydApi 测试程序启动");
|
||||
System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
||||
SydApi4j api = null;
|
||||
Scanner sc = new Scanner(System.in);
|
||||
|
||||
api = apiCon(sc);
|
||||
|
||||
if (api != null) {
|
||||
System.out.println("连接成功");
|
||||
}
|
||||
|
||||
while (true) {
|
||||
|
||||
while (api == null) {
|
||||
api = apiCon(sc);
|
||||
}
|
||||
|
||||
System.out.print("请输入指令(指令0可以查询所有指令):");
|
||||
int code = sc.nextInt();
|
||||
// 接int后面的一个换行
|
||||
String s = sc.nextLine();
|
||||
|
||||
try {
|
||||
|
||||
switch (code) {
|
||||
case 0: // 查询指令
|
||||
System.out.println("指令 0: 查询指令\n" +
|
||||
"指令 1: 更换ip\n" +
|
||||
"指令 2: PKCS#1签名验签(DN获取公私钥)\n" +
|
||||
"指令 3: PKCS1签名验签(序列号获取公私钥)\n" +
|
||||
"指令 4: PKCS#1签名验签(序列号获取公私钥,银联)\n" +
|
||||
"指令 5: PKCS#1签名验签(ID获取私钥,外部传入公钥,自定义ID)\n" +
|
||||
"指令 6: PKCS#7签名验签(DN获取私钥)\n" +
|
||||
"指令 7: PKCS#7签名验签(DN获取私钥, 数据不限长)\n" +
|
||||
"指令 8: PKCS#7签名(DN获取私钥)\n" +
|
||||
"指令 9: 生成会话密钥并确认\n" +
|
||||
"指令 10: 对称密钥加解密-ECB\n" +
|
||||
"指令 11: 对称密钥加解密-CBC\n" +
|
||||
"指令 12: 生成数字信封及解密数字信封(网联)\n" +
|
||||
"指令 13: 通过DN获取公钥\n" +
|
||||
"指令 14: 通过DN获取序列号\n" +
|
||||
"指令 15: 通过DN获取公钥\n" +
|
||||
"指令 16: 通过ID获取公钥\n" +
|
||||
"指令 17: 通过ID获取X509证书并解析\n" +
|
||||
"指令 18: 生成SM2密钥对\n" +
|
||||
"指令 19: 生成SM2密钥对\n" +
|
||||
"指令 20: SM2加解密\n" +
|
||||
"指令 21: SM3哈希(带公钥)\n" +
|
||||
"指令 22: SM3哈希\n" +
|
||||
"指令 -1: 退出");
|
||||
break;
|
||||
case 1: // 更换ip
|
||||
apiDisCon(api);
|
||||
|
||||
api = apiCon(sc);
|
||||
|
||||
break;
|
||||
|
||||
case 2: // PKCS#1签名验签(DN获取公私钥)
|
||||
inputStrs = inputUtil(sc, "DN", "签名验签原数据");
|
||||
|
||||
testSYD_NakedVerify(api, inputStrs[0], inputStrs[1]);
|
||||
break;
|
||||
|
||||
case 3: // PKCS1签名验签(序列号获取公私钥)
|
||||
inputStrs = inputUtil(sc, "序列号", "签名验签原数据");
|
||||
|
||||
testNetVerify(api, inputStrs[0], inputStrs[1]);
|
||||
break;
|
||||
|
||||
case 4: // PKCS#1签名验签(序列号获取公私钥,银联)
|
||||
inputStrs = inputUtil(sc, "序列号", "签名验签原数据");
|
||||
|
||||
testSM2VerifyCustomized(api, inputStrs[0], inputStrs[1]);
|
||||
break;
|
||||
|
||||
case 5: // PKCS#1签名验签(ID获取私钥,外部传入公钥,自定义ID)
|
||||
inputStrs = inputUtil(sc,
|
||||
"使用者的 ID(生成实体时的机构号)", "id(用于计算SM3-HASH)", "原始数据");
|
||||
|
||||
testSM2Verify(api, inputStrs[0], inputStrs[1],
|
||||
inputStrs[2]);
|
||||
break;
|
||||
case 6: // PKCS#7签名验签(DN获取私钥)
|
||||
inputStrs = inputUtil(sc, "签名者证书 DN", "待签名的原始数据");
|
||||
|
||||
testSYD_DetachedVerify(api, inputStrs[0], inputStrs[1]);
|
||||
break;
|
||||
|
||||
case 7: // PKCS#7签名验签(DN获取私钥, 数据不限长)
|
||||
inputStrs = inputUtil(sc, "sm3 hash所用的公钥",
|
||||
"待签名的原始数据", "签名者证书 DN");
|
||||
|
||||
testSm3AndDetachedSign(api, inputStrs[0],
|
||||
inputStrs[1], inputStrs[2]);
|
||||
break;
|
||||
|
||||
case 8: // PKCS#7签名(DN获取私钥)
|
||||
inputStrs = inputUtil(sc, "sm3 hash所用的公钥",
|
||||
"待签名的原始数据", "签名者证书 DN");
|
||||
|
||||
testDetachedSign(api, inputStrs[0],
|
||||
inputStrs[1], inputStrs[2]);
|
||||
break;
|
||||
|
||||
case 9: // 生成会话密钥并确认
|
||||
inputStrs = inputUtil(sc, "公钥的der编码", "使用者的ID");
|
||||
|
||||
testSM2ConfirmSessionKeyC(api, inputStrs[0], inputStrs[1]);
|
||||
break;
|
||||
|
||||
case 10: // 对称密钥加解密-ECB
|
||||
inputStrs = inputUtil(sc, "算法标识(0:3DES,1:SM4)",
|
||||
"使用者的 ID(生成密钥时的机构号)", "加解密原始数据");
|
||||
|
||||
testSYMEncryptData(api, Integer.parseInt(inputStrs[0]),
|
||||
inputStrs[1], inputStrs[2]);
|
||||
break;
|
||||
case 11: // 对称密钥加解密-CBC
|
||||
inputStrs = inputUtil(sc, "算法标识(0:3DES,1:SM4)",
|
||||
"使用者的 ID(生成密钥时的机构号)", "加解密原始数据");
|
||||
|
||||
testEncDecData(api, Integer.parseInt(inputStrs[0]),
|
||||
inputStrs[1], inputStrs[2]);
|
||||
break;
|
||||
|
||||
case 12: // 生成数字信封及解密数字信封(网联)
|
||||
inputStrs = inputUtil(sc, "X509证书的序列号");
|
||||
|
||||
testDecryptDE(api, inputStrs[0]);
|
||||
break;
|
||||
|
||||
case 13: // 通过DN获取公钥
|
||||
inputStrs = inputUtil(sc, "证书的DN", "密钥类型(0:签名公钥,1:加密公钥)");
|
||||
|
||||
testSYD_SM2_Get_Dn_PublicKey_HA(api, inputStrs[0],
|
||||
Integer.parseInt(inputStrs[1]));
|
||||
break;
|
||||
|
||||
case 14: // 通过DN获取序列号
|
||||
inputStrs = inputUtil(sc, "证书的DN", "证书类型(0:签名证书,1:加密证书)");
|
||||
|
||||
testGetX509SN(api, inputStrs[0],
|
||||
Integer.parseInt(inputStrs[1]));
|
||||
break;
|
||||
|
||||
case 15: // 通过DN获取公钥
|
||||
inputStrs = inputUtil(sc, "证书的DN", "证书类型(0:签名证书,1:加密证书)");
|
||||
|
||||
testGetDNPublicKey(api, inputStrs[0],
|
||||
Integer.parseInt(inputStrs[1]));
|
||||
break;
|
||||
|
||||
case 16: // 通过ID获取公钥
|
||||
inputStrs = inputUtil(sc, "使用者的ID", "密钥类型(0:签名公钥, 1:加密公钥)");
|
||||
|
||||
testSM2GetPublicKeyC(api, inputStrs[0],
|
||||
Integer.parseInt(inputStrs[1]));
|
||||
break;
|
||||
case 17: // 通过ID获取X509证书并解析
|
||||
inputStrs = inputUtil(sc, "使用者的ID", "证书类型(0:签名证书,1:加密证书)");
|
||||
|
||||
testGetX509CertFileC(api, inputStrs[0],
|
||||
Integer.parseInt(inputStrs[1]));
|
||||
break;
|
||||
|
||||
case 18: // 生成SM2密钥对
|
||||
testSM2GenKeyPair(api);
|
||||
break;
|
||||
|
||||
case 19: // 生成SM2密钥对
|
||||
testSYD_SM2GenKeyPair(api);
|
||||
break;
|
||||
|
||||
case 20: // SM2加解密
|
||||
inputStrs = inputUtil(sc, "使用者的ID(生成密钥时的机构号)", "加解密原始数据");
|
||||
|
||||
testSM2DecryptC(api, inputStrs[0], inputStrs[1]);
|
||||
break;
|
||||
|
||||
case 21: // SM3哈希(带公钥)
|
||||
inputStrs = inputUtil(sc, "公钥", "原始数据");
|
||||
|
||||
testSM3Hash(api, inputStrs[0], inputStrs[1]);
|
||||
break;
|
||||
|
||||
case 22: // SM3哈希
|
||||
inputStrs = inputUtil(sc, "原始数据");
|
||||
|
||||
testSM3Hash2(api, inputStrs[0]);
|
||||
break;
|
||||
case -1: // 退出
|
||||
System.out.print("退出程序");
|
||||
apiDisCon(api);
|
||||
|
||||
System.exit(0);
|
||||
default:
|
||||
System.out.println("输入指令错误,请重新输入....");
|
||||
System.out.println("可以输入指令0查询所有指令....");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static SydApi4j apiCon(Scanner sc) {
|
||||
SydApi4j api;
|
||||
System.out.print("请输入ip地址:");
|
||||
String ip = sc.nextLine().trim();
|
||||
try {
|
||||
api = (SydApi4j) new SydApi4j().connect(ip, 8889, null, 10000);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("连接未成功,请重新连接...");
|
||||
return null;
|
||||
}
|
||||
|
||||
return api;
|
||||
}
|
||||
|
||||
// 断开连接
|
||||
public static void apiDisCon(SydApi api) {
|
||||
try {
|
||||
api.disconnect();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static String[] inputUtil(Scanner sc, String... strs) {
|
||||
String[] retStrs = new String[strs.length];
|
||||
|
||||
for (int i = 0; i < strs.length; i++) {
|
||||
System.out.print("请输入" + strs[i] + ":");
|
||||
retStrs[i] = sc.nextLine().trim();
|
||||
}
|
||||
|
||||
return retStrs;
|
||||
}
|
||||
|
||||
|
||||
// ==========================================================================
|
||||
// 对称加解密流程-ECB
|
||||
public static void testSYMEncryptData(SydApi api, int alg, String userId, String orgData) {
|
||||
|
||||
System.out.println("通过id获取公钥");
|
||||
String publicKey = api.SM2GetPublicKeyC(userId, 1);
|
||||
System.out.println("获取的公钥为: " + publicKey);
|
||||
|
||||
// 生成会话密钥
|
||||
System.out.println("生成会话密钥");
|
||||
RetWrap retWrap = api.SM2GenSessionKey(publicKey);
|
||||
printRetWrap(retWrap);
|
||||
String cipherKey = (String) retWrap.get("CipherKey");
|
||||
|
||||
// 确认会话密钥
|
||||
System.out.println("确认会话密钥");
|
||||
RetWrap wrap = api.SM2ConfirmSessionKeyC(userId, cipherKey);
|
||||
printRetWrap(wrap);
|
||||
String sessionKey = (String) wrap.get(SydApi.RET_SESSION_KEY);
|
||||
|
||||
// 对称加密
|
||||
System.out.println("对称加密");
|
||||
byte[] bytes = api.SYMEncryptData(alg, sessionKey, orgData.getBytes());
|
||||
|
||||
System.out.println("对称解密");
|
||||
byte[] bytes1 = api.SYMDecryptData(alg, sessionKey, bytes);
|
||||
System.out.println("解密结果: " + new String(bytes1));
|
||||
}
|
||||
|
||||
// 对称加解密流程-CBC
|
||||
public static void testEncDecData(SydApi api, int alg, String userId, String orgData) {
|
||||
|
||||
System.out.println("通过id获取公钥");
|
||||
String publicKey = api.SM2GetPublicKeyC(userId, 1);
|
||||
System.out.println("获取的公钥为: " + publicKey);
|
||||
|
||||
System.out.println("生成会话密钥");
|
||||
RetWrap retWrap = api.SM2GenSessionKey(publicKey);
|
||||
printRetWrap(retWrap);
|
||||
String cipherKey = (String) retWrap.get("CipherKey");
|
||||
|
||||
System.out.println("确认会话密钥");
|
||||
RetWrap wrap = api.SM2ConfirmSessionKeyC(userId, cipherKey);
|
||||
printRetWrap(wrap);
|
||||
String sessionKey = (String) wrap.get(SydApi.RET_SESSION_KEY);
|
||||
|
||||
// 对称加密
|
||||
System.out.println("对称加密");
|
||||
byte[] bytes = api.SYMEncryptDataCBC(alg, sessionKey, orgData.getBytes());
|
||||
|
||||
System.out.println("对称解密");
|
||||
byte[] bytes1 = api.SYMDecryptDataCBC(alg, sessionKey, bytes);
|
||||
System.out.println("解密结果: " + new String(bytes1));
|
||||
}
|
||||
|
||||
// PKCS#1签名验签(DN获取公私钥)
|
||||
// 测试通过
|
||||
// 8004
|
||||
public static void testSYD_NakedVerify(SydApi api, String dn, String orgData) {
|
||||
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);
|
||||
}
|
||||
|
||||
// PKCS1签名验签(序列号获取公私钥)
|
||||
// 测试通过
|
||||
// 6D
|
||||
public static void testNetVerify(SydApi api, String sn, String orgData) {
|
||||
String sign = api.netSign(sn, orgData.getBytes());
|
||||
System.out.println("签名结果: " + sign);
|
||||
|
||||
boolean b = api.netVerify(sn, orgData.getBytes(), sign);
|
||||
System.out.println("验签结果: " + b);
|
||||
}
|
||||
|
||||
// PKCS#1签名验签(序列号获取公私钥,银联)
|
||||
// 测试通过
|
||||
// 81
|
||||
public static void testSM2VerifyCustomized(SydApi api, String sn, String orgData) {
|
||||
String sign = api.SM2SignCustomized(sn, orgData.getBytes());
|
||||
System.out.println("签名结果: " + sign);
|
||||
|
||||
boolean b = api.SM2VerifyCustomized(sn, orgData.getBytes(), sign);
|
||||
System.out.println("验签结果: " + b);
|
||||
}
|
||||
|
||||
// PKCS#1签名验签(ID获取私钥,外部传入公钥,自定义ID)
|
||||
// 测试通过
|
||||
// 75
|
||||
public static void testSM2Verify(SydApi api, String userId,
|
||||
String certNo, String orgData) {
|
||||
|
||||
String publicKey = api.SM2GetPublicKeyC(userId, 0);
|
||||
System.out.println("获取到的公钥: " + publicKey);
|
||||
|
||||
String sign = api.SM2SignC(userId, certNo, 1,
|
||||
Util.hexString2Bytes(publicKey), orgData.getBytes());
|
||||
System.out.println("签名结果: " + sign);
|
||||
|
||||
boolean b = api.SM2Verify(certNo, 1,
|
||||
Util.hexString2Bytes(publicKey), orgData.getBytes(), sign);
|
||||
System.out.println("验签结果: " + b);
|
||||
}
|
||||
|
||||
// PKCS#7签名验签(DN获取私钥, 数据8K以内)
|
||||
// 测试通过
|
||||
// 8006
|
||||
public static void testSYD_DetachedVerify(SydApi api, String DN,
|
||||
String orgData) {
|
||||
String sign = api.SYD_DetachedSign(orgData.getBytes(), DN);
|
||||
System.out.println("签名结果" + sign);
|
||||
|
||||
boolean b = api.SYD_DetachedVerify(orgData.getBytes(), sign);
|
||||
System.out.println("验签结果:" + b);
|
||||
}
|
||||
|
||||
// PKCS#7签名验签(DN获取私钥, 数据不限长)
|
||||
// 测试通过
|
||||
// 8005
|
||||
public static void testSm3AndDetachedSign(SydApi api, String publicKey,
|
||||
String orgData, String sCertDN) {
|
||||
|
||||
String sign = api.sm3AndDetachedSign(orgData.getBytes(), publicKey, sCertDN);
|
||||
System.out.println("签名结果: " + sign);
|
||||
|
||||
boolean b = api.sm3AndDetachedVerify(orgData.getBytes(), publicKey, sign);
|
||||
System.out.println("验签结果:" + b);
|
||||
}
|
||||
|
||||
// PKCS#7签名(DN获取私钥)
|
||||
// 测试通过
|
||||
// 8005
|
||||
public static void testDetachedSign(SydApi api, String publicKey,
|
||||
String orgData, String sCertDN) {
|
||||
|
||||
byte[] hash = api.SM3Hash(publicKey, orgData.getBytes());
|
||||
|
||||
String sign = api.detachedSign(
|
||||
SydApi.DATA_HASH,
|
||||
hash,
|
||||
sCertDN
|
||||
);
|
||||
|
||||
System.out.println("签名结果: " + sign);
|
||||
|
||||
boolean verify = api.detachedVerify(
|
||||
SydApi.DATA_HASH,
|
||||
hash,
|
||||
sign
|
||||
);
|
||||
|
||||
System.out.println("验签结果: " + verify);
|
||||
}
|
||||
|
||||
// 生成会话密钥并确认
|
||||
// 测试通过
|
||||
// 73
|
||||
public static void testSM2ConfirmSessionKeyC(SydApi api,
|
||||
String publicKey,
|
||||
String userId) {
|
||||
|
||||
RetWrap retWrap = api.SM2GenSessionKey(publicKey);
|
||||
printRetWrap(retWrap);
|
||||
String cipherKey = (String) retWrap.get("CipherKey");
|
||||
|
||||
RetWrap retWrap1 = api.SM2ConfirmSessionKeyC(userId, cipherKey);
|
||||
printRetWrap(retWrap1);
|
||||
}
|
||||
|
||||
// 生成数字信封及解密数字信封(网联)
|
||||
// 测试通过
|
||||
// 6B
|
||||
public static void testDecryptDE(SydApi api, String sn) {
|
||||
|
||||
RetWrap retWrap = api.generateDE(sn, (byte) 0x30);
|
||||
printRetWrap(retWrap);
|
||||
String CipherKey = (String) retWrap.get("CipherKey");
|
||||
|
||||
RetWrap retWrap1 = api.decryptDE(sn, CipherKey);
|
||||
printRetWrap(retWrap1);
|
||||
}
|
||||
|
||||
// 通过DN获取公钥
|
||||
// 测试通过
|
||||
// 7E
|
||||
public static void testSYD_SM2_Get_Dn_PublicKey_HA(SydApi api,
|
||||
String dn,
|
||||
int keyType) {
|
||||
String s = api.SYD_SM2_Get_Dn_PublicKey_HA(dn.getBytes(), keyType);
|
||||
System.out.println("公钥为: " + s);
|
||||
}
|
||||
|
||||
// 通过DN获取序列号
|
||||
// 测试通过
|
||||
// 6E
|
||||
public static void testGetX509SN(SydApi api, String dn, int keyType) {
|
||||
String s = api.getX509SN(keyType, dn.getBytes());
|
||||
System.out.println("证书序列号: " + s);
|
||||
}
|
||||
|
||||
// 通过DN获取公钥
|
||||
// 测试通过
|
||||
// 7E
|
||||
public static void testGetDNPublicKey(SydApi api, String dn, int keyType) {
|
||||
String s = api.GetDNPublicKey(keyType, dn.getBytes());
|
||||
System.out.println("公钥为: " + s);
|
||||
}
|
||||
|
||||
// 通过ID获取公钥
|
||||
// 测试通过
|
||||
// QQ
|
||||
public static void testSM2GetPublicKeyC(SydApi api,
|
||||
String userId,
|
||||
int keyType) {
|
||||
String s = api.SM2GetPublicKeyC(userId, keyType);
|
||||
System.out.println("公钥为: " + s);
|
||||
}
|
||||
|
||||
// 通过ID获取X509证书并解析
|
||||
// 测试通过
|
||||
// 8017
|
||||
public static void testGetX509CertFileC(SydApi api,
|
||||
String userId,
|
||||
int keyType) {
|
||||
String s = api.GetX509CertFileC(userId, keyType);
|
||||
System.out.println("证书: " + s);
|
||||
|
||||
X509 x509 = api.GetX509CertInfo(s);
|
||||
System.out.println("证书公钥: " + Util.bytes2HexString(x509.getPubKey()));
|
||||
}
|
||||
|
||||
// 生成SM2密钥对
|
||||
// 测试通过
|
||||
// E2
|
||||
public static void testSM2GenKeyPair(SydApi api) {
|
||||
RetWrap retWrap = api.SM2GenKeyPair();
|
||||
printRetWrap(retWrap, true);
|
||||
}
|
||||
|
||||
// 生成SM2密钥对
|
||||
// 测试通过
|
||||
// QN
|
||||
public static void testSYD_SM2GenKeyPair(SydApi api) {
|
||||
RetWrap retWrap = api.SYD_SM2GenKeyPair();
|
||||
printRetWrap(retWrap);
|
||||
}
|
||||
|
||||
// SM2加解密
|
||||
// 测试通过
|
||||
// 78
|
||||
public static void testSM2DecryptC(SydApi api,
|
||||
String userId,
|
||||
String orgData) {
|
||||
|
||||
String publicKey = api.SM2GetPublicKeyC(userId, 1);
|
||||
System.out.println("获取的公钥为: " + publicKey);
|
||||
|
||||
String s = api.SM2Encrypt(Util.hexString2Bytes(publicKey), orgData.getBytes());
|
||||
System.out.println("加密结果: " + s);
|
||||
|
||||
byte[] bytes = api.SM2DecryptC(userId, s);
|
||||
System.out.println("解密结果:" + new String(bytes));
|
||||
|
||||
}
|
||||
|
||||
// SM3哈希(带公钥)
|
||||
// 测试通过
|
||||
// 76
|
||||
// 带公钥
|
||||
public static void testSM3Hash(SydApi api, String publicKey, String orgData) {
|
||||
byte[] hash = api.SM3Hash(publicKey, orgData.getBytes());
|
||||
|
||||
System.out.println("hash结果:" + Util.bytes2HexString(hash));
|
||||
}
|
||||
|
||||
// SM3哈希
|
||||
// 测试通过
|
||||
// 76
|
||||
// 不带公钥
|
||||
public static void testSM3Hash2(SydApi api, String orgData) {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.sunyard.sydapi.example;
|
||||
package com.sunyard.sydapi.test;
|
||||
|
||||
import com.sunyard.RetWrap;
|
||||
import com.sunyard.SydApi;
|
||||
Reference in New Issue
Block a user