1、优化sm4_cbc加解密接口
This commit is contained in:
parent
b0a1c42ff3
commit
0ae7b29514
@ -131,14 +131,14 @@ public class SYMEnDeLongData {
|
|||||||
|
|
||||||
if ( this.isEn ){
|
if ( this.isEn ){
|
||||||
// this.data = this.api.SYMEncryptData(this.key,this.keyValue,this.alg,this.roundMode,this.fullMode,this.isFirst ? 1 : 2 , data);
|
// 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));
|
// 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);
|
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.data = (byte[]) en.get("msg_all");
|
||||||
this.iv = (byte[]) en.get("iv");
|
this.iv = (byte[]) en.get("iv");
|
||||||
System.out.println("下一IV长度:"+this.iv.length);
|
// System.out.println("下一IV长度:"+this.iv.length);
|
||||||
System.out.println("iv---"+ Util.bytes2HexString(this.iv));
|
// System.out.println("iv---"+ Util.bytes2HexString(this.iv));
|
||||||
} else {
|
} else {
|
||||||
RetWrap de = this.api.SM4EncryptDataCBC(this.isEn, this.keyType, this.keyValue, this.inType, this.outType, data, this.iv, this.isFirst ? 1 : 2);
|
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.data = (byte[]) de.get("msg_all");
|
||||||
this.iv = (byte[]) de.get("iv");
|
this.iv = (byte[]) de.get("iv");
|
||||||
}
|
}
|
||||||
@ -181,11 +181,11 @@ public class SYMEnDeLongData {
|
|||||||
if (last.length>0){
|
if (last.length>0){
|
||||||
|
|
||||||
if ( this.isEn ){
|
if ( this.isEn ){
|
||||||
System.out.println("block:"+block);
|
// 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");
|
this.data = (byte[]) this.api.SM4EncDecDataCBC(this.isEn, this.keyType, this.keyValue, this.inType, this.outType, last, this.iv, block).get("msg_all");
|
||||||
} else {
|
} else {
|
||||||
System.out.println("block:"+block);
|
// 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");
|
this.data = (byte[]) this.api.SM4EncDecDataCBC(this.isEn, this.keyType, this.keyValue, this.inType, this.outType, last, this.iv, block).get("msg_all");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -4250,7 +4250,7 @@ public class SydApi4j implements SydApi {
|
|||||||
*/
|
*/
|
||||||
// 测试通过
|
// 测试通过
|
||||||
// @Override
|
// @Override
|
||||||
public boolean SM4EncryptFileDataCBC(int keyType, String key, int inType, int outType, String inFilePath, String outFilePath) {
|
private boolean _SM4EncryptFileDataCBC(int keyType, String key, int inType, int outType, String inFilePath, String outFilePath) {
|
||||||
|
|
||||||
byte[] msg = FileUtil.getDataFromFile(inFilePath);
|
byte[] msg = FileUtil.getDataFromFile(inFilePath);
|
||||||
|
|
||||||
@ -4267,8 +4267,8 @@ public class SydApi4j implements SydApi {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Override
|
@Override
|
||||||
public boolean _SM4EncryptFileDataCBC(int keyType, String key, int inType, int outType, String inFilePath, String outFilePath) {
|
public boolean SM4EncryptFileDataCBC(int keyType, String key, int inType, int outType, String inFilePath, String outFilePath) {
|
||||||
|
|
||||||
byte[] iv = new byte[32];
|
byte[] iv = new byte[32];
|
||||||
Arrays.fill(iv, (byte) 0x30);
|
Arrays.fill(iv, (byte) 0x30);
|
||||||
@ -4279,13 +4279,13 @@ public class SydApi4j implements SydApi {
|
|||||||
File in = new File(inFilePath);
|
File in = new File(inFilePath);
|
||||||
|
|
||||||
if ( !in.exists() ){
|
if ( !in.exists() ){
|
||||||
System.out.println("要加密的文件不存在");
|
throw new SydApiException("要加密的文件不存在", -27);
|
||||||
}
|
}
|
||||||
|
|
||||||
long length = in.length();
|
long length = in.length();
|
||||||
int l = new Long(length).intValue();
|
int l = new Long(length).intValue();
|
||||||
int n = l / ( 2 * 1024);
|
int n = l / ( 2 * 1024);
|
||||||
System.out.println("n 个 2k:"+n);
|
println("n 个 2k:"+n);
|
||||||
|
|
||||||
if (l % ( 2 * 1024) != 0) {
|
if (l % ( 2 * 1024) != 0) {
|
||||||
n++;
|
n++;
|
||||||
@ -4300,12 +4300,12 @@ public class SydApi4j implements SydApi {
|
|||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
FileInputStream fis =null;
|
BufferedInputStream fis =null;
|
||||||
FileOutputStream fos=null;
|
BufferedOutputStream fos=null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
fis = new FileInputStream(in);
|
fis = new BufferedInputStream(new FileInputStream(in));
|
||||||
fos = new FileOutputStream(out);
|
fos = new BufferedOutputStream(new FileOutputStream(out));
|
||||||
byte[] buff = new byte[2 * 1024];
|
byte[] buff = new byte[2 * 1024];
|
||||||
|
|
||||||
for (int i = 0; i < n-1; i++) {
|
for (int i = 0; i < n-1; i++) {
|
||||||
@ -4316,7 +4316,6 @@ public class SydApi4j implements SydApi {
|
|||||||
println("len长度_update:"+len);
|
println("len长度_update:"+len);
|
||||||
byte[] en = sym.update( buff );
|
byte[] en = sym.update( buff );
|
||||||
println("en长度:"+en.length);
|
println("en长度:"+en.length);
|
||||||
// String str = Util.bytes2HexString(en);
|
|
||||||
fos.write( en );
|
fos.write( en );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -4328,7 +4327,6 @@ public class SydApi4j implements SydApi {
|
|||||||
println("len长度_calc:" + len);
|
println("len长度_calc:" + len);
|
||||||
byte[] en = sym.calc(buff, len);
|
byte[] en = sym.calc(buff, len);
|
||||||
println("en长度:" + en.length);
|
println("en长度:" + en.length);
|
||||||
// String str = Util.bytes2HexString(en);
|
|
||||||
fos.write( en );
|
fos.write( en );
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -4358,8 +4356,23 @@ public class SydApi4j implements SydApi {
|
|||||||
return new SYMEnDeLongData(this, Alg.SM4, RoundMode.CBC, FullMode.PKCS);
|
return new SYMEnDeLongData(this, Alg.SM4, RoundMode.CBC, FullMode.PKCS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用SM4算法进行加密,其中加密模式为CBC,
|
||||||
|
* Ⅳ使用缺省值0X 00000000000000000000000000000000,
|
||||||
|
* 补位模式为 PKCS5 PADDING 。
|
||||||
|
* @param isEn 是否加密
|
||||||
|
* @param keyType 密钥类型 0: ZEK,1: TEK
|
||||||
|
* @param key 密钥
|
||||||
|
* @param inType 输入消息类型 0:二进制 1:扩展十六进制
|
||||||
|
* @param outType 输出消息类型 0:二进制 1:扩展十六进制
|
||||||
|
* @param msg 消息数据
|
||||||
|
* @param iv iv
|
||||||
|
* @param block 消息块号
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
// @Override
|
// @Override
|
||||||
public RetWrap SM4EncryptDataCBC(Boolean isEn, int keyType, String key, int inType, int outType, byte[] msg, byte[] iv, int block) {
|
public RetWrap SM4EncDecDataCBC(Boolean isEn, int keyType, String key, int inType, int outType, byte[] msg, byte[] iv, int block) {
|
||||||
|
|
||||||
// 填充数据
|
// 填充数据
|
||||||
HashMap<String, Object> packet = new HashMap<String, Object>();
|
HashMap<String, Object> packet = new HashMap<String, Object>();
|
||||||
@ -4450,7 +4463,7 @@ public class SydApi4j implements SydApi {
|
|||||||
*/
|
*/
|
||||||
// 测试通过
|
// 测试通过
|
||||||
// @Override
|
// @Override
|
||||||
public boolean SM4DecryptFileDataCBC(int keyType, String key, int inType, int outType, String inFilePath, String outFilePath) throws UnsupportedEncodingException {
|
private boolean _SM4DecryptFileDataCBC(int keyType, String key, int inType, int outType, String inFilePath, String outFilePath) throws UnsupportedEncodingException {
|
||||||
|
|
||||||
byte[] msg = FileUtil.getDataFromFile(inFilePath);
|
byte[] msg = FileUtil.getDataFromFile(inFilePath);
|
||||||
|
|
||||||
@ -4466,8 +4479,8 @@ public class SydApi4j implements SydApi {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Override
|
@Override
|
||||||
public boolean _SM4DecryptFileDataCBC(int keyType, String key, int inType, int outType, String inFilePath, String outFilePath) {
|
public boolean SM4DecryptFileDataCBC(int keyType, String key, int inType, int outType, String inFilePath, String outFilePath) {
|
||||||
|
|
||||||
println("解密============================");
|
println("解密============================");
|
||||||
byte[] iv = new byte[32];
|
byte[] iv = new byte[32];
|
||||||
@ -4479,13 +4492,13 @@ public class SydApi4j implements SydApi {
|
|||||||
File in = new File(inFilePath);
|
File in = new File(inFilePath);
|
||||||
|
|
||||||
if ( !in.exists() ){
|
if ( !in.exists() ){
|
||||||
System.out.println("要解密的文件不存在");
|
throw new SydApiException("要解密的文件不存在", -27);
|
||||||
}
|
}
|
||||||
|
|
||||||
long length = in.length();
|
long length = in.length();
|
||||||
int l = new Long(length).intValue();
|
int l = new Long(length).intValue();
|
||||||
int n = l / ( 2 * 1024);
|
int n = l / ( 2 * 1024);
|
||||||
System.out.println("n 个 2k:"+n);
|
println("n 个 2k:"+n);
|
||||||
|
|
||||||
if (l % ( 2 * 1024) != 0) {
|
if (l % ( 2 * 1024) != 0) {
|
||||||
n++;
|
n++;
|
||||||
@ -4500,12 +4513,12 @@ public class SydApi4j implements SydApi {
|
|||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
FileInputStream fis =null;
|
BufferedInputStream fis =null;
|
||||||
FileOutputStream fos=null;
|
BufferedOutputStream fos=null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
fis = new FileInputStream(in);
|
fis = new BufferedInputStream(new FileInputStream(in));
|
||||||
fos = new FileOutputStream(out);
|
fos = new BufferedOutputStream(new FileOutputStream(out));
|
||||||
byte[] buff = new byte[2 * 1024];
|
byte[] buff = new byte[2 * 1024];
|
||||||
byte[] buff1 = new byte[2 * 1024 + 16];
|
byte[] buff1 = new byte[2 * 1024 + 16];
|
||||||
|
|
||||||
|
|||||||
@ -23,7 +23,7 @@ public class TestAllMethods {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void before() {
|
public void before() {
|
||||||
System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
System.setProperty("com.sunyard.sydapi4j.debug", "false");
|
||||||
// api = (SydApi4j) new SydApi4j().connect("172.16.17.163", 8889, null, 1000);
|
// api = (SydApi4j) new SydApi4j().connect("172.16.17.163", 8889, null, 1000);
|
||||||
api = (SydApi4j) new SydApi4j().connect("192.168.1.7", 8889, null, 5000);
|
api = (SydApi4j) new SydApi4j().connect("192.168.1.7", 8889, null, 5000);
|
||||||
// api = (SydApi4j) new SydApi4j().connect("192.168.0.66", 8889, null, 1000);
|
// api = (SydApi4j) new SydApi4j().connect("192.168.0.66", 8889, null, 1000);
|
||||||
@ -678,9 +678,9 @@ public class TestAllMethods {
|
|||||||
String outFilePath = "C:\\Users\\edgar\\Desktop\\fsdownload\\test\\in2.txt";
|
String outFilePath = "C:\\Users\\edgar\\Desktop\\fsdownload\\test\\in2.txt";
|
||||||
String outFilePath2 = "C:\\Users\\edgar\\Desktop\\fsdownload\\test\\out1.txt";
|
String outFilePath2 = "C:\\Users\\edgar\\Desktop\\fsdownload\\test\\out1.txt";
|
||||||
|
|
||||||
boolean b = api._SM4EncryptFileDataCBC(0, key, 0, 0, inFilePath, outFilePath);
|
boolean b = api.SM4EncryptFileDataCBC(0, key, 0, 0, inFilePath, outFilePath);
|
||||||
System.out.println(b);
|
System.out.println(b);
|
||||||
boolean c = api._SM4DecryptFileDataCBC(0, key, 0, 0, outFilePath, outFilePath2);
|
boolean c = api.SM4DecryptFileDataCBC(0, key, 0, 0, outFilePath, outFilePath2);
|
||||||
System.out.println(c);
|
System.out.println(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user