sydapi/src/main/java/com/sunyard/SYMEnDeLongData.java
2025-07-31 11:57:12 +08:00

195 lines
5.0 KiB
Java

package com.sunyard;
import com.sunyard.proto.Util;
import com.sunyard.trans.Alg;
import com.sunyard.trans.FullMode;
import com.sunyard.trans.RoundMode;
import racal.sunyard.main.SydApi4j;
public class SYMEnDeLongData {
private SydApi4j api;
private String sykey = "";
private String keyValue = "";
private int keyType = 0;
private int inType = 0;
private int outType = 0;
private byte[] iv = new byte[0];
private Alg alg = null;
private RoundMode roundMode=null;
private FullMode fullMode=null;
private byte[] data=null;
//private byte[] data=new byte[0];
private boolean isFirst = true;
private boolean isFinal = false;
private boolean isEn = true;
public RoundMode getRoundMode() {
return roundMode;
}
public SYMEnDeLongData setRoundMode(RoundMode roundMode) {
this.roundMode = roundMode;
return this;
}
public FullMode getFullMode() {
return fullMode;
}
public SYMEnDeLongData setFullMode(FullMode fullMode) {
this.fullMode = fullMode;
return this;
}
public byte[] getIv() {
return iv;
}
public SYMEnDeLongData setIv(byte[] iv) {
this.iv = iv;
return this;
}
public Alg getAlg() {
return alg;
}
public void setAlg(Alg alg) {
this.alg = alg;
}
public int getKeyType() {
return keyType;
}
public SYMEnDeLongData setKeyType(int keyType) {
this.keyType = keyType;
return this;
}
public int getInType() {
return inType;
}
public SYMEnDeLongData setInType(int inType) {
this.inType = inType;
return this;
}
public int getOutType() {
return outType;
}
public SYMEnDeLongData setOutType(int outType) {
this.outType = outType;
return this;
}
public SYMEnDeLongData(SydApi4j api, Alg alg, RoundMode roundMode, FullMode fullMode) {
this.api = api;
this.alg=alg;
this.roundMode=roundMode;
this.fullMode=fullMode;
}
public String getKey() {
return sykey;
}
public SYMEnDeLongData setKey(String sykey) {
this.sykey = sykey;
return this;
}
public String getKeyValue() {
return keyValue;
}
public SYMEnDeLongData setKeyValue(String keyValue) {
this.keyValue = keyValue;
return this;
}
public SYMEnDeLongData en(){
this.isEn = true;
return this;
}
public SYMEnDeLongData de(){
this.isEn = false;
return this;
}
public byte[] update(byte[] data){
if ( isFinal ){
throw new SydApiException("此时已经不能再更新",370000);
}
if ( this.isEn ){
// this.data = this.api.SYMEncryptData(this.key,this.keyValue,this.alg,this.roundMode,this.fullMode,this.isFirst ? 1 : 2 , data);
// System.out.println("块号block:"+(this.isFirst ? 1 : 2));
RetWrap en = this.api.SM4EncDecDataCBC(this.isEn, this.keyType, this.keyValue, this.inType, this.outType, data, this.iv, this.isFirst ? 1 : 2);
this.data = (byte[]) en.get("msg_all");
this.iv = (byte[]) en.get("iv");
// System.out.println("下一IV长度:"+this.iv.length);
// System.out.println("iv---"+ Util.bytes2HexString(this.iv));
} else {
RetWrap de = this.api.SM4EncDecDataCBC(this.isEn, this.keyType, this.keyValue, this.inType, this.outType, data, this.iv, this.isFirst ? 1 : 2);
this.data = (byte[]) de.get("msg_all");
this.iv = (byte[]) de.get("iv");
}
this.isFirst = false;
return this.data;
}
public byte[] calc(byte[] lastData ){
return calc( lastData, lastData.length );
}
public byte[] calc(byte[] lastData, int len ) {
if ( isFinal ){
throw new SydApiException("此时已经不能再计算",370000);
}
byte[] last = null;
if( len < 0 ){
len = 0;
}
if ( 0 == lastData.length ){
last = new byte[0];
} else if ( lastData.length == len ){
last = lastData;
} else {
last = new byte[len];
System.arraycopy( lastData, 0, last, 0, len );
}
this.isFinal = true;
int block = 3;
if ( null == this.data ){
block = 0;
}
if (last.length>0){
if ( this.isEn ){
// System.out.println("block:"+block);
this.data = (byte[]) this.api.SM4EncDecDataCBC(this.isEn, this.keyType, this.keyValue, this.inType, this.outType, last, this.iv, block).get("msg_all");
} else {
// System.out.println("block:"+block);
this.data = (byte[]) this.api.SM4EncDecDataCBC(this.isEn, this.keyType, this.keyValue, this.inType, this.outType, last, this.iv, block).get("msg_all");
}
}
return this.data;
}
}