更新中移S9接口

This commit is contained in:
zhoubin
2021-08-13 16:21:42 +08:00
parent f1ba00d880
commit d55a195656
5 changed files with 327 additions and 23 deletions
+1 -1
View File
@@ -44,7 +44,7 @@ public interface SydGenApi {
/**
* 生成一个对称密钥(*) -> pass (对比之前添加了可产生ZMK加密下的密钥)
* @param mode 是否产生ZMK加密下的密钥
* @param mode 是否产生ZMK加密下的密钥,0-产生密钥。1-产生密钥并在ZMK下加密
* @param keyType 密钥类型
* @param solution 密钥方案(LMK) 密钥长度/LMK下加密密钥的方案。
* @param zmk zmk
@@ -9258,6 +9258,119 @@ public class SydApi4j implements SydApi {
*/
@Override
public RetWrap exportKeyAndCalc(String protectedKeyIndex, int algFlag, byte[] iv, int protectedKeyDivNum, List<byte[]> protectedKeyDivData, int[] protectedKeyDivAlgFlag, List<byte[]> protectedKeyDivIv, int exportKeyNum, List<? extends Struct> structList) {
// 填充数据
PacketSN sn = PacketSN.gen();
ByteBuffer bb = ByteBuffer.allocate(2048);
byte[] ret = new byte[2];
bb.put(ret);
bb.put(sn.getSn().array());
bb.put("S9".getBytes());
// 保护密钥索引
String gId = "00000000" + protectedKeyIndex;
gId = gId.substring(gId.length() - 8);
bb.put(("0000000000000000000" + gId).getBytes());
// 保护密钥算法
int algorithmKey = MyUtil.transAlg(algFlag);
bb.put(String.format("%02d", algorithmKey).getBytes());
if (6==algorithmKey || 8==algorithmKey || 2==algorithmKey || 4==algorithmKey || 10==algorithmKey) {
// IV 数据域
bb.put(iv);
}
// 保护密钥分散级数
bb.put(( String.valueOf( protectedKeyDivNum ) ).getBytes());
if (protectedKeyDivNum != 0) {
// 分散因子
for (int i = 0; i < protectedKeyDivData.size(); i++) {
bb.put(protectedKeyDivData.get(i));
}
// 分散算法标识
for (int i = 0; i < protectedKeyDivAlgFlag.length; i++) {
bb.put(String.format("%02d", MyUtil.transAlg(protectedKeyDivAlgFlag[i])).getBytes());
}
// 分散算法的初始向量
for (int i = 0; i < protectedKeyDivIv.size(); i++) {
bb.put(protectedKeyDivIv.get(i));
}
}
// 导出的密钥索引个数
bb.put(String.format("%02d", exportKeyNum).getBytes());
//导出的密钥结构体
byte[] b = new byte[ 144 * exportKeyNum ];
int destPos=0;
for(Struct struct:structList){
System.arraycopy(struct.getByte(),0,b,destPos,struct.getByte().length);
destPos+=struct.getByte().length;
}
bb.put(b);
int len = bb.position() - 2;
bb.put(0, (byte) (0xFF & (len / 256)));
bb.put(1, (byte) (0xFF & (len % 256)));
synchronized (this) {
bb = syncRead(syncSend(bb));
}
bb.flip();
// 00240000000000000000533A
// 3030
// 5D0D63E2C673934E5677E902C155F54F3FBE8E53D709F32E
// 解析头部
bb.get(new byte[2], 0, 2);
// 解析header
byte[] header = new byte[8];
bb.get(header, 0, 8);
// 解析协议
bb.get(new byte[2], 0, 2);
if (!Arrays.equals(sn.getSn().array(), header)) {
throw new SydApiException("包序号不匹配", -4);
}
// 检测错误
byte[] error = new byte[2];
bb.get(error, 0, 2);
int e = Util.a2i(error, 16);
if (0 != e) {
throw new SydApiException(0x80 == e ? "数据长度错" : "输入长度错", e);
}
// 依次连接所有密钥密文输出
byte[] linkKeyOut = new byte[exportKeyNum * 16];
bb.get(linkKeyOut, 0, exportKeyNum * 16);
// 依次连接所有密钥校验值输出
byte[] linkKeyCheckOut = new byte[exportKeyNum * 8];
bb.get(linkKeyCheckOut, 0, exportKeyNum * 8);
RetWrap retWrap = new RetWrap();
retWrap.put("linkKeyOut", linkKeyOut);
retWrap.put("linkKeyCheckOut", linkKeyCheckOut);
return retWrap;
}
private RetWrap _exportKeyAndCalc(String protectedKeyIndex, int algFlag, byte[] iv, int protectedKeyDivNum, List<byte[]> protectedKeyDivData, int[] protectedKeyDivAlgFlag, List<byte[]> protectedKeyDivIv, int exportKeyNum, List<? extends Struct> structList) {
ProtoS9 proto = new ProtoS9();
HashMap<String, Object> packet = new HashMap();
HashMap<String, Object> data = new HashMap();