1、优化sm4_cbc加解密方法
This commit is contained in:
@@ -0,0 +1,194 @@
|
||||
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 key = "";
|
||||
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 key;
|
||||
}
|
||||
|
||||
public SYMEnDeLongData setKey(String key) {
|
||||
this.key = key;
|
||||
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.SM4EncryptDataCBC(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.SM4EncryptDataCBC(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.SM4EncryptDataCBC(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.SM4EncryptDataCBC(this.isEn, this.keyType, this.keyValue, this.inType, this.outType, last, this.iv, block).get("msg_all");
|
||||
}
|
||||
}
|
||||
|
||||
return this.data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.sunyard.trans;
|
||||
|
||||
public enum Alg {
|
||||
|
||||
DES("DES", 0),
|
||||
DES3("DES3", 1),
|
||||
AES( "AES", 2),
|
||||
AES256( "AES256", 3),
|
||||
SSF33( "SSF33", 4),
|
||||
SM1( "SM1", 5),
|
||||
SM2( "SM2", 6),
|
||||
SM3( "SM3", 7),
|
||||
SM4( "SM4", 8),
|
||||
RSA( "RSA", 9);
|
||||
|
||||
|
||||
private String name;
|
||||
private int value;
|
||||
// 构造方法
|
||||
private Alg(String name, int value) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -4,13 +4,34 @@ package com.sunyard.trans;
|
||||
*/
|
||||
public enum FullMode {
|
||||
|
||||
PKCS("PKCS", 1), //PKCS5/PKCS7
|
||||
NOFILL( "NOFILL", 2),
|
||||
WEAKFILL( "WEAKFILL", 3),
|
||||
STRONGFILL( "STRONGFILL", 4);
|
||||
|
||||
private String name;
|
||||
private int value;
|
||||
// 构造方法
|
||||
private FullMode(String name, int value) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果数据块长度为 8/16 的整数倍,不需要填
|
||||
充;如果长度不为 8/16 的整数倍,填充至长度满
|
||||
8/16 的整数倍(填充方式按 Pad character 域中
|
||||
描述的规则进行,由加密机内部自动完成)
|
||||
*/
|
||||
FULL_8_16,
|
||||
// FULL_8_16,
|
||||
/**
|
||||
* 如果数据块长度为 8/16 的整数倍,填充一个
|
||||
整 8/16 字节的数据(填充方式按 Pad character
|
||||
@@ -19,5 +40,7 @@ public enum FullMode {
|
||||
的整数倍(填充方式按 Pad character 域中描述
|
||||
的规则进行,由加密机内部自动完成)
|
||||
*/
|
||||
FULL_MORE_8_16
|
||||
// FULL_MORE_8_16
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.sunyard.trans;
|
||||
|
||||
public enum RoundMode {
|
||||
|
||||
ECB("ECB", 1),
|
||||
CBC( "CBC", 2),
|
||||
CFB( "CFB", 3),
|
||||
OFB( "OFB", 4);
|
||||
|
||||
private String name;
|
||||
private int value;
|
||||
// 构造方法
|
||||
private RoundMode(String name, int value) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -64,10 +64,10 @@ public class FileUtil {
|
||||
* @param path 文件路径
|
||||
* @param data 数据
|
||||
*/
|
||||
public static void printDataToFile(String path, String data) {
|
||||
BufferedWriter osw = null;
|
||||
public static void printDataToFile(String path, byte[] data) {
|
||||
BufferedOutputStream osw = null;
|
||||
try {
|
||||
osw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), "UTF-8"));
|
||||
osw = new BufferedOutputStream(new FileOutputStream(path));
|
||||
|
||||
osw.write(data);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user