DN reverse bug fix
This commit is contained in:
parent
15124bf487
commit
3cd9e78990
@ -187,7 +187,7 @@
|
||||
}
|
||||
|
||||
function dataLen() {
|
||||
return $packet.data.orgData.length()
|
||||
return $packet.data.orgData.length
|
||||
}
|
||||
|
||||
<![CDATA[
|
||||
|
||||
@ -226,7 +226,7 @@ public interface SydGenApi {
|
||||
* SessionKey 本地LMK 加密的会话密钥,HEX 格式, 本地 LMK 加密的会话密钥长度,HEX 格式(不包括第 1 个字符),一般为 33 个 Bytes。默认输入值为缓冲区的大小。
|
||||
* KCV 会话密钥的检验值,HEX 格式,长度为 32H。
|
||||
*/
|
||||
RetWrap generateDEByDn(String certSerial, String oData);
|
||||
RetWrap generateDEByDn(String certSerial, byte[] oData);
|
||||
|
||||
/**
|
||||
* 产生对称密钥,通过公钥加密生成网联格式的数字信封。
|
||||
@ -240,7 +240,7 @@ public interface SydGenApi {
|
||||
* SessionKey 本地LMK 加密的会话密钥,HEX 格式, 本地 LMK 加密的会话密钥长度,HEX 格式(不包括第 1 个字符),一般为 33 个 Bytes。默认输入值为缓冲区的大小。
|
||||
* KCV 会话密钥的检验值,HEX 格式,长度为 32H。
|
||||
*/
|
||||
RetWrap generateDE(byte[] certSerial, int alg, int model, String oData);
|
||||
RetWrap generateDE(byte[] certSerial, int alg, int model, byte[] oData);
|
||||
|
||||
/**
|
||||
* 产生对称密钥,通过公钥加密生成网联格式的数字信封。
|
||||
@ -308,7 +308,7 @@ public interface SydGenApi {
|
||||
* KCV 会话密钥的检验值,HEX 格式,长度为 36H。
|
||||
* orgCode 如果有机构码的话会返回机构码
|
||||
*/
|
||||
RetWrap decryptDEByDN(String certSerial, String pCipherKey);
|
||||
RetWrap decryptDEByDN(String certSerial, byte[] pCipherKey);
|
||||
|
||||
/**
|
||||
* 接收用私钥解密数字信封,还原对称密钥。
|
||||
|
||||
@ -177,7 +177,7 @@ interface SydAttachedSVApi {
|
||||
* @return
|
||||
* 符合PKCS7标准的签名结果
|
||||
*/
|
||||
String attachedSign(byte[] orgData, String sCertDN);
|
||||
byte[] attachedSign(byte[] orgData, String sCertDN);
|
||||
|
||||
/**
|
||||
* 带公钥证书与原始数据的数字签名验签(遵循 PKCS#7)
|
||||
@ -186,7 +186,7 @@ interface SydAttachedSVApi {
|
||||
* @param sign 已签名数据
|
||||
* @return true 验签通过 false 验签失败
|
||||
*/
|
||||
boolean attachedVerify(String sign);
|
||||
boolean attachedVerify(byte[] sign);
|
||||
|
||||
/**
|
||||
* 带公钥证书与原始数据的数字签名验签(遵循 PKCS#7)
|
||||
@ -195,7 +195,7 @@ interface SydAttachedSVApi {
|
||||
* @param sign 已签名数据
|
||||
* @return 成功返回签名的原数据,失败则报错
|
||||
*/
|
||||
String attachedVerifyRD(String sign);
|
||||
byte[] attachedVerifyRD(byte[] sign);
|
||||
|
||||
/**
|
||||
* 带公钥证书与原始数据的数字签名验签(遵循 PKCS#7)
|
||||
@ -206,7 +206,7 @@ interface SydAttachedSVApi {
|
||||
* 若发送报文返回证书信息标志位为0,则返回已签名数据,
|
||||
* 若发送报文返回证书信息标志位为1,则返回CERT_INFO结构的证书数据
|
||||
*/
|
||||
X509 attachedVerifyAndGetX509(String sign);
|
||||
X509 attachedVerifyAndGetX509(byte[] sign);
|
||||
|
||||
/**
|
||||
* 带公钥证书与原始数据的数字签名验签(遵循 PKCS#7)
|
||||
@ -217,7 +217,7 @@ interface SydAttachedSVApi {
|
||||
* 若发送报文返回证书信息标志位为0,则返回已签名数据,
|
||||
* 若发送报文返回证书信息标志位为1,则返回CERT_INFO结构的证书数据
|
||||
*/
|
||||
RetWrap attachedVerifyAndGetAll(String sign);
|
||||
RetWrap attachedVerifyAndGetAll(byte[] sign);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package com.sunyard.util;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class DnUtil {
|
||||
|
||||
@ -20,14 +21,20 @@ public class DnUtil {
|
||||
return dn;
|
||||
}
|
||||
// 反转顺序
|
||||
LinkedHashMap<String, String> map = dnToMap( dn );
|
||||
String[] kvs = dn.split(",");
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for ( String key : map.keySet() ) {
|
||||
for ( String kv : kvs ) {
|
||||
|
||||
if ( sb.length() > 0 ) {
|
||||
sb.insert(0, ",");
|
||||
}
|
||||
sb.insert(0, key + "=" + map.get( key ));
|
||||
sb.insert(0, kv );
|
||||
|
||||
// if ( sb.length() > 0 ) {
|
||||
// sb.append(",");
|
||||
// }
|
||||
// sb.append( key + "=" + map.get( key ) );
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
|
||||
@ -2302,15 +2302,15 @@ public class SydApi4j implements SydApi {
|
||||
}
|
||||
|
||||
@Override
|
||||
public RetWrap generateDEByDn(String certSerial, String oData) {
|
||||
if ( oData.length() <= 4 * 1024 ) {
|
||||
public RetWrap generateDEByDn(String certSerial, byte[] oData) {
|
||||
if ( oData.length <= 4 * 1024 ) {
|
||||
return generateDEByDnHsm( certSerial, oData );
|
||||
} else {
|
||||
return generateDEByDnSoft( certSerial, oData );
|
||||
}
|
||||
}
|
||||
|
||||
public RetWrap generateDEByDnHsm(String certSerial, String oData) {
|
||||
public RetWrap generateDEByDnHsm(String certSerial, byte[] oData) {
|
||||
|
||||
certSerial = DnUtil.verifyDn( certSerial );
|
||||
|
||||
@ -2324,7 +2324,7 @@ public class SydApi4j implements SydApi {
|
||||
}
|
||||
|
||||
@Override
|
||||
public RetWrap generateDE(byte[] certSerial, int alg, int model, String oData) {
|
||||
public RetWrap generateDE(byte[] certSerial, int alg, int model, byte[] oData) {
|
||||
Proto6A proto = new Proto6A();
|
||||
// 填充数据
|
||||
PacketSN sn = PacketSN.gen();
|
||||
@ -2368,12 +2368,12 @@ public class SydApi4j implements SydApi {
|
||||
throw new SydApiException(error.getInfo(), error.autoInt());
|
||||
}
|
||||
RetWrap rw = new RetWrap();
|
||||
rw.put(SydApi.RET_CIPHER_KEY, ((PacketSection) ret.get("key")).getString());
|
||||
rw.put(SydApi.RET_SESSION_KEY, ((PacketSection) ret.get("lmkHead")).getString() +
|
||||
((PacketSection) ret.get("lmk")).getString()
|
||||
rw.put(SydApi.RET_CIPHER_KEY, ((PacketSection) ret.get("key")).getBytes() );
|
||||
rw.put(SydApi.RET_SESSION_KEY, (((PacketSection) ret.get("lmkHead")).getString() +
|
||||
((PacketSection) ret.get("lmk")).getString()).getBytes()
|
||||
);
|
||||
|
||||
rw.put(SydApi.RET_KCV, ((PacketSection) ret.get("keyCheck")).getString());
|
||||
rw.put(SydApi.RET_KCV, ((PacketSection) ret.get("keyCheck")).getBytes());
|
||||
|
||||
return rw;
|
||||
}
|
||||
@ -2528,7 +2528,7 @@ public class SydApi4j implements SydApi {
|
||||
return (X509Certificate) cf.generateCertificate(is);
|
||||
}
|
||||
|
||||
public RetWrap generateDEByDnSoft(String certSerial, String oData) {
|
||||
public RetWrap generateDEByDnSoft(String certSerial, byte[] oData) {
|
||||
|
||||
certSerial = DnUtil.verifyDn( certSerial );
|
||||
|
||||
@ -2561,15 +2561,15 @@ public class SydApi4j implements SydApi {
|
||||
|
||||
// 生成会话密钥
|
||||
ret = SM2GenSessionKey(pk);
|
||||
String ciperKey = ret.get(SydApi.RET_CIPHER_KEY).toString();
|
||||
String sessionKeyByLMK = ret.get(SydApi.RET_SESSION_KEY).toString();
|
||||
String ciperKey = new String( (byte[]) ret.get(SydApi.RET_CIPHER_KEY));
|
||||
String sessionKeyByLMK = new String( (byte[]) ret.get(SydApi.RET_SESSION_KEY ));
|
||||
|
||||
println("ciperKey=" + ciperKey);
|
||||
|
||||
// 加密数据
|
||||
byte[] iv = new byte[32];
|
||||
Arrays.fill(iv, (byte) 0x30);
|
||||
RetWrap retWrap = SYMEnDeData(true, 1, sessionKeyByLMK, 1, 1, oData.getBytes(), iv, 0);
|
||||
RetWrap retWrap = SYMEnDeData(true, 1, sessionKeyByLMK, 1, 1, oData, iv, 0);
|
||||
byte[] enData = (byte[]) retWrap.get("msg_all");
|
||||
|
||||
// 组织格式
|
||||
@ -2578,7 +2578,7 @@ public class SydApi4j implements SydApi {
|
||||
dnToMap(issuser.getName() )
|
||||
);
|
||||
|
||||
ret.put(SydApi.RET_CIPHER_KEY, Util.encodeBase64(cms.getEncoded()));
|
||||
ret.put(SydApi.RET_CIPHER_KEY, Util.encodeBase64(cms.getEncoded()).getBytes() );
|
||||
|
||||
return ret;
|
||||
|
||||
@ -2658,11 +2658,11 @@ public class SydApi4j implements SydApi {
|
||||
return info;
|
||||
}
|
||||
|
||||
public RetWrap decryptDEByDN(String certSerial, String pCipherKey){
|
||||
if ( pCipherKey.length() <= 4 * 1024 ) {
|
||||
return decryptDEByDNHsm( certSerial, pCipherKey );
|
||||
public RetWrap decryptDEByDN(String certSerial, byte[] pCipherKey){
|
||||
if ( pCipherKey.length <= 4 * 1024 ) {
|
||||
return decryptDEByDNHsm( certSerial, new String(pCipherKey) );
|
||||
} else {
|
||||
return decryptDEByDNSoft( certSerial, pCipherKey );
|
||||
return decryptDEByDNSoft( certSerial, new String(pCipherKey) );
|
||||
}
|
||||
}
|
||||
|
||||
@ -2683,7 +2683,7 @@ public class SydApi4j implements SydApi {
|
||||
|
||||
// 解密会话密钥
|
||||
ret = decryptDE(false, 1, dnBytes, Util.encodeBase64(sessionKeyDer) );
|
||||
String sessionKey = (String) ret.get("SessionKey");
|
||||
String sessionKey = new String((byte[]) ret.get("SessionKey") );
|
||||
|
||||
// 解密原文
|
||||
byte[] iv = new byte[32];
|
||||
@ -2747,19 +2747,19 @@ public class SydApi4j implements SydApi {
|
||||
RetWrap rw = new RetWrap();
|
||||
|
||||
if (pFlag) {
|
||||
rw.put("oData", ((PacketSection) ret.get("oData")).getString());
|
||||
rw.put("oData", ((PacketSection) ret.get("oData")).getBytes());
|
||||
}
|
||||
|
||||
int orgCodeLen = Util.a2i(((PacketSection) ret.get("orgCodeLen")).getBytes(), 10);
|
||||
if (orgCodeLen != 0) {
|
||||
rw.put("orgCode", ((PacketSection) ret.get("orgCode")).getString());
|
||||
rw.put("orgCode", ((PacketSection) ret.get("orgCode")).getBytes());
|
||||
}
|
||||
|
||||
rw.put(SydApi.RET_SESSION_KEY, ((PacketSection) ret.get("lmkHead")).getString() +
|
||||
((PacketSection) ret.get("lmk")).getString()
|
||||
rw.put(SydApi.RET_SESSION_KEY, (((PacketSection) ret.get("lmkHead")).getString() +
|
||||
((PacketSection) ret.get("lmk")).getString()).getBytes()
|
||||
);
|
||||
|
||||
rw.put(SydApi.RET_KCV, ((PacketSection) ret.get("keyCheck")).getString());
|
||||
rw.put(SydApi.RET_KCV, ((PacketSection) ret.get("keyCheck")).getBytes());
|
||||
|
||||
return rw;
|
||||
}
|
||||
@ -5141,14 +5141,15 @@ public class SydApi4j implements SydApi {
|
||||
throw new SydApiException(error.getInfo(), error.autoInt());
|
||||
}
|
||||
RetWrap rw = new RetWrap();
|
||||
rw.put(SydApi.RET_CIPHER_KEY, Util.bytes2HexString(
|
||||
rw.put(SydApi.RET_CIPHER_KEY,
|
||||
((PacketSection) ret.get("key")).getBytes()
|
||||
));
|
||||
rw.put(SydApi.RET_SESSION_KEY, ((PacketSection) ret.get("lmkHead")).getString() +
|
||||
((PacketSection) ret.get("lmk")).getString()
|
||||
);
|
||||
|
||||
rw.put(SydApi.RET_KCV, ((PacketSection) ret.get("keyCheck")).getString());
|
||||
rw.put(SydApi.RET_SESSION_KEY, (((PacketSection) ret.get("lmkHead")).getString() +
|
||||
((PacketSection) ret.get("lmk")).getString()).getBytes()
|
||||
);
|
||||
|
||||
rw.put(SydApi.RET_KCV, ((PacketSection) ret.get("keyCheck")).getBytes());
|
||||
|
||||
return rw;
|
||||
}
|
||||
@ -10412,7 +10413,7 @@ public class SydApi4j implements SydApi {
|
||||
* @return 符合PKCS7标准的签名结果
|
||||
*/
|
||||
@Override
|
||||
public String attachedSign(byte[] orgData, String sCertDN) {
|
||||
public byte[] attachedSign(byte[] orgData, String sCertDN) {
|
||||
sCertDN = DnUtil.verifyDn( sCertDN );
|
||||
|
||||
Proto8018 proto = new Proto8018();
|
||||
@ -10459,11 +10460,11 @@ public class SydApi4j implements SydApi {
|
||||
throw new SydApiException(error.getInfo(), error.autoInt());
|
||||
}
|
||||
|
||||
return ((PacketSection) ret.get("signature")).getString();
|
||||
return ((PacketSection) ret.get("signature")).getString().getBytes();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean attachedVerify(String sign) {
|
||||
public boolean attachedVerify(byte[] sign) {
|
||||
Proto8019 proto = new Proto8019();
|
||||
|
||||
// 填充数据
|
||||
@ -10473,7 +10474,7 @@ public class SydApi4j implements SydApi {
|
||||
PacketSN sn = PacketSN.gen();
|
||||
packet.put("header", sn.getSn().array());
|
||||
|
||||
data.put("signData", sign.getBytes());
|
||||
data.put("signData", sign);
|
||||
data.put("flag$option", 0);
|
||||
|
||||
// 渲染发送
|
||||
@ -10506,12 +10507,12 @@ public class SydApi4j implements SydApi {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String attachedVerifyRD(String sign) {
|
||||
public byte[] attachedVerifyRD(byte[] sign) {
|
||||
RetWrap retWrap = attachedVerifyAndGetAll(sign);
|
||||
if (retWrap == null)
|
||||
return null;
|
||||
|
||||
return (String) retWrap.get("oData");
|
||||
return (byte[]) retWrap.get("oData");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -10521,15 +10522,15 @@ public class SydApi4j implements SydApi {
|
||||
* @param sign 已签名数据
|
||||
* @return 返回CERT_INFO结构的证书数据
|
||||
*/
|
||||
public X509 attachedVerifyAndGetX509(String sign) {
|
||||
RetWrap retWrap = attachedVerifyAndGetAll(sign);
|
||||
public X509 attachedVerifyAndGetX509(byte[] sign) {
|
||||
RetWrap retWrap = attachedVerifyAndGetAll( sign);
|
||||
if (retWrap == null)
|
||||
return null;
|
||||
|
||||
return (X509) retWrap.get("x509");
|
||||
}
|
||||
|
||||
public RetWrap attachedVerifyAndGetAll(String sign) {
|
||||
public RetWrap attachedVerifyAndGetAll(byte[] sign) {
|
||||
Proto8019 proto = new Proto8019();
|
||||
|
||||
// 填充数据
|
||||
@ -10539,7 +10540,7 @@ public class SydApi4j implements SydApi {
|
||||
PacketSN sn = PacketSN.gen();
|
||||
packet.put("header", sn.getSn().array());
|
||||
|
||||
data.put("signData", sign.getBytes());
|
||||
data.put("signData", sign);
|
||||
data.put("flag$option", 1);
|
||||
|
||||
// 渲染发送
|
||||
@ -10579,7 +10580,7 @@ public class SydApi4j implements SydApi {
|
||||
|
||||
RetWrap retWrap = new RetWrap();
|
||||
retWrap.put("x509", x509);
|
||||
retWrap.put("oData", new String(oDataB));
|
||||
retWrap.put("oData", oDataB );
|
||||
|
||||
return retWrap;
|
||||
}
|
||||
|
||||
@ -22,16 +22,11 @@ public class ShangTest {
|
||||
}
|
||||
|
||||
public void action(){
|
||||
String sign = api.attachedSign(testData, dn);
|
||||
byte[] sign = api.attachedSign(testData, dn);
|
||||
boolean ret = api.attachedVerify( sign );
|
||||
|
||||
assert ret;
|
||||
|
||||
sign = api.SYD_DetachedSign(testData, dn);
|
||||
ret = api.SYD_DetachedVerify( testData, sign );
|
||||
|
||||
assert ret;
|
||||
|
||||
}
|
||||
|
||||
public void end(){
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -4,17 +4,13 @@ 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.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import racal.sunyard.main.SydApi4j;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
/**
|
||||
* 衡泰例子
|
||||
@ -26,7 +22,7 @@ public class TestHT {
|
||||
@Before
|
||||
public void before() {
|
||||
// 通过设置debug的属性开启debug,如果不设置则默认不打开
|
||||
// System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
||||
System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
||||
api = (SydApi4j) new SydApi4j().connect("172.16.18.59", 8889, null, 1000);
|
||||
// api = (SydApi4j) new SydApi4j().connect("192.168.0.66", 8889, null, 1000);
|
||||
}
|
||||
@ -40,97 +36,35 @@ public class TestHT {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 流程演示
|
||||
*/
|
||||
@Test
|
||||
public void testDecryptDE5() throws UnsupportedEncodingException {
|
||||
public void testProcess() throws UnsupportedEncodingException {
|
||||
String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1";
|
||||
String oData = "00001111222233";
|
||||
byte[] orgData = "111111213323143243243".getBytes("UTF-8");
|
||||
|
||||
RetWrap retWrap = api.generateDEByDn(dn, oData);
|
||||
String cipherKey = (String) retWrap.get(SydApi.RET_CIPHER_KEY);
|
||||
System.out.println("数字信封: " + cipherKey);
|
||||
// 签名
|
||||
byte[] sign = api.attachedSign(orgData, dn);
|
||||
System.out.println("签名结果: " + Util.bytes2HexString(sign) );
|
||||
|
||||
// 生成数字信封
|
||||
RetWrap retWrap = api.generateDEByDn(dn, sign);
|
||||
byte[] cipherKey = (byte[]) retWrap.get(SydApi.RET_CIPHER_KEY);
|
||||
System.out.println("数字信封: " + new String(cipherKey));
|
||||
|
||||
// 解密数字信封
|
||||
RetWrap retWrap1 = api.decryptDEByDN(dn, cipherKey);
|
||||
byte[] oData = (byte[]) retWrap1.get("oData");
|
||||
System.out.println("原数据: " + retWrap1.get("oData"));
|
||||
|
||||
// 验签
|
||||
RetWrap verify = api.attachedVerifyAndGetAll(oData);
|
||||
if (verify != null) {
|
||||
System.out.println("x509公钥: " + Util.bytes2HexString(((X509)verify.get("x509")).getPubKey()));
|
||||
System.out.println("签名原数据: " + Util.bytes2HexString((byte[])(verify.get("oData"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecryptDE5Soft1() throws UnsupportedEncodingException {
|
||||
String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1";
|
||||
byte[] test = new byte[ 4 * 1024 ];
|
||||
Arrays.fill( test, (byte) 0x30 );
|
||||
String oData = new String(test);
|
||||
|
||||
RetWrap retWrap = api.generateDEByDn(dn, oData);
|
||||
String cipherKey = (String) retWrap.get(SydApi.RET_CIPHER_KEY);
|
||||
System.out.println("数字信封: " + cipherKey);
|
||||
|
||||
RetWrap retWrap1 = api.decryptDEByDN(dn, cipherKey);
|
||||
System.out.println("原数据: " + retWrap1.get("oData"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecryptDE5Soft2() throws UnsupportedEncodingException {
|
||||
String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1";
|
||||
String oData = "00001111222233000011112222330000111122223300001111222233000011112222330000111122223300001111222233";
|
||||
|
||||
RetWrap retWrap = api.generateDEByDnHsm(dn, oData);
|
||||
String cipherKey = (String) retWrap.get(SydApi.RET_CIPHER_KEY);
|
||||
System.out.println("数字信封: " + cipherKey);
|
||||
|
||||
RetWrap retWrap1 = api.decryptDEByDNSoft(dn, cipherKey);
|
||||
byte[] cat = (byte[]) retWrap1.get("oData");
|
||||
System.out.println("原数据: " + new String( cat ));
|
||||
|
||||
Assert.assertEquals( oData, new String( cat ));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testDecryptDE5Soft3() throws UnsupportedEncodingException {
|
||||
|
||||
long t1 = System.currentTimeMillis();
|
||||
System.out.println("start");
|
||||
|
||||
String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1";
|
||||
byte[] aData = new byte[ 20 * 1024 * 1024 ];
|
||||
Arrays.fill( aData, (byte) 0x55 );
|
||||
|
||||
String oData = new String( aData );
|
||||
RetWrap retWrap = api.generateDEByDnSoft(dn, oData);
|
||||
String cipherKey = (String) retWrap.get(SydApi.RET_CIPHER_KEY);
|
||||
System.out.println("数字信封: " + cipherKey);
|
||||
write("./cms.p7", cipherKey.getBytes());
|
||||
long t2 = System.currentTimeMillis();
|
||||
System.out.println("加密耗时=" + ( t2 - t1 ));
|
||||
|
||||
RetWrap retWrap1 = api.decryptDEByDNSoft(dn, cipherKey);
|
||||
byte[] cat = (byte[]) retWrap1.get("oData");
|
||||
System.out.println("原数据: " + new String( cat ));
|
||||
long t3 = System.currentTimeMillis();
|
||||
System.out.println("解密耗时=" + ( t3 - t2 ));
|
||||
System.out.println("总耗时=" + ( t3 - t1 ));
|
||||
|
||||
Assert.assertEquals( oData, new String( cat ));
|
||||
}
|
||||
|
||||
public static void write(String path, byte[] data){
|
||||
try {
|
||||
File f = new File( path );
|
||||
if ( f.exists() ) {
|
||||
f.createNewFile();
|
||||
}
|
||||
FileOutputStream out = new FileOutputStream( f );
|
||||
try {
|
||||
out.write( data );
|
||||
out.flush();
|
||||
out.close();
|
||||
} finally {
|
||||
out.close();
|
||||
}
|
||||
}catch ( Exception e ){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
@ -139,8 +73,8 @@ public class TestHT {
|
||||
@Test
|
||||
public void testSM2ConfirmSessionKeyC() {
|
||||
|
||||
String userId = "1111111111111151";
|
||||
String data = "12343242";
|
||||
String userId = "8012";
|
||||
byte[] data = "12343242".getBytes();
|
||||
|
||||
// 通过用户id获取公钥
|
||||
String pk = api.SM2GetPublicKeyC(userId, SydApi.FOR_ENCRYPT);
|
||||
@ -153,7 +87,7 @@ public class TestHT {
|
||||
String kcv = (String) retWrap.get(SydApi.RET_KCV);
|
||||
|
||||
// 使用生成的对称密钥加密数据
|
||||
byte[] dataEnc = api.SYMEncryptData(SydApi.NALG_SM4, sessionKey, data.getBytes());
|
||||
byte[] dataEnc = api.SYMEncryptData(SydApi.NALG_SM4, sessionKey, data);
|
||||
|
||||
// 解密数字信封
|
||||
RetWrap retWrap1 = api.SM2ConfirmSessionKeyC(userId, cipherKey);
|
||||
@ -172,8 +106,8 @@ public class TestHT {
|
||||
public void testAttachedSignAndVerify() {
|
||||
String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1";
|
||||
String orgData = "111111213323143243243";
|
||||
String sign = api.attachedSign(orgData.getBytes(), dn);
|
||||
System.out.println("签名结果: " + sign);
|
||||
byte[] sign = api.attachedSign(orgData.getBytes(), dn);
|
||||
System.out.println("签名结果: " + Util.bytes2HexString(sign) );
|
||||
|
||||
boolean verify = api.attachedVerify(sign);
|
||||
System.out.println("验签结果: " + verify);
|
||||
@ -182,9 +116,9 @@ public class TestHT {
|
||||
@Test
|
||||
public void testAttachedSignAndVerifyGetX509() {
|
||||
String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1";
|
||||
String orgData = "111111213323143243243";
|
||||
String sign = api.attachedSign(orgData.getBytes(), dn);
|
||||
System.out.println("签名结果: " + sign);
|
||||
byte[] orgData = "111111213323143243243".getBytes();
|
||||
byte[] sign = api.attachedSign(orgData, dn);
|
||||
System.out.println("签名结果: " + Util.bytes2HexString(sign) );
|
||||
|
||||
X509 verify = api.attachedVerifyAndGetX509(sign);
|
||||
System.out.println("验签结果: " + Util.bytes2HexString(verify.getPubKey()));
|
||||
@ -194,19 +128,19 @@ public class TestHT {
|
||||
public void testAttachedSignAndVerifyRD() {
|
||||
String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1";
|
||||
String orgData = "111111213323143243243";
|
||||
String sign = api.attachedSign(orgData.getBytes(), dn);
|
||||
System.out.println("签名结果: " + sign);
|
||||
byte[] sign = api.attachedSign(orgData.getBytes(), dn);
|
||||
System.out.println("签名结果: " + Util.bytes2HexString(sign));
|
||||
|
||||
String verify = api.attachedVerifyRD(sign);
|
||||
System.out.println("验签结果: " + verify);
|
||||
byte[] verify = api.attachedVerifyRD(sign);
|
||||
System.out.println("验签结果: " + Util.bytes2HexString(verify));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAttachedSignAndVerifyGetAll() {
|
||||
String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1";
|
||||
String orgData = "111111213323143243243";
|
||||
String sign = api.attachedSign(orgData.getBytes(), dn);
|
||||
System.out.println("签名结果: " + sign);
|
||||
byte[] orgData = "111111213323143243243".getBytes();
|
||||
byte[] sign = api.attachedSign(orgData, dn);
|
||||
System.out.println("签名结果: " + Util.bytes2HexString(sign));
|
||||
|
||||
RetWrap verify = api.attachedVerifyAndGetAll(sign);
|
||||
if (verify != null) {
|
||||
@ -215,50 +149,69 @@ public class TestHT {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecryptDE54k() throws UnsupportedEncodingException {
|
||||
String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1";
|
||||
byte[] oData = new byte[ 4 * 1024 ];
|
||||
|
||||
RetWrap retWrap = api.generateDEByDn(dn, oData);
|
||||
byte[] CipherKey = (byte[]) retWrap.get(SydApi.RET_CIPHER_KEY);
|
||||
byte[] sessionKey = (byte[]) retWrap.get(SydApi.RET_SESSION_KEY);
|
||||
printRetWrap(retWrap);
|
||||
|
||||
RetWrap retWrap1 = api.decryptDEByDN(dn, CipherKey);
|
||||
printRetWrap(retWrap1);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getCert(){
|
||||
String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1";
|
||||
String cert = api.GetX509CertFileBySN("1111111111111151", SydApi.FOR_ENCRYPT );
|
||||
System.out.println( cert );
|
||||
public void testDecryptDE54kr() throws UnsupportedEncodingException {
|
||||
String dn = "CN=YCCA@黄金交易所@Zhuangj@1,OU=Individual-2,OU=YCCA,O=CFCA OCA1,C=CN";
|
||||
byte[] oData = new byte[ 4 * 1024 ];
|
||||
|
||||
RetWrap retWrap = api.generateDEByDn(dn, oData);
|
||||
byte[] CipherKey = (byte[]) retWrap.get(SydApi.RET_CIPHER_KEY);
|
||||
byte[] sessionKey = (byte[]) retWrap.get(SydApi.RET_SESSION_KEY);
|
||||
printRetWrap(retWrap);
|
||||
|
||||
RetWrap retWrap1 = api.decryptDEByDN(dn, CipherKey);
|
||||
printRetWrap(retWrap1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 流程演示
|
||||
*/
|
||||
@Test
|
||||
public void testProcess() {
|
||||
public void testDecryptDE520M() throws UnsupportedEncodingException {
|
||||
String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1";
|
||||
String orgData = "111111213323143243243";
|
||||
byte[] oData = new byte[ 20 * 1024 * 1024 ];
|
||||
|
||||
// 签名
|
||||
String sign = api.attachedSign(orgData.getBytes(), dn);
|
||||
System.out.println("签名结果: " + sign);
|
||||
RetWrap retWrap = api.generateDEByDn(dn, oData);
|
||||
byte[] CipherKey = (byte[]) retWrap.get(SydApi.RET_CIPHER_KEY);
|
||||
byte[] sessionKey = (byte[]) retWrap.get(SydApi.RET_SESSION_KEY);
|
||||
printRetWrap(retWrap);
|
||||
|
||||
// 生成数字信封
|
||||
RetWrap retWrap = api.generateDEByDn(dn, sign);
|
||||
String cipherKey = (String) retWrap.get(SydApi.RET_CIPHER_KEY);
|
||||
System.out.println("数字信封: " + cipherKey);
|
||||
|
||||
// 解密数字信封
|
||||
RetWrap retWrap1 = api.decryptDEByDN(dn, cipherKey);
|
||||
String oData = (String) retWrap1.get("oData");
|
||||
System.out.println("原数据: " + retWrap1.get("oData"));
|
||||
|
||||
// 验签
|
||||
RetWrap verify = api.attachedVerifyAndGetAll(oData);
|
||||
if (verify != null) {
|
||||
System.out.println("x509公钥: " + Util.bytes2HexString(((X509)verify.get("x509")).getPubKey()));
|
||||
System.out.println("签名原数据: " + verify.get("oData"));
|
||||
RetWrap retWrap1 = api.decryptDEByDN(dn, CipherKey);
|
||||
printRetWrap(retWrap1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecryptDE5() throws UnsupportedEncodingException {
|
||||
String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1";
|
||||
byte[] oData = "00001111222233".getBytes();
|
||||
|
||||
RetWrap retWrap = api.generateDEByDn(dn, oData);
|
||||
byte[] CipherKey = (byte[]) retWrap.get(SydApi.RET_CIPHER_KEY);
|
||||
byte[] sessionKey = (byte[]) retWrap.get(SydApi.RET_SESSION_KEY);
|
||||
printRetWrap(retWrap);
|
||||
|
||||
RetWrap retWrap1 = api.decryptDEByDN(dn, CipherKey);
|
||||
printRetWrap(retWrap1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// 测试通过
|
||||
// 76
|
||||
@Test
|
||||
@ -271,6 +224,7 @@ public class TestHT {
|
||||
}
|
||||
|
||||
|
||||
|
||||
// =============================工具方法==============================================
|
||||
|
||||
public static void printRetWrap(RetWrap rw) {
|
||||
|
||||
@ -1,104 +1,104 @@
|
||||
package com.sunyard.sydapi.test;
|
||||
|
||||
|
||||
import com.sunyard.SydApi;
|
||||
import com.sunyard.proto.Util;
|
||||
import racal.sunyard.main.SydApi4j;
|
||||
import racal.sunyard.main.SydApiBuilder;
|
||||
|
||||
import static com.sunyard.SydApi.*;
|
||||
|
||||
|
||||
public class TestSV {
|
||||
|
||||
private static final String inHexData = "67c6697351ff4aec29cdbaabf2fbe346";
|
||||
private static final String dn = "C=CN,ST=浙江,L=杭州,O=信雅达测试123,OU=安全应用1,OU=安全应用2,CN=@信雅达安全测试";
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
||||
// 连接加密机
|
||||
SydApi4j api = (SydApi4j) new SydApi4j().connect("172.16.18.59", 8889, null, 1000);
|
||||
|
||||
SydApiBuilder builder = new SydApiBuilder().setIp("192.168.5.88");
|
||||
builder.build();
|
||||
|
||||
try {
|
||||
|
||||
// case1(api);
|
||||
case4(api);
|
||||
case5(api);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
api.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
public static void case1(SydApi api) {
|
||||
String sign = api.attachedSign( Util.hexString2Bytes( inHexData ), dn );
|
||||
System.out.println("签名结果 " + sign );
|
||||
|
||||
|
||||
boolean pass = api.attachedVerify(sign);
|
||||
System.out.println("验签结果 " + pass);
|
||||
}
|
||||
|
||||
|
||||
public static void case2(SydApi api) {
|
||||
System.out.println("begin SYD_Sign_Detach");
|
||||
String sign = api.detachedSign( DATA_ORIGIN, Util.hexString2Bytes( inHexData ), dn );
|
||||
System.out.println("签名结果 " + sign );
|
||||
System.out.println("begin SYD_Sign_Detach");
|
||||
|
||||
System.out.println("begin SYD_Verify_Detach");
|
||||
boolean pass = api.detachedVerify(DATA_ORIGIN, Util.hexString2Bytes( inHexData ), sign);
|
||||
System.out.println("验签结果 " + pass);
|
||||
System.out.println("end SYD_Verify_Detach");
|
||||
}
|
||||
|
||||
|
||||
public static void case3(SydApi api) {
|
||||
System.out.println("begin SYD_NakedSign");
|
||||
byte[] sign = api.SYD_NakedSign( Util.hexString2Bytes(inHexData) , dn);
|
||||
System.out.println("签名结果 " + Util.bytes2HexString(sign) );
|
||||
System.out.println("end SYD_NakedSign");
|
||||
|
||||
System.out.println("begin SYD_NakedVerify");
|
||||
boolean pass = api.SYD_NakedVerify( Util.hexString2Bytes(inHexData), sign, dn);
|
||||
System.out.println("验签结果 " + pass);
|
||||
System.out.println("end SYD_NakedVerify");
|
||||
}
|
||||
|
||||
|
||||
public static void case4(SydApi api) {
|
||||
// 不传 ID
|
||||
String pk = api.SM2GetPublicKeyC("65667", 0 );
|
||||
String sk = api.SM2GetPrivateKeyC("000000000006566700000000001");
|
||||
|
||||
String sign = api.SM2SignC( 0, sk, Util.hexString2Bytes(pk), Util.hexString2Bytes(inHexData) );
|
||||
System.out.println("签名结果 " + sign );
|
||||
|
||||
boolean pass = api.SM2Verify(Util.hexString2Bytes(pk), Util.hexString2Bytes(inHexData), sign );
|
||||
System.out.println("验签结果 " + pass);
|
||||
}
|
||||
|
||||
|
||||
public static void case5(SydApi api) {
|
||||
// 传 ID
|
||||
String uid = "8765432187654321";
|
||||
String pk = api.SM2GetPublicKeyC("65667", 0 );
|
||||
String sk = api.SM2GetPrivateKeyC("000000000006566700000000001");
|
||||
|
||||
System.out.println("begin SYD_Sign_P1");
|
||||
String sign = api.SM2SignC( 0, uid, sk, Util.hexString2Bytes(pk), Util.hexString2Bytes(inHexData) );
|
||||
System.out.println("签名结果 " + Util.bytes2HexString( Util.decodeBase64( sign ) ) );
|
||||
System.out.println("end SYD_Verify_P1");
|
||||
|
||||
System.out.println("begin SYD_Sign_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");
|
||||
}
|
||||
}
|
||||
//package com.sunyard.sydapi.test;
|
||||
//
|
||||
//
|
||||
//import com.sunyard.SydApi;
|
||||
//import com.sunyard.proto.Util;
|
||||
//import racal.sunyard.main.SydApi4j;
|
||||
//import racal.sunyard.main.SydApiBuilder;
|
||||
//
|
||||
//import static com.sunyard.SydApi.*;
|
||||
//
|
||||
//
|
||||
//public class TestSV {
|
||||
//
|
||||
// private static final String inHexData = "67c6697351ff4aec29cdbaabf2fbe346";
|
||||
// private static final String dn = "C=CN,ST=浙江,L=杭州,O=信雅达测试123,OU=安全应用1,OU=安全应用2,CN=@信雅达安全测试";
|
||||
//
|
||||
//
|
||||
// public static void main(String[] args) {
|
||||
// System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
||||
// // 连接加密机
|
||||
// SydApi4j api = (SydApi4j) new SydApi4j().connect("172.16.18.59", 8889, null, 1000);
|
||||
//
|
||||
// SydApiBuilder builder = new SydApiBuilder().setIp("192.168.5.88");
|
||||
// builder.build();
|
||||
//
|
||||
// try {
|
||||
//
|
||||
// // case1(api);
|
||||
// case4(api);
|
||||
// case5(api);
|
||||
//
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// } finally {
|
||||
// api.disconnect();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public static void case1(SydApi api) {
|
||||
// String sign = api.attachedSign( Util.hexString2Bytes( inHexData ), dn );
|
||||
// System.out.println("签名结果 " + sign );
|
||||
//
|
||||
//
|
||||
// boolean pass = api.attachedVerify(sign);
|
||||
// System.out.println("验签结果 " + pass);
|
||||
// }
|
||||
//
|
||||
//
|
||||
// public static void case2(SydApi api) {
|
||||
// System.out.println("begin SYD_Sign_Detach");
|
||||
// String sign = api.detachedSign( DATA_ORIGIN, Util.hexString2Bytes( inHexData ), dn );
|
||||
// System.out.println("签名结果 " + sign );
|
||||
// System.out.println("begin SYD_Sign_Detach");
|
||||
//
|
||||
// System.out.println("begin SYD_Verify_Detach");
|
||||
// boolean pass = api.detachedVerify(DATA_ORIGIN, Util.hexString2Bytes( inHexData ), sign);
|
||||
// System.out.println("验签结果 " + pass);
|
||||
// System.out.println("end SYD_Verify_Detach");
|
||||
// }
|
||||
//
|
||||
//
|
||||
// public static void case3(SydApi api) {
|
||||
// System.out.println("begin SYD_NakedSign");
|
||||
// byte[] sign = api.SYD_NakedSign( Util.hexString2Bytes(inHexData) , dn);
|
||||
// System.out.println("签名结果 " + Util.bytes2HexString(sign) );
|
||||
// System.out.println("end SYD_NakedSign");
|
||||
//
|
||||
// System.out.println("begin SYD_NakedVerify");
|
||||
// boolean pass = api.SYD_NakedVerify( Util.hexString2Bytes(inHexData), sign, dn);
|
||||
// System.out.println("验签结果 " + pass);
|
||||
// System.out.println("end SYD_NakedVerify");
|
||||
// }
|
||||
//
|
||||
//
|
||||
// public static void case4(SydApi api) {
|
||||
// // 不传 ID
|
||||
// String pk = api.SM2GetPublicKeyC("65667", 0 );
|
||||
// String sk = api.SM2GetPrivateKeyC("000000000006566700000000001");
|
||||
//
|
||||
// String sign = api.SM2SignC( 0, sk, Util.hexString2Bytes(pk), Util.hexString2Bytes(inHexData) );
|
||||
// System.out.println("签名结果 " + sign );
|
||||
//
|
||||
// boolean pass = api.SM2Verify(Util.hexString2Bytes(pk), Util.hexString2Bytes(inHexData), sign );
|
||||
// System.out.println("验签结果 " + pass);
|
||||
// }
|
||||
//
|
||||
//
|
||||
// public static void case5(SydApi api) {
|
||||
// // 传 ID
|
||||
// String uid = "8765432187654321";
|
||||
// String pk = api.SM2GetPublicKeyC("65667", 0 );
|
||||
// String sk = api.SM2GetPrivateKeyC("000000000006566700000000001");
|
||||
//
|
||||
// System.out.println("begin SYD_Sign_P1");
|
||||
// String sign = api.SM2SignC( 0, uid, sk, Util.hexString2Bytes(pk), Util.hexString2Bytes(inHexData) );
|
||||
// System.out.println("签名结果 " + Util.bytes2HexString( Util.decodeBase64( sign ) ) );
|
||||
// System.out.println("end SYD_Verify_P1");
|
||||
//
|
||||
// System.out.println("begin SYD_Sign_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");
|
||||
// }
|
||||
//}
|
||||
|
||||
@ -28,8 +28,8 @@ public class SydCmsUtilTest {
|
||||
String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1";
|
||||
String oData = "00001111222233";
|
||||
|
||||
RetWrap retWrap = api.generateDEByDnSoft(dn, oData);
|
||||
String cipherKey = (String) retWrap.get(SydApi.RET_CIPHER_KEY);
|
||||
RetWrap retWrap = api.generateDEByDnSoft(dn, oData.getBytes() );
|
||||
byte[] cipherKey = (byte[]) retWrap.get(SydApi.RET_CIPHER_KEY);
|
||||
System.out.println("数字信封: " + cipherKey);
|
||||
|
||||
RetWrap retWrap1 = api.decryptDEByDN(dn, cipherKey);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user