三类模式写
This commit is contained in:
@@ -0,0 +1,186 @@
|
||||
package com.sunyard.sge.database;
|
||||
|
||||
import com.sunyard.SydApiException;
|
||||
import com.sunyard.proto.Util;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class SydSM4Backup {
|
||||
|
||||
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];
|
||||
|
||||
ByteBuffer sourceWrap = ByteBuffer.wrap(new byte[0]);
|
||||
|
||||
|
||||
public SydSM4Backup(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(0);
|
||||
}
|
||||
if (plength > 4064) {
|
||||
throw new SydApiException(0);
|
||||
}
|
||||
if (iFlag == 1) {
|
||||
if (0 == sourceWrap.array().length) {
|
||||
//加密
|
||||
if (plength < 16) {
|
||||
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
|
||||
bufferResult.put(sourceWrap);
|
||||
bufferResult.put(pcData);
|
||||
sourceWrap = bufferResult;
|
||||
return new byte[0];
|
||||
}
|
||||
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;
|
||||
return bytes;
|
||||
|
||||
} else {
|
||||
if (16 > pcData.length + sourceWrap.array().length) {
|
||||
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
|
||||
bufferResult.put(sourceWrap.array());
|
||||
bufferResult.put(pcData);
|
||||
sourceWrap = bufferResult;
|
||||
return new byte[0];
|
||||
} else if ((pcData.length + sourceWrap.array().length) % 16 == 0) {
|
||||
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
|
||||
bufferResult.put(sourceWrap.array());
|
||||
bufferResult.put(pcData);
|
||||
byte[] bytes = SydSM4Mac.bytebuffer2ByteArray(bufferResult);
|
||||
byte[] bytes1 = api.SYD_SM4_LongData(iKeyIndex, bytes, iPkgNum, iFlag);
|
||||
iPkgNum = 2;
|
||||
return bytes1;
|
||||
} else {
|
||||
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
|
||||
bufferResult.put(sourceWrap.array());
|
||||
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;
|
||||
|
||||
byte[] bytes2 = api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag);
|
||||
iPkgNum = 2;
|
||||
return bytes2;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
ByteBuffer nSourceWrap = ByteBuffer.allocate( sourceWrap.array().length + pcData.length );
|
||||
byte[] waitData = sourceWrap.array();
|
||||
nSourceWrap.put( waitData );
|
||||
nSourceWrap.put( pcData );
|
||||
sourceWrap = nSourceWrap;
|
||||
|
||||
System.out.println("new sourceWrap=" + Util.bytes2HexString(sourceWrap.array()));
|
||||
|
||||
int totalLen = sourceWrap.array().length;
|
||||
if ( totalLen / 16 < 2){
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
|
||||
int com = totalLen % 16;
|
||||
int sendBuffLen = totalLen - com -16;
|
||||
byte[] sendBuff = new byte[sendBuffLen];
|
||||
|
||||
|
||||
|
||||
sourceWrap.position(0);
|
||||
sourceWrap.get( sendBuff , 0, sendBuff.length );
|
||||
byte[] waitBuff = new byte[ totalLen - sendBuffLen ];
|
||||
sourceWrap.get( waitBuff , 0, waitBuff.length );
|
||||
|
||||
System.out.println( String.format("debug inDataLen=%d totalDataLen=%d sendDataLen=%d 16=%d iPkgNum=%d",
|
||||
pcData.length,
|
||||
totalLen,
|
||||
sendBuff.length,
|
||||
sendBuff.length % 16,
|
||||
iPkgNum)
|
||||
);
|
||||
|
||||
System.out.println( "waitBuff=" + Util.bytes2HexString( waitBuff ) );
|
||||
|
||||
if( 0x00 == waitBuff[0] ) {
|
||||
System.out.println("eee");
|
||||
}
|
||||
sourceWrap = ByteBuffer.wrap( waitBuff );
|
||||
System.out.println("sourceWrap=" + Util.bytes2HexString(sourceWrap.array()));
|
||||
byte[] bytes2 = api.SYD_SM4_LongData(iKeyIndex, sendBuff, iPkgNum, iFlag);
|
||||
|
||||
iPkgNum = 2;
|
||||
return bytes2;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
if (iFlag == 0){
|
||||
byte[] d = sourceWrap.array();
|
||||
byte[] bytes = api.SYD_SM4_LongData(iKeyIndex,d, iPkgNum, iFlag);
|
||||
sourceWrap = null;
|
||||
return bytes;
|
||||
}
|
||||
byte[] bytes = api.SYD_SM4_LongData(iKeyIndex,sourceWrap.array(), 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user