103 lines
2.8 KiB
Java
103 lines
2.8 KiB
Java
package com.sunyard.sge.database;
|
|
|
|
import com.sunyard.SydApiException;
|
|
import com.sunyard.sge.bytes.BytesQueue;
|
|
|
|
|
|
public class SydSM4 implements ISM4 {
|
|
|
|
private SydApi api;
|
|
private int iKeyIndex;
|
|
private byte[] pcdataResult = new byte[0];
|
|
private int iPkgNum = 1;
|
|
private int iFlag;
|
|
private boolean is_finished = false;
|
|
private byte[] dataResult = new byte[0];
|
|
private BytesQueue queue = new BytesQueue();
|
|
|
|
|
|
public SydSM4(SydApi api, int iKeyIndex, int endeFlag) {
|
|
this.api = api;
|
|
this.iFlag = endeFlag;
|
|
this.iKeyIndex = iKeyIndex;
|
|
}
|
|
|
|
public byte[] update(byte[] pcData) {
|
|
this.pcdataResult = pcData;
|
|
// this.dataResult = pcData;
|
|
if (null == pcData || pcData.length == 0) {
|
|
return new byte[0];
|
|
}
|
|
int plength = pcData.length;
|
|
if (is_finished) {
|
|
throw new SydApiException(0x8007);
|
|
}
|
|
if (plength > 4064) {
|
|
throw new SydApiException(0x8006);
|
|
}
|
|
if (iFlag == 1) {
|
|
if (0 == queue.getLength()) {
|
|
//加密
|
|
if (plength < 16) {
|
|
queue.push(pcData);
|
|
byte[] pop = queue.pop(16, 0);
|
|
pcdataResult = pop;
|
|
return new byte[0];
|
|
}
|
|
if (plength % 16 != 0) {
|
|
queue.push(pcData);
|
|
byte[] pop = queue.pop(16, 0);
|
|
pcdataResult = pop;
|
|
}
|
|
byte[] bytes = api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag);
|
|
iPkgNum = 2;
|
|
return bytes;
|
|
|
|
} else {
|
|
if (16 > (pcData.length + queue.getLength())) {
|
|
queue.push(pcData);
|
|
return new byte[0];
|
|
}
|
|
queue.push(pcData);
|
|
byte[] pop = queue.pop(16, 0);
|
|
byte[] bytes2 = api.SYD_SM4_LongData(iKeyIndex, pop, iPkgNum, iFlag);
|
|
iPkgNum = 2;
|
|
return bytes2;
|
|
}
|
|
}
|
|
else {
|
|
queue.push(pcData);
|
|
if ((queue.getLength()) / 16 < 2) {
|
|
return new byte[0];
|
|
}
|
|
byte[] pop = queue.pop(16, 16);
|
|
byte[] bytes2 = api.SYD_SM4_LongData(iKeyIndex, pop, iPkgNum, iFlag);
|
|
|
|
iPkgNum = 2;
|
|
return bytes2;
|
|
}
|
|
}
|
|
|
|
public byte[] finish() {
|
|
if (is_finished) {
|
|
throw new SydApiException(0x8007);
|
|
}
|
|
this.is_finished = true;
|
|
if (1 == this.iPkgNum) {
|
|
this.iPkgNum = 0;
|
|
} else if (2 == this.iPkgNum) {
|
|
this.iPkgNum = 3;
|
|
}
|
|
|
|
|
|
byte[] pop = queue.pop();
|
|
byte[] bytes = api.SYD_SM4_LongData(iKeyIndex, pop, iPkgNum, iFlag);
|
|
queue = null;
|
|
return bytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|