添加6A、6B关于P7的数字信封
This commit is contained in:
@@ -216,6 +216,32 @@ public interface SydGenApi {
|
||||
*/
|
||||
RetWrap generateDEByDn(byte[] dn, int alg, int model) throws Exception;
|
||||
|
||||
/**
|
||||
* 产生对称密钥,通过公钥加密生成网联格式的数字信封。
|
||||
|
||||
* @param certSerial 证书DN。
|
||||
* @param oData 明文数据
|
||||
* @return RetWrap 包含 CipherKey SessionKey KCV
|
||||
* CipherKey 公钥加密的会话密钥 ,BASE64 编码
|
||||
* SessionKey 本地LMK 加密的会话密钥,HEX 格式, 本地 LMK 加密的会话密钥长度,HEX 格式(不包括第 1 个字符),一般为 33 个 Bytes。默认输入值为缓冲区的大小。
|
||||
* KCV 会话密钥的检验值,HEX 格式,长度为 32H。
|
||||
*/
|
||||
RetWrap generateDEByDn(byte[] certSerial, String oData);
|
||||
|
||||
/**
|
||||
* 产生对称密钥,通过公钥加密生成网联格式的数字信封。
|
||||
|
||||
* @param certSerial 证书DN或SN。
|
||||
* @param alg 算法标识,0:AES,1-3:SM4
|
||||
* @param model 证书模式,0:证书序号,1:证书 DN
|
||||
* @param oData 明文数据
|
||||
* @return RetWrap 包含 CipherKey SessionKey KCV
|
||||
* CipherKey 公钥加密的会话密钥 ,BASE64 编码
|
||||
* SessionKey 本地LMK 加密的会话密钥,HEX 格式, 本地 LMK 加密的会话密钥长度,HEX 格式(不包括第 1 个字符),一般为 33 个 Bytes。默认输入值为缓冲区的大小。
|
||||
* KCV 会话密钥的检验值,HEX 格式,长度为 32H。
|
||||
*/
|
||||
RetWrap generateDE(byte[] certSerial, int alg, int model, String oData);
|
||||
|
||||
/**
|
||||
* 产生对称密钥,通过公钥加密生成网联格式的数字信封。
|
||||
|
||||
@@ -271,6 +297,34 @@ public interface SydGenApi {
|
||||
String pCipherKey
|
||||
);
|
||||
|
||||
/**
|
||||
* 接收用私钥解密数字信封,还原对称密钥。
|
||||
* 默认会传P
|
||||
* @param certSerial 输入。 对方 X509 证书的DN。
|
||||
* @param pCipherKey 输入。 公钥加密的会话密钥,BASE64 编码
|
||||
* @return RetWrap 包含 SessionKey KCV
|
||||
* oData 如果有机构码的话会返回原数据
|
||||
* SessionKey 输出。 本地LMK 加密的会话密钥,格式为:密钥类型|密钥密文,比如:02|密钥密文
|
||||
* KCV 会话密钥的检验值,HEX 格式,长度为 36H。
|
||||
* orgCode 如果有机构码的话会返回机构码
|
||||
*/
|
||||
RetWrap decryptDEByDN(byte[] certSerial, String pCipherKey);
|
||||
|
||||
/**
|
||||
* 接收用私钥解密数字信封,还原对称密钥。
|
||||
*
|
||||
* @param pFlag 输入。 p标识,如果为true,则传入P
|
||||
* @param model 输入。 证书模式,0:证书序号,1:证书 DN
|
||||
* @param certSerial 输入。 对方 X509 证书的序列号或DN。
|
||||
* @param pCipherKey 输入。 公钥加密的会话密钥,BASE64 编码
|
||||
* @return RetWrap 包含 SessionKey KCV
|
||||
* oData 如果有机构码的话会返回原数据
|
||||
* SessionKey 输出。 本地LMK 加密的会话密钥,格式为:密钥类型|密钥密文,比如:02|密钥密文
|
||||
* KCV 会话密钥的检验值,HEX 格式,长度为 36H。
|
||||
* orgCode 如果有机构码的话会返回机构码
|
||||
*/
|
||||
RetWrap decryptDE(boolean pFlag, int model, byte[] certSerial, String pCipherKey);
|
||||
|
||||
/**
|
||||
* 产生要求长度的随即数据串,可用于身份认证
|
||||
*
|
||||
|
||||
@@ -2115,58 +2115,6 @@ public class SydApi4j implements SydApi {
|
||||
return repackEncrypt(packet);
|
||||
}
|
||||
|
||||
// 当证书模式为证书序号时,测试成功
|
||||
// 测试通过
|
||||
@Override
|
||||
public RetWrap generateDEByDn(byte[] dn, int alg, int model) {
|
||||
Proto6A proto = new Proto6A();
|
||||
// 填充数据
|
||||
PacketSN sn = PacketSN.gen();
|
||||
HashMap<String, Object> packet = new HashMap<String, Object>();
|
||||
packet.put("header", sn.getSn().array());
|
||||
HashMap<String, Object> data = new HashMap<String, Object>();
|
||||
data.put("alg$option", alg);
|
||||
data.put("certificateModel$option", model);
|
||||
|
||||
if (model == 0) {
|
||||
data.put("certificateSn", dn);
|
||||
} else {
|
||||
data.put("certificateDn", dn);
|
||||
}
|
||||
data.put("keyType", "X");
|
||||
|
||||
packet.put("data", data);
|
||||
// 渲染发送 耗时长
|
||||
ByteBuffer bb = proto.rend(packet);
|
||||
synchronized (this) {
|
||||
// 响应解析
|
||||
bb = syncRead(syncSend(bb));
|
||||
}
|
||||
//ProtocolParser parser = pset.getProtocolResParse();
|
||||
Packet p = proto.parse(bb);
|
||||
|
||||
Map<String, Object> ret = p.toMap();
|
||||
if (
|
||||
!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())
|
||||
) {
|
||||
throw new SydApiException("包序号不匹配", -4);
|
||||
}
|
||||
ret = (Map<String, Object>) ret.get("data");
|
||||
PacketSection error = (PacketSection) ret.get("error");
|
||||
if (!"00".equals(error.getString())) {
|
||||
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_KCV, ((PacketSection) ret.get("keyCheck")).getString());
|
||||
|
||||
return rw;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RetWrap generateDEbyGS(String certSerial, byte nAlg) {
|
||||
return generateDE(1, certSerial, nAlg, null);
|
||||
@@ -2238,7 +2186,74 @@ public class SydApi4j implements SydApi {
|
||||
return rw;
|
||||
}
|
||||
|
||||
// 当证书模式为证书序号时,测试成功
|
||||
// 测试通过
|
||||
@Override
|
||||
public RetWrap generateDEByDn(byte[] dn, int alg, int model) {
|
||||
return generateDE(dn, alg, model, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public RetWrap generateDEByDn(byte[] certSerial, String oData) {
|
||||
return generateDE(certSerial, 3, 1, oData);
|
||||
}
|
||||
|
||||
@Override
|
||||
public RetWrap generateDE(byte[] certSerial, int alg, int model, String oData) {
|
||||
Proto6A proto = new Proto6A();
|
||||
// 填充数据
|
||||
PacketSN sn = PacketSN.gen();
|
||||
HashMap<String, Object> packet = new HashMap<String, Object>();
|
||||
packet.put("header", sn.getSn().array());
|
||||
HashMap<String, Object> data = new HashMap<String, Object>();
|
||||
data.put("alg$option", alg);
|
||||
data.put("certificateModel$option", model);
|
||||
|
||||
if (model == 0) {
|
||||
data.put("certificateSn", certSerial);
|
||||
} else {
|
||||
data.put("certificateDn", certSerial);
|
||||
}
|
||||
|
||||
if (alg == 3) {
|
||||
data.put("orgData", oData);
|
||||
}
|
||||
|
||||
data.put("keyType", "X");
|
||||
|
||||
packet.put("data", data);
|
||||
// 渲染发送 耗时长
|
||||
ByteBuffer bb = proto.rend(packet);
|
||||
synchronized (this) {
|
||||
// 响应解析
|
||||
bb = syncRead(syncSend(bb));
|
||||
}
|
||||
//ProtocolParser parser = pset.getProtocolResParse();
|
||||
Packet p = proto.parse(bb);
|
||||
|
||||
Map<String, Object> ret = p.toMap();
|
||||
if (
|
||||
!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())
|
||||
) {
|
||||
throw new SydApiException("包序号不匹配", -4);
|
||||
}
|
||||
ret = (Map<String, Object>) ret.get("data");
|
||||
PacketSection error = (PacketSection) ret.get("error");
|
||||
if (!"00".equals(error.getString())) {
|
||||
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_KCV, ((PacketSection) ret.get("keyCheck")).getString());
|
||||
|
||||
return rw;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public RetWrap decryptDE(String certSerial, String pCipherKey) {
|
||||
// 获取协议
|
||||
@@ -2292,6 +2307,78 @@ public class SydApi4j implements SydApi {
|
||||
return rw;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RetWrap decryptDEByDN(byte[] certSerial, String pCipherKey) {
|
||||
return decryptDE(true, 1, certSerial, pCipherKey);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
@Override
|
||||
public RetWrap decryptDE(boolean pFlag, int model, byte[] certSerial, String pCipherKey) {
|
||||
// 获取协议
|
||||
//ProtocolRender render = pset.getProtocolReqRender("6B");
|
||||
// 与6B相同
|
||||
Proto6BXF proto = new Proto6BXF();
|
||||
// 填充数据
|
||||
PacketSN sn = PacketSN.gen();
|
||||
HashMap<String, Object> packet = new HashMap<String, Object>();
|
||||
packet.put("header", sn.getSn().array());
|
||||
HashMap<String, Object> data = new HashMap<String, Object>();
|
||||
if (pFlag) {
|
||||
data.put("pFlag", "P");
|
||||
}
|
||||
data.put("certificateModel$option", model);
|
||||
|
||||
if (model == 0) {
|
||||
data.put("certificateSn", certSerial);
|
||||
} else {
|
||||
data.put("certificateDn", certSerial);
|
||||
}
|
||||
data.put("key", pCipherKey);
|
||||
data.put("keyType", "X");
|
||||
packet.put("data", data);
|
||||
|
||||
// 渲染发送
|
||||
ByteBuffer bb = proto.rend(packet);
|
||||
synchronized (this) {
|
||||
// 响应解析
|
||||
bb = syncRead(syncSend(bb));
|
||||
}
|
||||
|
||||
//ProtocolParser parser = pset.getProtocolResParse();
|
||||
Packet p = proto.parse(bb);
|
||||
|
||||
Map<String, Object> ret = p.toMap();
|
||||
if (
|
||||
!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())
|
||||
) {
|
||||
throw new SydApiException("包序号不匹配", -4);
|
||||
}
|
||||
ret = (Map<String, Object>) ret.get("data");
|
||||
PacketSection error = (PacketSection) ret.get("error");
|
||||
if (!"00".equals(error.getString())) {
|
||||
throw new SydApiException(error.getInfo(), error.autoInt());
|
||||
}
|
||||
RetWrap rw = new RetWrap();
|
||||
|
||||
if (pFlag) {
|
||||
rw.put("oData", ((PacketSection) ret.get("oData")).getString());
|
||||
}
|
||||
|
||||
int orgCodeLen = Util.a2i(((PacketSection) ret.get("orgCodeLen")).getBytes(), 10);
|
||||
if (orgCodeLen != 0) {
|
||||
rw.put("orgCode", ((PacketSection) ret.get("orgCode")).getString());
|
||||
}
|
||||
|
||||
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());
|
||||
|
||||
return rw;
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
@Override
|
||||
public String GenerateRandom(int nRandomSize) {
|
||||
|
||||
Reference in New Issue
Block a user