EO指令新增通过索引去就行sm4加解密的接口

This commit is contained in:
zhoubin 2022-03-08 09:30:28 +08:00
parent 85edd173ae
commit c320566d9c
4 changed files with 88 additions and 1 deletions

Binary file not shown.

View File

@ -246,6 +246,16 @@ interface SydSYMApi {
*/ */
byte[] SYMEncryptData(int nAlg, String seesionKey, byte[] pINdata); byte[] SYMEncryptData(int nAlg, String seesionKey, byte[] pINdata);
/**
* 对指定报文数据进行SM4加密
* @param type 密钥类型0: ZEK,1: TEK
* @param userId 生成密钥时的机构号
* @param index 密钥索引
* @param data 待加密的数据
* @return
*/
byte[] SYMSM4EncDataByIndex(int type, String userId, String index, byte[] data);
/** /**
* 用输入的会话密钥对指定报文数据进行加密处理 * 用输入的会话密钥对指定报文数据进行加密处理
* *

View File

@ -1911,6 +1911,25 @@ public class SydApi4j implements SydApi {
return retWrap; return retWrap;
} }
/**
* 对指定报文数据进行SM4加密
* @param type 密钥类型0: ZEK,1: TEK
* @param userId 生成密钥时的机构号
* @param index 密钥索引
* @param data 待加密的数据
* @return
*/
public byte[] SYMSM4EncDataByIndex(int type, String userId, String index, byte[] data) {
if (userId.length() > 16 || index.length() > 8) {
throw new SydApiException("机构号或索引长度不匹配", -27);
}
String indexN = "0000000000000000" + index;
indexN = indexN.substring(indexN.length() - 8);
return SYMEncryptData(SydApi.NALG_SM4, "XK" + SYMUtil.genPrivateKeyMark(userId, 0 == type ? "00A" : "00B", indexN), 1, data);
}
@Override @Override
public byte[] SYMEncryptData(int nAlg, String seesionKey, int fullMode, byte[] pInData) { public byte[] SYMEncryptData(int nAlg, String seesionKey, int fullMode, byte[] pInData) {
@ -2033,6 +2052,26 @@ public class SydApi4j implements SydApi {
return SYMDecryptData(nAlg, seesionKey, 1, pInData); return SYMDecryptData(nAlg, seesionKey, 1, pInData);
} }
/**
* 对指定报文数据进行SM4解密
* @param type 密钥类型0: ZEK,1: TEK
* @param userId 生成密钥时的机构号
* @param index 密钥索引
* @param pINdata 要解密的报文密文长度必须是8 或16 的倍数
* @return
*/
public byte[] SYMSM4DecDataByIndex (int type, String userId, String index, byte[] pINdata) {
if (userId.length() > 16 || index.length() > 8) {
throw new SydApiException("机构号或索引长度不匹配", -27);
}
String indexN = "00000000" + index;
indexN = indexN.substring(indexN.length() - 8);
return SYMDecryptData(SydApi4j.NALG_SM4, "XK" + SYMUtil.genPrivateKeyMark(userId, 0 == type ? "00A" : "00B", indexN), 1, pINdata);
}
@Override @Override
public byte[] SYMDecryptData(int nAlg, String seesionKey, int fullmode, byte[] pInData) { public byte[] SYMDecryptData(int nAlg, String seesionKey, int fullmode, byte[] pInData) {
if (2 == nAlg && 'L' != seesionKey.charAt(0)) { if (2 == nAlg && 'L' != seesionKey.charAt(0)) {

View File

@ -36,7 +36,8 @@ public class TestAllMethods {
public void before() { public void before() {
// 通过设置debug的属性开启debug如果不设置则默认不打开 // 通过设置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("172.16.18.59", 8889, null, 1000);
api = (SydApi4j) new SydApi4j().connect("192.168.0.119", 8889, null, 1000);
// api = (SydApi4j) new SydApi4j().connect("61.164.40.82", 8889, null, 1000); // api = (SydApi4j) new SydApi4j().connect("61.164.40.82", 8889, null, 1000);
// api = (SydApi4j) new SydApi4j().connect("192.168.0.66", 8889, null, 1000); // api = (SydApi4j) new SydApi4j().connect("192.168.0.66", 8889, null, 1000);
// api = (SydApi4j) new SydApi4j().connect("192.168.0.100", 8889, null, 1000); // api = (SydApi4j) new SydApi4j().connect("192.168.0.100", 8889, null, 1000);
@ -710,6 +711,7 @@ public class TestAllMethods {
String key = "X6CEE744329499B426CEE744329499B42"; String key = "X6CEE744329499B426CEE744329499B42";
String msg = "11111111111"; String msg = "11111111111";
byte[] bytes = api.SM4EncryptDataCBC(0, key, 0, 0 , msg.getBytes()); byte[] bytes = api.SM4EncryptDataCBC(0, key, 0, 0 , msg.getBytes());
System.out.println("输出为: " + Util.bytes2HexString(bytes));
byte[] bytes1 = api.SM4DecryptDataCBC(0, key, 0, 0, bytes); byte[] bytes1 = api.SM4DecryptDataCBC(0, key, 0, 0, bytes);
@ -803,6 +805,42 @@ public class TestAllMethods {
System.out.println("明文:" + new String(bytes1)); System.out.println("明文:" + new String(bytes1));
} }
// 测试通过
// E0
@Test
public void testEncDecData1() {
// 000000000000011100000000001
String key = "XK000000000000011100000000001";
String msg = "111111111111111111qewehtrhq1";
byte[] bytes = api.SYMEncryptDataCBC(1, key, msg.getBytes());
System.out.println("输出为: " + Util.bytes2HexString(bytes));
byte[] bytes1 = api.SYMDecryptDataCBC(1, key, bytes);
System.out.println("明文:" + new String(bytes1));
}
// 测试通过
// E0
@Test
public void testEncDecData2() {
String userId = "0000000000000111";
String index = "00000001";
String msg = "1011111111111111101";
byte[] bytes = api.SYMSM4EncDataByIndex(0, userId, index, msg.getBytes());
System.out.println("输出为: " + Util.bytes2HexString(bytes));
byte[] bytes1 = api.SYMSM4DecDataByIndex(0, userId, index, bytes);
System.out.println("明文:" + new String(bytes1));
}
@Test @Test
public void testaaa() throws UnsupportedEncodingException { public void testaaa() throws UnsupportedEncodingException {
String key = "X6CEE744329499B426CEE744329499B42"; String key = "X6CEE744329499B426CEE744329499B42";