diff --git a/DProto/LF.xml b/DProto/LF.xml index 923b057..3384973 100644 --- a/DProto/LF.xml +++ b/DProto/LF.xml @@ -91,7 +91,7 @@ }else if( $final.data.keyHead.eqStr("X") || $final.data.keyHead.eqStr("U") || $final.data.keyHead.eqStr("S") ){ return 32 }else{ - return 16 + return 15 } } function keyCheckLen() { diff --git a/src/main/java/racal/sunyard/main/SydApi4j.java b/src/main/java/racal/sunyard/main/SydApi4j.java index 33edbba..bace115 100644 --- a/src/main/java/racal/sunyard/main/SydApi4j.java +++ b/src/main/java/racal/sunyard/main/SydApi4j.java @@ -322,6 +322,7 @@ public class SydApi4j implements SydApi { //return SM2Verify( "1234567812345678", nOrgDataType, publicKey, orgData, base64Sign); // if (certNo.length() > 32) { // certNo = certNo.substring(0, 32); +// } // } if (SydApi.DATA_HASH == nOrgDataType) { orgData = SM3Hash(Util.bytes2HexString(publicKey), orgData); @@ -366,7 +367,7 @@ public class SydApi4j implements SydApi { @Override public byte[] SYD_NakedSign(byte[] orgData, String sCertDN) { - Proto8003 proto = new Proto8003(); //pset.getProtocolReqRender("75"); + Proto8003 proto = new Proto8003(); byte[] dn = new byte[256]; byte[] s = null; try { @@ -417,7 +418,7 @@ public class SydApi4j implements SydApi { @Override public boolean SYD_NakedVerify(byte[] orgData, byte[] sign, String sCertDN) { - Proto8004 proto = new Proto8004(); //pset.getProtocolReqRender("75"); + Proto8004 proto = new Proto8004(); byte[] dn = new byte[256]; byte[] s = null; @@ -613,7 +614,7 @@ public class SydApi4j implements SydApi { // 测试通过 @Override public String detachedSign(int dataType, byte[] orgData, String sCertDN) { - Proto8005 proto = new Proto8005(); //pset.getProtocolReqRender("75"); + Proto8005 proto = new Proto8005(); byte[] dn = new byte[256]; byte[] s = null; try { @@ -671,7 +672,7 @@ public class SydApi4j implements SydApi { // 测试通过 @Override public boolean detachedVerify(int dataType, byte[] orgData, String sign) { - Proto8006 proto = new Proto8006(); //pset.getProtocolReqRender("75"); + Proto8006 proto = new Proto8006(); // 填充数据 HashMap packet = new HashMap(); @@ -719,7 +720,7 @@ public class SydApi4j implements SydApi { @Override public X509 detachedVerifyAndGetX509(int dataType, byte[] orgData, String sign) { - Proto8006 proto = new Proto8006(); //pset.getProtocolReqRender("75"); + Proto8006 proto = new Proto8006(); // 填充数据 HashMap packet = new HashMap(); @@ -773,7 +774,7 @@ public class SydApi4j implements SydApi { @Override public boolean detachedVerify(int dataType, byte[] orgData, byte[] sign) { - Proto8025 proto = new Proto8025(); //pset.getProtocolReqRender("75"); + Proto8025 proto = new Proto8025(); // 填充数据 HashMap packet = new HashMap(); @@ -851,7 +852,7 @@ public class SydApi4j implements SydApi { String sign ) { - Proto8012 proto = new Proto8012(); //pset.getProtocolReqRender("75"); + Proto8012 proto = new Proto8012(); // 填充数据 @@ -3513,7 +3514,7 @@ public class SydApi4j implements SydApi { if (certNo.length() > 32) { certNo = certNo.substring(0, 32); } - Proto80 proto = new Proto80(); //pset.getProtocolReqRender("75"); + Proto80 proto = new Proto80(); // 填充数据 @@ -3650,7 +3651,7 @@ public class SydApi4j implements SydApi { certNo = certNo.substring(0, 32); } - Proto81 proto = new Proto81(); //pset.getProtocolReqRender("75"); + Proto81 proto = new Proto81(); // 填充数据 @@ -9779,7 +9780,7 @@ public class SydApi4j implements SydApi { */ @Override public String attachedSign(byte[] orgData, String sCertDN) { - Proto8018 proto = new Proto8018(); //pset.getProtocolReqRender("75"); + Proto8018 proto = new Proto8018(); byte[] dn = new byte[256]; byte[] s = null; try { @@ -9828,7 +9829,7 @@ public class SydApi4j implements SydApi { @Override public boolean attachedVerify(String sign) { - Proto8019 proto = new Proto8019(); //pset.getProtocolReqRender("75"); + Proto8019 proto = new Proto8019(); // 填充数据 HashMap packet = new HashMap(); @@ -9875,7 +9876,7 @@ public class SydApi4j implements SydApi { * 返回CERT_INFO结构的证书数据 */ public X509 attachedVerifyAndGetX509(String sign) { - Proto8019 proto = new Proto8019(); //pset.getProtocolReqRender("75"); + Proto8019 proto = new Proto8019(); // 填充数据 HashMap packet = new HashMap(); diff --git a/src/test/java/com/sunyard/sydapi/example/ABCNorthBranch.java b/src/test/java/com/sunyard/sydapi/example/ABCNorthBranch.java index c5aeca1..30d3403 100644 --- a/src/test/java/com/sunyard/sydapi/example/ABCNorthBranch.java +++ b/src/test/java/com/sunyard/sydapi/example/ABCNorthBranch.java @@ -1,13 +1,16 @@ package com.sunyard.sydapi.example; import com.sunyard.RetWrap; +import com.sunyard.SydApi; import com.sunyard.proto.Util; +import com.sunyard.util.SYMUtil; import org.junit.After; import org.junit.Before; import org.junit.Test; import racal.sunyard.main.SydApi4j; import java.io.UnsupportedEncodingException; +import java.util.Base64; import static com.sunyard.sydapi.util.PrintUtil.printByteArray; import static com.sunyard.sydapi.util.PrintUtil.printRetWrap; @@ -20,7 +23,7 @@ public class ABCNorthBranch { SydApi4j api; // SM2密钥索引 - String SM2PrivateKeyTag = "111111111111111800000000001"; + String SM2PrivateKeyTag = "111111111111115100000000001"; // SM2公钥 String SM2PublicKey = null; // SM2私钥 @@ -85,19 +88,39 @@ public class ABCNorthBranch { System.out.println(s); } - // 测试通过 // 75 @Test - public void testSM2Verify2() { - String thisOrgData = "FFFFFFFFFFFFFFFF"; + public void testSM2SignAndVerify() { + String orgData = "11111111111111"; - String s = api.SM2SignC(0, SM2PrivateKey, Util.hexString2Bytes(SM2PublicKey), thisOrgData.getBytes()); + // 这里,不管是签名还是验签,都需要在外部做一次hash。 + byte[] hash = api.SM3Hash(SM2PublicKey, orgData.getBytes()); - System.out.println(s); - boolean b = api.SM2Verify( Util.hexString2Bytes(SM2PublicKey), thisOrgData.getBytes(), s); - System.out.println(b); + /** + * 用私钥签名(外部私钥) + * @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 diff --git a/src/test/java/com/sunyard/sydapi/test/TestAllMethods.java b/src/test/java/com/sunyard/sydapi/test/TestAllMethods.java index bde421a..2d33d87 100644 --- a/src/test/java/com/sunyard/sydapi/test/TestAllMethods.java +++ b/src/test/java/com/sunyard/sydapi/test/TestAllMethods.java @@ -775,7 +775,7 @@ public class TestAllMethods { // LF @Test public void testReadKey() { - String index = "333333333333333300A00000001"; + String index = "000000000000000100022222222"; RetWrap retWrap = api.readKey(index); printRetWrap(retWrap); @@ -1011,6 +1011,7 @@ public class TestAllMethods { byte[] oData = api.SM3Hash(pk, Util.hexString2Bytes(orgData)); String s = api.SYD_SM2_Sign_HA(SydApi.DATA_HASH, Util.hexString2Bytes(sk), Util.hexString2Bytes(pk), oData); + System.out.println("sign:" + s); System.out.println("decBase64:" + Util.bytes2HexString(Util.decodeBase64(s))); System.out.println("decBase64-1:" + Util.bytes2HexString(SYMUtil.base64SignAsDerSign(s))); boolean b = api.SYD_SM2_Verify_HA(SydApi.DATA_HASH, Util.hexString2Bytes(pk), Util.hexString2Bytes(orgData), s); @@ -1663,10 +1664,10 @@ public class TestAllMethods { @Test public void testSM2Verify3() { - String thisPublicKey = "03420004C0D7FCD41C4A27B6B2DAC20B1332ED89202F2F85EF79929CAD1028F11AE0E8EFECA9C26A2145773F234631B2D2F1C7DAE5880C681122B28DA842509CEC8DF82D"; + String thisPublicKey = "034200048043AF118B45629857DB693105849B2617FED249D59DFD29BE9A891ADEE7441A2D8C62020953DAEF038184E0244310CF2123C507F26F5380FFECC8D53E79B24E"; String thisOrgData = "FFFFFFFFFFFFFFFF"; - String s = api.SM2SignC(1, "000000000000000100000000001", Util.hexString2Bytes(thisPublicKey), thisOrgData.getBytes()); + String s = api.SM2SignC(1, "111111111111115100100000001", Util.hexString2Bytes(thisPublicKey), thisOrgData.getBytes()); System.out.println(s); System.out.println("DecBase64:" + Util.bytes2HexString(Base64.getDecoder().decode(s))); @@ -1691,6 +1692,26 @@ public class TestAllMethods { System.out.println(b); } + /** + * 外部做hash,避免当原始数据过长时解析参数时ByteBuffer长度不够时报错的问题 + */ + // 测试通过 + // 75 + @Test + public void testSM2SignAndVerify() { + String privateKey = "000100009FBA90C22E18982D60022C683219F72DAAA1B878A094181381486FC5137B1B160000000000000000000000000000000000000000000000000000000000000000"; + String thisPublicKey = "03420004E5888368B9D5BC319FE48AB92123857E696EFB1A7E08D75DD5C3C5A5E9FA18099EFFCAFD61A0838527D36C7FA6AE8E2649FB89883FC44A494830CE642A03DC24"; + String orgData = "11111111111111"; + + // 这里,不管是签名还是验签,都需要在外部做一次hash。 + byte[] hash = api.SM3Hash(thisPublicKey, orgData.getBytes()); + String s = api.SM2Sign(SydApi.DATA_HASH, Util.hexString2Bytes(privateKey), hash); + + System.out.println("decBase64:" + Util.bytes2HexString(Util.decodeBase64(s))); + boolean b = api.SM2Verify(SydApi.DATA_HASH, Util.hexString2Bytes(thisPublicKey), hash, s); + System.out.println("验签结果: " + b); + } + // 测试通过 // 77 @Test