124 lines
4.7 KiB
Java
124 lines
4.7 KiB
Java
package com.sunyard.sge.database;
|
|
|
|
import com.sunyard.SydApiException;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
public class SydApiSM4 {
|
|
|
|
private SydApi api;
|
|
private int iKeyIndex;
|
|
private byte[] pcdataResult = new byte[0];
|
|
private int iPkgNum = 1;
|
|
private int iFlag;
|
|
private boolean is_finished = false;
|
|
ByteBuffer sourceWrap = ByteBuffer.wrap(new byte[0]);
|
|
|
|
|
|
public SydApiSM4(SydApi api, int iKeyIndex, int endeFlag) {
|
|
this.api = api;
|
|
this.iFlag = endeFlag;
|
|
this.iKeyIndex = iKeyIndex;
|
|
}
|
|
|
|
public void update(byte[] pcData) {
|
|
this.pcdataResult = pcData;
|
|
if (null == pcData || pcData.length == 0) {
|
|
return;
|
|
}
|
|
int plength = pcData.length;
|
|
if (is_finished) {
|
|
throw new SydApiException(0);
|
|
}
|
|
if (plength > 4064) {
|
|
throw new SydApiException(0);
|
|
}
|
|
if (0 == sourceWrap.array().length) {
|
|
//加密
|
|
if (plength < 16) {
|
|
api.SYD_SM4_LongData(iKeyIndex, new byte[0], iPkgNum, iFlag);
|
|
if (iFlag == 0 && 1 == iPkgNum) {
|
|
throw new SydApiException("解密的首包不能小于 16 字节", 0);
|
|
}
|
|
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
|
|
bufferResult.put(sourceWrap);
|
|
bufferResult.put(pcData);
|
|
sourceWrap = bufferResult;
|
|
}
|
|
if (plength % 16 != 0) {
|
|
ByteBuffer buffer = ByteBuffer.wrap(pcData);
|
|
int remainder = plength % 16;
|
|
int intercept = plength - remainder;
|
|
byte[] bytes = new byte[intercept];
|
|
ByteBuffer byteBuffer = buffer.get(bytes, 0, intercept);
|
|
byte[] bytes1 = new byte[remainder];
|
|
byteBuffer.get(bytes1);
|
|
this.pcdataResult = bytes;
|
|
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + remainder);
|
|
bufferResult.put(bytes1);
|
|
sourceWrap = bufferResult;
|
|
}
|
|
byte[] bytes = api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag);
|
|
iPkgNum = 2;
|
|
} else {
|
|
if (16 > pcData.length + sourceWrap.array().length) {
|
|
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
|
|
bufferResult.put(SydSM4Mac.bytebuffer2ByteArray(sourceWrap));
|
|
bufferResult.put(pcData);
|
|
sourceWrap = bufferResult;
|
|
} else if ((pcData.length + sourceWrap.array().length) % 16 == 0) {
|
|
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
|
|
bufferResult.put(SydSM4Mac.bytebuffer2ByteArray(sourceWrap));
|
|
bufferResult.put(pcData);
|
|
byte[] bytes = SydSM4Mac.bytebuffer2ByteArray(bufferResult);
|
|
api.SYD_SM4_LongData(iKeyIndex, bytes, iPkgNum, iFlag);
|
|
} else {
|
|
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
|
|
bufferResult.put(SydSM4Mac.bytebuffer2ByteArray(sourceWrap));
|
|
bufferResult.put(pcData);
|
|
byte[] bytes = SydSM4Mac.bytebuffer2ByteArray(bufferResult);
|
|
ByteBuffer buffer = ByteBuffer.wrap(bytes);
|
|
int remainder = bytes.length % 16;
|
|
int intercept = bytes.length - remainder;
|
|
byte[] pcdataByte = new byte[intercept];
|
|
ByteBuffer byteBuffer = buffer.get(pcdataByte, 0, intercept);
|
|
byte[] bytes1 = new byte[remainder];
|
|
byteBuffer.get(bytes1);
|
|
this.pcdataResult = pcdataByte;
|
|
ByteBuffer bufferResults = ByteBuffer.allocate(remainder);
|
|
bufferResults.put(bytes1);
|
|
sourceWrap = bufferResults;
|
|
api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag);
|
|
iPkgNum = 2;
|
|
}
|
|
}
|
|
}
|
|
|
|
public byte[] finish() {
|
|
if (is_finished) {
|
|
throw new SydApiException(0);
|
|
}
|
|
this.is_finished = true;
|
|
if (1 == this.iPkgNum) {
|
|
this.iPkgNum = 0;
|
|
} else if (2 == this.iPkgNum) {
|
|
this.iPkgNum = 3;
|
|
}
|
|
|
|
byte[] bytes = api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag);
|
|
// sourceWrap = null;
|
|
return bytes;
|
|
}
|
|
|
|
public ByteBuffer bufferResult(int plength, byte[] pcData) {
|
|
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
|
|
bufferResult.put(sourceWrap);
|
|
bufferResult.put(pcData);
|
|
return bufferResult;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|