添加批量加密解密接口

This commit is contained in:
cheney 2022-07-12 20:12:43 +08:00
parent acc2f14b44
commit dd63df5abb
4 changed files with 148 additions and 2 deletions

View File

@ -4,7 +4,7 @@
<groupId>com.sunyard</groupId> <groupId>com.sunyard</groupId>
<artifactId>sydapi4j</artifactId> <artifactId>sydapi4j</artifactId>
<version>1.0.20220402</version> <version>1.0.20220712</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>sydapi4j</name> <name>sydapi4j</name>

View File

@ -3,6 +3,7 @@ package com.sunyard.inf;
import com.sunyard.RetWrap; import com.sunyard.RetWrap;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.util.List;
/** /**
* 对称加解密相关接口 * 对称加解密相关接口
@ -265,6 +266,9 @@ interface SydSYMApi {
*/ */
byte[] SYMEncryptData(int nAlg, String seesionKey, int fullmode, byte[] pINdata); byte[] SYMEncryptData(int nAlg, String seesionKey, int fullmode, byte[] pINdata);
List<byte[]> SYMEncryptDecryptBatchData(int nAlg, String[] seesionKey, int fullmode, List<byte[]> pINdata);
/** /**
* 用输入的会话密钥对指定报文数据进行加密处理 * 用输入的会话密钥对指定报文数据进行加密处理
* *

View File

@ -1165,6 +1165,7 @@ public class SydApi4j implements SydApi {
try { try {
socket.getOutputStream().write(buff); socket.getOutputStream().write(buff);
socket.getOutputStream().flush();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -1969,7 +1970,119 @@ public class SydApi4j implements SydApi {
data.put("msg", pInData); data.put("msg", pInData);
packet.put("data", data); packet.put("data", data);
return repackEncrypt(packet); return repackEncrypt( packet );
}
public List<byte[]> SYMEncryptDecryptBatchData(int nAlg, String[] seesionKey, int fullmode, List<byte[]> pINdata) {
int batchSize = pINdata.size();
if ( batchSize < 1 || batchSize > 10 ) {
throw new IllegalArgumentException("批量数量错误");
}
if ( seesionKey.length != pINdata.size() ) {
throw new IllegalArgumentException("密钥和数据数量不匹配");
}
if ( 0x0 != (byte) (nAlg & 0x1) ){
// 解密
for ( byte[]d : pINdata ) {
if ( d.length > 1040 ) {
throw new IllegalArgumentException("批量单数数据超长 1040");
}
}
} else {
// 加密
for ( byte[]d : pINdata ) {
if ( d.length > 1024 ) {
throw new IllegalArgumentException("批量单数数据超长 1024");
}
}
}
ByteBuffer bb = ByteBuffer.allocate(10 * 1024);
PacketSN sn = PacketSN.gen();
bb.put( new byte[2] );
bb.put( sn.getSn().array() );
bb.put( "E0".getBytes() );
bb.put( (byte) 0x34 ); // 批量加解密
bb.put( (byte) batchSize ); // 批量数
bb.put( (byte) ( 0x30 + nAlg) ); // 密钥模式
bb.put( (byte) ( 0x31) ); // ECB
bb.put( (byte) ( 0x30) ); // ZEK
// 填充多个密钥
for ( String key : seesionKey ) {
bb.put( key.getBytes() );
}
bb.put( (byte) ( 0x30) ); // 输入消息类型二进制
bb.put( (byte) ( 0x30) ); // 输出消息类型二进制
bb.put( (byte) ( 0x30 + fullmode) ); // 填充模式
bb.put( "0000".getBytes() ); // 填充字符
bb.put( (byte) ( 0x32) ); // 填充类型
// iv 暂未支持
// P 长度暂未支持
// 填充多个消息数据长度
for ( byte[] data : pINdata ) {
bb.put( String.format("%03X", data.length).getBytes() );
}
// 填充多个消息数据
for ( byte[] data : pINdata ) {
bb.put( data );
}
// 重新计算长度
int len = bb.position();
len -= 2;
byte[] lenArray = new byte[ 2 ];
lenArray[0] = (byte)((len >> 8) & 0xFF);
lenArray[1] = (byte)( len & 0xFF);
bb.put( 0, (byte)((len >> 8) & 0xFF) );
bb.put( 1, (byte)( len & 0xFF) );
// 通信
synchronized (this) {
// 响应解析
bb = syncRead(syncSend(bb));
}
bb.flip();
bb.position( 12 );
byte[] code = new byte[2];
bb.get( code );
int retCode = Integer.valueOf( new String( code ) );
if ( 0 != retCode ) {
throw new SydApiException( retCode );
}
// 跳过输出模式
bb.get();
// 获取 N 个长度
int[] lens = new int[ batchSize ];
for ( int i = 0; i < batchSize; i++ ){
byte[] iLenArray = new byte[3];
bb.get( iLenArray );
lens[ i ] = Integer.valueOf( new String( iLenArray ), 16 );
}
List<byte[]>ret = new ArrayList<byte[]>();
// 获取 N 个长度的数据
for ( int l : lens ) {
byte[] d = new byte[ l ];
bb.get(d);
ret.add( d );
}
return ret;
} }

View File

@ -0,0 +1,29 @@
package com.sunyard.sydapi.test;
import com.sunyard.proto.Util;
import racal.sunyard.main.SydApi4j;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
public class TestBatchData {
public static void main(String[] args) throws UnsupportedEncodingException {
System.setProperty("com.sunyard.sydapi4j.debug", "true");
SydApi4j.SYD_Set_Parameter_HA("unkown", "192.168.0.200", 8889, 3000);
SydApi4j api = (SydApi4j) new SydApi4j();//.connect("wuyong.fullstack.club" , 8889, null,1000);
String[] keys = new String[]{"SK000000000000000000A00000000"};
List<byte[]> pINdata = new ArrayList<byte[]>();
pINdata.add("12345678".getBytes());
List<byte[]> datas = api.SYMEncryptDecryptBatchData(0x8, keys, 1, pINdata);
// boolean pass = api.SM2Verify( 1, Util.hexString2Bytes(pk), Util.hexString2Bytes(inHexData), sign );
}
}