修改部分xml文档,java类合并其他库中的接口

This commit is contained in:
zhoubin
2021-08-05 10:08:44 +08:00
parent 505f379bcf
commit 1db7955ca7
10 changed files with 911 additions and 66 deletions
-2
View File
@@ -34,11 +34,9 @@
<full len="1" cn="填充类型"> <full len="1" cn="填充类型">
<text>0</text>
</full> </full>
<macLen len="1" cn="MAC 长度标识"> <macLen len="1" cn="MAC 长度标识">
<text>1</text>
</macLen> </macLen>
<key cn="密钥"> <key cn="密钥">
-2
View File
@@ -34,11 +34,9 @@
</type> </type>
<full len="1" cn="填充类型"> <full len="1" cn="填充类型">
<text>0</text>
</full> </full>
<macLen len="1" cn="MAC 长度标识"> <macLen len="1" cn="MAC 长度标识">
<text>1</text>
</macLen> </macLen>
<key cn="密钥"> <key cn="密钥">
+116
View File
@@ -0,0 +1,116 @@
<?xml version="1.0" encoding="UTF-8" ?>
<packet extend="base">
<req>
<command>
<bin>8008</bin>
</command>
<data extend="data">
<dataFlag len="1" cn="数据包标志">
<options>
<option cn="起始包">
<bin>00</bin>
</option>
<option cn="中间包">
<bin>01</bin>
</option>
<option cn="结束包">
<bin>02</bin>
</option>
</options>
</dataFlag>
<sqlite3 cn="Sqlite3 查询条件">
若没有特别查询条件,则值为“ 1 = 1 ”
</sqlite3>
</data>
</req>
<res>
<command>
<bin>8009</bin>
</command>
<data>
<error len="2" cn="错误码" format="X" :catch="catchError('%s')">
<options>
<option cn="无错误">
<hex>00</hex>
</option>
<option cn="公钥不符合编码规则">
<hex>04</hex>
</option>
<option cn="无效的加密标识">
<hex>06</hex>
</option>
<option cn="无效的附加模式标识">
<hex>07</hex>
</option>
<option cn="密钥奇偶校验错误">
<hex>10</hex>
</option>
<option cn="LMK 错误">
LMK 错误;报告给管理员。
<hex>13</hex>
</option>
<option cn="输入数据错">
<hex>15</hex>
</option>
<option cn="DSP 错误">
DSP 错误;报告给管理员。
<hex>47</hex>
</option>
<option cn="私钥错误">
私钥错误;报告给管理员。
<hex>49</hex>
</option>
<option cn="密钥块长度错误">
<hex>76</hex>
</option>
<option cn="输入数据长度错误">
<hex>80</hex>
</option>
</options>
</error>
<dataFlag len="4" cn="数据包标志">
0:起始包
1:中间包
2:结束包
(可能hi因为字节对此,后三字节为垃圾值)
</dataFlag>
<certAllNum len="4" cn="证书总数">
主机字节序
</certAllNum>
<realCertNum len="4" cn="实际证书个数">
主机字节序
</realCertNum>
<dataAttrFlag len="4" cn="数据包属性标志">
0:固定长度包
1:变长包,用@分隔
目前默认为0,
</dataAttrFlag>
<certLenMax len="4" cn="证书最大长度">
目前固定128,若不够用,可用考虑改为256
</certLenMax>
<certDN maxLen="1280" cn="证书DN">
证书DN
</certDN>
</data>
</res>
<script><![CDATA[
function certDNLen() {
return $final.getSection('data').getLen() - 22
}
]]> </script>
</packet>
+96
View File
@@ -0,0 +1,96 @@
<?xml version="1.0" encoding="UTF-8" ?>
<packet extend="base">
<req>
<command>
<bin>8009</bin>
</command>
<data extend="data">
<certLen len="2" :value="certDataLen()" cn="公钥证书数据长度">
</certLen>
<certData cn="公钥证书数据">
</certData>
<organizationIdLen len="2" :value="organizationIdLen()" required="false" cn="机构号长度">
机构号长度
</organizationIdLen>
<organizationId required="false" cn="机构号">
机构号,可选
</organizationId>
</data>
</req>
<res>
<command>
<bin>800A</bin>
</command>
<data>
<error len="2" cn="错误码" format="X" :catch="catchError('%s')">
<options>
<option cn="无错误">
<hex>00</hex>
</option>
<option cn="公钥不符合编码规则">
<hex>04</hex>
</option>
<option cn="无效的加密标识">
<hex>06</hex>
</option>
<option cn="无效的附加模式标识">
<hex>07</hex>
</option>
<option cn="密钥奇偶校验错误">
<hex>10</hex>
</option>
<option cn="LMK 错误">
LMK 错误;报告给管理员。
<hex>13</hex>
</option>
<option cn="输入数据错">
<hex>15</hex>
</option>
<option cn="DSP 错误">
DSP 错误;报告给管理员。
<hex>47</hex>
</option>
<option cn="私钥错误">
私钥错误;报告给管理员。
<hex>49</hex>
</option>
<option cn="密钥块长度错误">
<hex>76</hex>
</option>
<option cn="输入数据长度错误">
<hex>80</hex>
</option>
</options>
</error>
</data>
</res>
<script><![CDATA[
function certDataLen(){
def len = $packet.data.certData.length;
def data = new byte[2];
data[0] = (byte)(len >> 8 & 0xFF)
data[1] = (byte)(len & 0xFF)
// data[0] = 0x04
// data[1] = 0xB4
return data;
}
function organizationIdLen(){
def organizationId = $packet.data.organizationId;
if(null!=organizationId){
def len = organizationId.length;
def data = new byte[2];
data[0] = (byte)(len >> 8 & 0xFF)
data[1] = (byte)(len & 0xFF)
return data;
}
}
]]> </script>
</packet>
+198
View File
@@ -8,4 +8,202 @@ import java.util.HashMap;
*/ */
public class RetWrap extends HashMap<String, Object>{ public class RetWrap extends HashMap<String, Object>{
public static String transMsg(int retCode){
String msg;
switch (retCode){
case -1:
case -2:
case -3:
msg = "网络错误";
break;
case -4:
msg = "返回包的序列号有误";
break;
case -5:
msg = "返回包的命令码有误";
break;
case -6:
msg = "输出缓冲区预留太小";
break;
case -25:
msg = "输入的 HASH 值长度应该为 32 个字节";
break;
case -26:
msg = "用户标识超长(应该<=16) ";
break;
case -27:
msg = "数据类型不合法 ";
break;
case 33:
msg = "检索的数据或密钥不存在 ";
break;
case 4:
msg = "DER 编码格式错误(公钥) ";
break;
case 21:
msg = "输入的数据格式错误 ";
break;
case 19:
msg = "数据解密错(C3) ";
break;
case 2:
msg = "签名验证错误 ";
break;
case 32770:
msg = "签名验证错误(P7) ";
break;
case 36868:
msg = "DN 所指向的实体不存在 ";
break;
case 32884:
msg = "证书或签名数据的格式错 ";
break;
case 36865:
msg = "不支持的命令";
break;
case 36866:
msg = "数据库操作错误";
break;
case 36867:
msg = "数据记录已经存在";
break;
case 36869:
msg = "加密卡错误";
break;
case 36870:
msg = "系统错误(加密数据)";
break;
case 36871:
msg = "CRL文件中包含的废除证书太多(系统限制200个)";
break;
case 36872:
msg = "用户证书不在此废除的证书之列";
break;
case 36874:
msg = "IC卡错误";
break;
case 36875:
msg = "IC卡口令验证错误,请重新连接";
break;
case 36876:
msg = "IC卡编号错误,请重新连接";
break;
case 36877:
msg = "IC卡角色错误,请重新连接";
break;
case 36878:
msg = "IC卡初始化错误,请重新插入";
break;
case 36879:
msg = "IC卡已初始化";
break;
case 36880:
msg = "不是空白卡";
break;
case 36881:
msg = "IC卡未初始化";
break;
case 32769:
msg = "签名出错,请核对索引号";
break;
case 32772:
msg = "证书过期";
break;
case 32773:
msg = "该证书发行者不可信";
break;
case 32774:
msg = "证书格式错";
break;
case 32775:
msg = "机构ID不合法";
break;
case 32776:
msg = "此证书属于根证书,不在用户证书管理范围";
break;
case 32777:
msg = "产生证书请求出错";
break;
case 32784:
msg = "该证书已被废弃";
break;
case 28673:
msg = "随机数的时效已过";
break;
case 28674:
msg = "登录超时";
break;
case 28675:
msg = "登录口令错";
break;
case 28677:
msg = "操作功能非法使用";
break;
case 28678:
msg = "系统已签退,请重新登录";
break;
case 28679:
msg = "操作权限限制";
break;
case 28680:
msg = "随机数据长度超过系统的要求";
break;
case 28681:
msg = "此操作员不存在";
break;
case 28688:
msg = "此操作员已经存在";
break;
case 28689:
msg = "该IP是本地IP,不能进行分发";
break;
case 24581:
msg = "文件在设备后端读写失败";
break;
case 24582:
msg = "ERROR_HANDLE_NULL";
break;
case 24585:
msg = "升级文件格式有误,请核对后再操作";
break;
case 24592:
msg = "命令执行过程中出现系统错误";
break;
case 24593:
msg = "本功能升级已经结束,不用重复操作";
break;
case 24594:
msg = "数据不合法或格式不正确";
break;
case 24595:
msg = "中心标识未设,请先设置中心标识";
break;
case 24596:
msg = "加密密钥不存在";
break;
case 24597:
msg = "密钥应用超过10个(密钥必须索引0-9)";
break;
case 24598:
msg = "行标识没有设置";
break;
case 24577:
msg = "查询类型错误";
break;
case 24578:
msg = "查询格式错误";
break;
case 24579:
msg = "数据不合法";
break;
case 24580:
msg = "系统的口令错误";
break;
default:
msg = "未定义的错误";
}
return msg;
}
} }
+8
View File
@@ -20,6 +20,14 @@ public interface SydApi extends SydRawApi, SydCertApi, SydCryptionApi, SydGenApi
int NALG_SM4 = 0x01; int NALG_SM4 = 0x01;
int NALG_AES = 0x02; int NALG_AES = 0x02;
//清算总行密押MAC填充模式
byte MIYA_FULL_WEAK = 0x30;
byte MIYA_FULL_FORCE = 0x31;
//清算总行密押MAC长度标识
byte MIYA_MAC_LEN_4 = 0x31;
byte MIYA_MAC_LEN_8 = 0x32;
byte MIYA_MAC_LEN_16 = 0x33;
String RET_CIPHER_KEY = "CipherKey"; String RET_CIPHER_KEY = "CipherKey";
String RET_SESSION_KEY = "SessionKey"; String RET_SESSION_KEY = "SessionKey";
String RET_PUBLIC_KEY = "PublicKey"; String RET_PUBLIC_KEY = "PublicKey";
@@ -1,8 +1,11 @@
package com.sunyard.inf; package com.sunyard.inf;
import com.sunyard.RetWrap;
import com.sunyard.cert.X509; import com.sunyard.cert.X509;
import java.io.UnsupportedEncodingException;
/** /**
* 证书操作相关接口 * 证书操作相关接口
*/ */
@@ -29,6 +32,11 @@ public interface SydCertApi {
byte[] dn byte[] dn
); );
String RSAGetPublicKeyC(
String userId,
int nKeyTypes
);
/** /**
* 通过使用者 ID 获得使用者的 SM2 公钥(DER 编码)。 * 通过使用者 ID 获得使用者的 SM2 公钥(DER 编码)。
* @param userId 使用者的 ID,最大长度为 16 位数字,右对齐。 * @param userId 使用者的 ID,最大长度为 16 位数字,右对齐。
@@ -79,4 +87,19 @@ public interface SydCertApi {
String sign String sign
); );
/**
* 查询所有证书
* @param dataFlag 数据包标志 0:起始包 1:中间包 2:结束包
* @return
*/
RetWrap getAllCert(int dataFlag) throws UnsupportedEncodingException;
/**
* 上传指定证书到加密设备
* @param certData 证书的字节数组
* @param organizationId 机构号的字节数组
* @return
*/
RetWrap importCert(byte[] certData, byte[] organizationId);
} }
@@ -1,5 +1,6 @@
package com.sunyard.inf; package com.sunyard.inf;
import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
import com.sunyard.RetWrap; import com.sunyard.RetWrap;
/** /**
@@ -94,6 +95,77 @@ interface SydASYMApi {
String userId, String userId,
String pEnData String pEnData
); );
/**
* 使用外部私钥解密
* @param sk
* @param enData
* @return
*/
byte[] SM2Decrypt(byte[] sk, String enData);
/**
* 用指定的 RSA 私钥对指定的密文数据进行加密。
*
* @param publicKey 私钥串,DER 编码,HEX 格式。
* @param pOrgData 加密的数据。
* @return 加密的数据 base64 编码。
*/
String RSAEncrypt(
String publicKey,
byte[] pOrgData
);
/**
* 通过使用者 ID 所对应的私钥对指定的密文数据进行解密。
*
* @param userId 使用者的 ID,最大长度为 16 位数字,右对齐。
* @param pEnData 加密的数据,base64 编码。
* @return 明文数据。
*/
byte[] RSADecryptC(
String userId,
String pEnData
) throws Base64DecodingException;
/**
* 使用外部私钥解密
* @param sk
* @param enData
* @return
*/
public byte[] RSADecrypt(String sk, String enData) throws Base64DecodingException;
/**
* 非对称公钥加密
* @param alg
* @param pk
* @param data
* @return
*/
public byte[] encryptByPk(int alg, String pk, byte[] data);
/**
* 非对称私钥解密
* @param alg
* @param sk 私钥值
* @param data
* @return
*/
public byte[] decryptBySk(int alg, String sk, byte[] data);
/**
* 非对称私钥解密
* @param alg
* @param user
* @param data
* @return
*/
public byte[] decryptBySkC(int alg, String user, byte[] data);
} }
+27 -2
View File
@@ -10,7 +10,19 @@ public interface SydPledge {
* @param Data 原始数据 * @param Data 原始数据
* @return MAC 长度为 8 的 HEX字符串 * @return MAC 长度为 8 的 HEX字符串
*/ */
public String GenerateMIYA(String AreaCode, int Version, int Alg, byte[] Data); String GenerateMIYA(String AreaCode, int Version, int Alg, byte[] Data);
/**
*
* @param AreaCode 地区代码 6 位
* @param Version 版本号
* @param Alg SydApi.NALG_3DES/SydApi.NALG_SM4
* @param Data 原始数据
* @param fullFlag 填充类型
* @param macLenFlag MAC 长度标识
* @return
*/
String GenerateMIYA(String AreaCode, int Version, int Alg, byte[] Data, byte fullFlag, byte macLenFlag);
/** /**
* *
@@ -21,6 +33,19 @@ public interface SydPledge {
* @param Data 原始数据 * @param Data 原始数据
* @return true=核押通过 false=核押不通过 * @return true=核押通过 false=核押不通过
*/ */
public boolean VerifyMIYA(String AreaCode, int Version, int Alg, String MAC, byte[] Data); boolean VerifyMIYA(String AreaCode, int Version, int Alg, String MAC, byte[] Data);
/**
*
* @param AreaCode 地区代码 6 位
* @param Version 版本号
* @param Alg SydApi.NALG_3DES/SydApi.NALG_SM4
* @param mac 长度为 8 的 HEX字符串
* @param Data 原始数据
* @param fullFlag 填充类型
* @param macLenFlag MAC 长度标识
* @return
*/
boolean VerifyMIYA(String AreaCode, int Version, int Alg, String mac, byte[] Data, byte fullFlag, byte macLenFlag);
} }
+326 -15
View File
@@ -535,6 +535,7 @@ public class SydApi4j implements SydApi {
} }
@Override
public RetWrap genComposeKey(String keyType, char keySolution, String keyClearPattern) { public RetWrap genComposeKey(String keyType, char keySolution, String keyClearPattern) {
ProtoAA proto = new ProtoAA(); ProtoAA proto = new ProtoAA();
@@ -825,6 +826,127 @@ public class SydApi4j implements SydApi {
return GetX509CertInfo(new String(pCertInfo)); return GetX509CertInfo(new String(pCertInfo));
} }
@Override
public RetWrap getAllCert(int dataFlag) throws UnsupportedEncodingException {
Proto8008 proto = new Proto8008();
// 填充数据
HashMap<String, Object> packet = new HashMap<String, Object>();
HashMap<String, Object> data = new HashMap<String, Object>();
packet.put("data", data);
PacketSN sn = PacketSN.gen();
packet.put("header", sn.getSn().array());
data.put("dataFlag$option", dataFlag);
String sqlite3 = "1 = 1 ";
data.put("sqlite3", sqlite3);
// 渲染发送
ByteBuffer bb = proto.rend(packet);
synchronized (this) {
// 响应解析
bb = syncRead(syncSend(bb));
}
Packet p = proto.parse(bb, packet);
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");
byte[] be = error.getBytes();
if ( ( 0 != (be[0] ^ be[1]))) {
throw new SydApiException(error.getInfo(), error.autoInt());
}
RetWrap r = new RetWrap();
PacketSection resDataFlag = (PacketSection) ret.get("dataFlag");
r.put("dataFlag", (int) resDataFlag.getBytes()[0]);
PacketSection certAllNum = (PacketSection) ret.get("certAllNum");
r.put("certAllNum", (int) certAllNum.getBytes()[0]);
PacketSection realCertNum = (PacketSection) ret.get("realCertNum");
r.put("realCertNum", (int) realCertNum.getBytes()[0]);
PacketSection dataAttrFlag = (PacketSection) ret.get("dataAttrFlag");
r.put("dataAttrFlag", (int) dataAttrFlag.getBytes()[0]);
r.put("certLenMax", Integer.parseInt(Util.bytes2HexString(Util.toArray(bb)).substring(60, 62), 16));
List<String> certs = new ArrayList<String>();
while( bb.limit() - bb.position() >= 128 ) {
byte[] cert = new byte[ 128 ];
bb.get( cert );
certs.add( new String(cert, "GBK") );
}
r.put("certDNs", certs.toArray(new String[0]));
return r;
}
@Override
public RetWrap importCert(byte[] certData, byte[] organizationId) {
Proto8009 proto = new Proto8009();
// 填充数据
HashMap<String, Object> packet = new HashMap<String, Object>();
HashMap<String, Object> data = new HashMap<String, Object>();
packet.put("data", data);
PacketSN sn = PacketSN.gen();
packet.put("header", sn.getSn().array());
data.put("certData", certData );
if(null != organizationId && organizationId.length > 0){
data.put("organizationId", organizationId );
}
// int length = certData.length;
// System.out.println("dataLength: " + length);
// 渲染发送
ByteBuffer bb = proto.rend(packet);
// String s = Util.bytes2HexString(Util.toArray(bb));
// System.out.println(s);
synchronized (this) {
// 响应解析
bb = syncRead(syncSend(bb));
}
Packet p = proto.parse(bb, packet);
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");
byte[] be = error.getBytes();
// String s1 = Util.bytes2HexString(be);
// System.out.println(s1);
RetWrap r = new RetWrap();
r.put("retCode", true);
if ( ( 0 != (be[0] ^ be[1]))) {
r.put("retCode", false);
r.put("msg", error.getInfo() == null || "".equals(error.getInfo()) ? RetWrap.transMsg(error.autoInt()) : error.getInfo());
}
return r;
}
public SydApi4j() { public SydApi4j() {
// if (null == pset) { // if (null == pset) {
@@ -1247,7 +1369,6 @@ public class SydApi4j implements SydApi {
byte[] msgAll = (byte[]) (data.get("msg")); byte[] msgAll = (byte[]) (data.get("msg"));
// 大包分包 // 大包分包
int block = 0; int block = 0;
if (data.containsKey("block")) { if (data.containsKey("block")) {
@@ -1270,7 +1391,6 @@ public class SydApi4j implements SydApi {
} }
/** /**
* 初始值,仅当密钥加密模式为2,3,4时有此域。 * 初始值,仅当密钥加密模式为2,3,4时有此域。
* 当密钥模式为 0,1时,该域为16H * 当密钥模式为 0,1时,该域为16H
@@ -1441,7 +1561,6 @@ public class SydApi4j implements SydApi {
} }
@Override @Override
public boolean SYMDecryptDataWithFile(int nAlg, String seesionKey, String inputPath, String outputPath) { public boolean SYMDecryptDataWithFile(int nAlg, String seesionKey, String inputPath, String outputPath) {
@@ -1624,6 +1743,7 @@ public class SydApi4j implements SydApi {
/** /**
* E0 指令复合接口 * E0 指令复合接口
*
* @param isEn * @param isEn
* @param nAlg * @param nAlg
* @param seesionKey * @param seesionKey
@@ -1697,7 +1817,6 @@ public class SydApi4j implements SydApi {
} }
@Override @Override
public byte[] SYMEncryptData(int nAlg, String seesionKey, int fullMode, byte[] pInData) { public byte[] SYMEncryptData(int nAlg, String seesionKey, int fullMode, byte[] pInData) {
@@ -1737,6 +1856,7 @@ public class SydApi4j implements SydApi {
} }
@Override
public byte[] SYMEncryptData_nofill(int nAlg, String seesionKey, byte[] pInData) { public byte[] SYMEncryptData_nofill(int nAlg, String seesionKey, byte[] pInData) {
if (2 == nAlg && 'L' != seesionKey.charAt(0)) { if (2 == nAlg && 'L' != seesionKey.charAt(0)) {
@@ -2256,7 +2376,8 @@ public class SydApi4j implements SydApi {
} }
public boolean RSAVerify(int HashMark, int nOrgDataType, byte[] publickey, byte[] orgData, byte[] base64Sign) { @Override
public boolean RSAVerify(int HashMark, int nOrgDataType, byte[] publicKey, byte[] orgData, byte[] base64Sign) {
Proto7C proto = new Proto7C(); Proto7C proto = new Proto7C();
byte[] sign = base64Sign; byte[] sign = base64Sign;
@@ -2269,7 +2390,7 @@ public class SydApi4j implements SydApi {
data.put("hashMark$option", HashMark); data.put("hashMark$option", HashMark);
data.put("signature", sign); data.put("signature", sign);
data.put("hashData", orgData); data.put("hashData", orgData);
data.put("publicKey", publickey); data.put("publicKey", publicKey);
ByteBuffer bb = proto.rend(packet); ByteBuffer bb = proto.rend(packet);
@@ -2294,6 +2415,7 @@ public class SydApi4j implements SydApi {
} }
} }
@Override
public byte[] RSASignC(String privateKeyIndex, int HashMark, int nOrgDataType, byte[] orgData) { public byte[] RSASignC(String privateKeyIndex, int HashMark, int nOrgDataType, byte[] orgData) {
Proto7B proto = new Proto7B(); Proto7B proto = new Proto7B();
HashMap<String, Object> packet = new HashMap(); HashMap<String, Object> packet = new HashMap();
@@ -2308,7 +2430,6 @@ public class SydApi4j implements SydApi {
data.put("privateKeyTag", SYMUtil.genPrivateKeyMark(privateKeyIndex, "000", "00000001")); data.put("privateKeyTag", SYMUtil.genPrivateKeyMark(privateKeyIndex, "000", "00000001"));
ByteBuffer bb = proto.rend(packet); ByteBuffer bb = proto.rend(packet);
synchronized (this) { synchronized (this) {
@@ -2380,6 +2501,46 @@ public class SydApi4j implements SydApi {
} }
} }
@Override
public String RSAGetPublicKeyC(String userId, int nKeyType) {
//ProtocolRender render = pset.getProtocolReqRender("QQ");
ProtoQQ proto = new ProtoQQ();
HashMap<String, Object> packet = new HashMap<String, Object>();
HashMap<String, Object> data = new HashMap<String, Object>();
packet.put("data", data);
PacketSN sn = PacketSN.gen();
packet.put("header", sn.getSn().array());
data.put("privateKeyTag", SYMUtil.genPrivateKeyMark(userId, 0 == nKeyType ? "000" : "001", "00000001"));
// 渲染发送
ByteBuffer bb = proto.rend(packet);
//System.out.println(Util.bytes2HexString(Util.toArray(bb)));
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());
}
int len = Integer.parseInt(((PacketSection) ret.get("publicLen")).getString());
if (len == 128) {
return "03420004" + ((PacketSection) ret.get("publicKey")).getString();
}
System.out.println("len=" + len);
return Util.bytes2HexString(((PacketSection) ret.get("publicKey")).getBytes());
}
@Override @Override
public String SM2GetPublicKeyC(String userId, int nKeyType) { public String SM2GetPublicKeyC(String userId, int nKeyType) {
@@ -2688,7 +2849,6 @@ public class SydApi4j implements SydApi {
} }
public boolean SM2Verify( public boolean SM2Verify(
int nOrgDataType, int nOrgDataType,
byte[] publicKey, byte[] publicKey,
@@ -2732,7 +2892,6 @@ public class SydApi4j implements SydApi {
} }
@Override @Override
public String SM2Sign( public String SM2Sign(
int nOrgDataType, int nOrgDataType,
@@ -2781,8 +2940,6 @@ public class SydApi4j implements SydApi {
} }
@Override @Override
public boolean SM2VerifyCustomized(String certNo, byte[] orgData, String base64Sign) { public boolean SM2VerifyCustomized(String certNo, byte[] orgData, String base64Sign) {
@@ -2950,6 +3107,7 @@ public class SydApi4j implements SydApi {
* @param pk HEX 格式的公钥 * @param pk HEX 格式的公钥
* @return * @return
*/ */
@Override
public byte[] encryptByPk(int alg, String pk, byte[] data) { public byte[] encryptByPk(int alg, String pk, byte[] data) {
byte[] algBin = 0 == alg ? DTO77.Req.Data.AlgorithmKeyOptions.OPVRSA.getBin() : DTO77.Req.Data.AlgorithmKeyOptions.OPVSM2.getBin(); byte[] algBin = 0 == alg ? DTO77.Req.Data.AlgorithmKeyOptions.OPVRSA.getBin() : DTO77.Req.Data.AlgorithmKeyOptions.OPVSM2.getBin();
@@ -2990,6 +3148,38 @@ public class SydApi4j implements SydApi {
return decryptBySk(alg, sk, SYMUtil.base64EnData2DerEnData(data)); return decryptBySk(alg, sk, SYMUtil.base64EnData2DerEnData(data));
} }
@Override
public byte[] decryptBySkC(int alg, String userId, byte[] data) {
byte[] algBin = 0 == alg ? DTO78.Req.Data.AlgorithmKeyOptions.OPVRSA.getBin() : DTO78.Req.Data.AlgorithmKeyOptions.OPVSM2.getBin();
Proto78 proto = new Proto78();
DTO78.Req req = new DTO78.Req()
.setHeader( PacketSN.gen().getSn().array() )
.buildData()
.setAlgorithmKey( algBin )
.setFillKey( DTO78.Req.Data.FillKeyOptions.OPVPKCS1v15填充方式.getBin() )
.setPrivateKeyTag( SYMUtil.genPrivateKeyMark(userId, SYMUtil.KT_ENCRYPT, "00000001").getBytes() )
.setInfoData( data )
.closeBuilding();
ByteBuffer bb = proto.rend( req.toSoup() );
synchronized (this) {
// 响应解析
bb = syncRead(syncSend(bb));
}
Packet p = proto.parse(bb);
DTO78.Res res = new DTO78.Res();
res = res.fullFromSoup( proto.parse(bb).resSoup() );
if ( "3030".equals(res.getData().getErrorAsHex()) ){
return res.getData().getDataAsBin();
} else {
throw SydApiException.fromBin(res.getData().getErrorAsBin(), res.getData().getErrorCn());
}
}
@Override
public byte[] decryptBySk(int alg, String sk, byte[] data) { public byte[] decryptBySk(int alg, String sk, byte[] data) {
byte[] algBin = 0 == alg ? DTO78.Req.Data.AlgorithmKeyOptions.OPVRSA.getBin() : DTO78.Req.Data.AlgorithmKeyOptions.OPVSM2.getBin(); byte[] algBin = 0 == alg ? DTO78.Req.Data.AlgorithmKeyOptions.OPVRSA.getBin() : DTO78.Req.Data.AlgorithmKeyOptions.OPVSM2.getBin();
@@ -3023,6 +3213,56 @@ public class SydApi4j implements SydApi {
} }
} }
/**
* 用指定的 RSA 私钥对指定的密文数据进行加密。
*
* @param publicKey 私钥串,DER 编码,HEX 格式。
* @param pOrgData 加密的数据。
* @return 加密的数据 base64 编码。
*/
@Override
public String RSAEncrypt(
String publicKey,
byte[] pOrgData
) {
return Util.encodeBase64(encryptByPk(0, publicKey, pOrgData));
}
/**
* 通过使用者 ID 所对应的私钥对指定的密文数据进行解密。
*
* @param userId 使用者的 ID,最大长度为 16 位数字,右对齐。
* @param pEnData 加密的数据,base64 编码。
* @return 明文数据。
*/
@Override
public byte[] RSADecryptC(
String userId,
String pEnData
) {
return decryptBySk(0, userId, Util.decodeBase64(pEnData));
}
/**
* 使用外部私钥解密
*
* @param sk HEX 私钥值
* @param enData base64 值
* @return
*/
@Override
public byte[] RSADecrypt(String sk, String enData) {
return decryptBySk(0, sk, Util.decodeBase64(enData));
}
@Override @Override
public String SM2Encrypt(byte[] publicKey, byte[] pOrgData) { public String SM2Encrypt(byte[] publicKey, byte[] pOrgData) {
Proto77 proto = new Proto77(); Proto77 proto = new Proto77();
@@ -3059,6 +3299,48 @@ public class SydApi4j implements SydApi {
return SYMUtil.derEnData2Base64EnData(((PacketSection) ret.get("cipherData")).getBytes()); return SYMUtil.derEnData2Base64EnData(((PacketSection) ret.get("cipherData")).getBytes());
} }
@Override
public byte[] SM2Decrypt(byte[] sk, String enData) {
//ProtocolRender render = pset.getProtocolReqRender("78");
Proto78 proto = new Proto78();
byte[] pOrgData = SYMUtil.base64EnData2DerEnData(enData);
// 填充数据
HashMap<String, Object> packet = new HashMap<String, Object>();
HashMap<String, Object> data = new HashMap<String, Object>();
packet.put("data", data);
PacketSN sn = PacketSN.gen();
packet.put("header", sn.getSn().array());
data.put("AlgorithmKey$option", 1);
data.put("infoData", pOrgData);
data.put("privateKeyTag", "999999999999999999999999999");
data.put("privateKey", sk);
// 渲染发送
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());
}
return ((PacketSection) ret.get("data")).getBytes();
}
@Override @Override
public byte[] SM2DecryptC(String userId, String enData) { public byte[] SM2DecryptC(String userId, String enData) {
//ProtocolRender render = pset.getProtocolReqRender("78"); //ProtocolRender render = pset.getProtocolReqRender("78");
@@ -3380,8 +3662,8 @@ public class SydApi4j implements SydApi {
} }
RetWrap rw = new RetWrap(); RetWrap rw = new RetWrap();
System.out.println("lmkHead" + ((PacketSection) ret.get("lmkHead")).getString() + // System.out.println("lmkHead" + ((PacketSection) ret.get("lmkHead")).getString() +
"\nlmk" + ((PacketSection) ret.get("lmk")).getString()); // "\nlmk" + ((PacketSection) ret.get("lmk")).getString());
rw.put(SydApi.RET_SESSION_KEY, ((PacketSection) ret.get("lmkHead")).getString() + rw.put(SydApi.RET_SESSION_KEY, ((PacketSection) ret.get("lmkHead")).getString() +
@@ -4247,6 +4529,12 @@ public class SydApi4j implements SydApi {
@Override @Override
public String GenerateMIYA(String AreaCode, int Version, int Alg, byte[] Data) { public String GenerateMIYA(String AreaCode, int Version, int Alg, byte[] Data) {
return GenerateMIYA(AreaCode, Version, Alg, Data, MIYA_FULL_WEAK, MIYA_MAC_LEN_4);
}
@Override
public String GenerateMIYA(String AreaCode, int Version, int Alg, byte[] Data, byte fullFlag, byte macLenFlag) {
if (Version < 0 || Version > 999) { if (Version < 0 || Version > 999) {
throw new SydApiException("Version 范围不对", 21); throw new SydApiException("Version 范围不对", 21);
} }
@@ -4287,6 +4575,8 @@ public class SydApi4j implements SydApi {
data.put("MACData", Util.bytes2HexString(Data).getBytes()); data.put("MACData", Util.bytes2HexString(Data).getBytes());
data.put("key", key); data.put("key", key);
data.put("full", fullFlag);
data.put("macLen", macLenFlag);
// 渲染发送 // 渲染发送
ByteBuffer bb = proto.rend(packet); ByteBuffer bb = proto.rend(packet);
@@ -4317,6 +4607,11 @@ public class SydApi4j implements SydApi {
@Override @Override
public boolean VerifyMIYA(String AreaCode, int Version, int Alg, String mac, byte[] Data) { public boolean VerifyMIYA(String AreaCode, int Version, int Alg, String mac, byte[] Data) {
return VerifyMIYA(AreaCode, Version, Alg, mac, Data, MIYA_FULL_WEAK, MIYA_MAC_LEN_4);
}
@Override
public boolean VerifyMIYA(String AreaCode, int Version, int Alg, String mac, byte[] Data, byte fullFlag, byte macLenFlag) {
if (Version < 0 || Version > 999) { if (Version < 0 || Version > 999) {
throw new SydApiException("Version 范围不对", 21); throw new SydApiException("Version 范围不对", 21);
@@ -4359,6 +4654,8 @@ public class SydApi4j implements SydApi {
data.put("MAC", mac); data.put("MAC", mac);
data.put("MACData", Util.bytes2HexString(Data).getBytes()); data.put("MACData", Util.bytes2HexString(Data).getBytes());
data.put("key", key); data.put("key", key);
data.put("full", fullFlag);
data.put("macLen", macLenFlag);
// 渲染发送 // 渲染发送
ByteBuffer bb = proto.rend(packet); ByteBuffer bb = proto.rend(packet);
@@ -5645,7 +5942,19 @@ public class SydApi4j implements SydApi {
return true; return true;
} }
//9.2 数据转加密指令(Y0)(=) -> pass (注:密文数据那项不一定需要传加密出来的数据) /**
* 数据转加密指令(Y0)(=) -> pass (注:密文数据那项不一定需要传加密出来的数据)
* @param srcType 源密钥类型
* @param sca 源算法SCA
* @param desKeyType 目的秘钥类型
* @param dca 目的算法SCA
* @param roundModel 加密模式
* @param sDEK 源秘钥DEK
* @param dDEK 目的秘钥DEK
* @param infoData 密文数据
* @param vector 向量
* @return byte[] 目的密钥加密的密文数据
*/
public byte[] switchEncryY(String srcType, int sca, String desKeyType, int dca, int roundModel, String sDEK, String dDEK, byte[] infoData, String vector) { public byte[] switchEncryY(String srcType, int sca, String desKeyType, int dca, int roundModel, String sDEK, String dDEK, byte[] infoData, String vector) {
ProtoY0 proto = new ProtoY0(); ProtoY0 proto = new ProtoY0();
HashMap<String, Object> packet = new HashMap(); HashMap<String, Object> packet = new HashMap();
@@ -7255,6 +7564,8 @@ public class SydApi4j implements SydApi {
bytes[i] = bb.get(); bytes[i] = bb.get();
} }
return bytes; return bytes;
}; }
;
} }