diff --git a/src/main/java/com/sunyard/inf/SydCryptionApi.java b/src/main/java/com/sunyard/inf/SydCryptionApi.java index 965e4f7..5f45fbb 100644 --- a/src/main/java/com/sunyard/inf/SydCryptionApi.java +++ b/src/main/java/com/sunyard/inf/SydCryptionApi.java @@ -3,6 +3,8 @@ package com.sunyard.inf; import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException; import com.sunyard.RetWrap; +import java.io.UnsupportedEncodingException; + /** * 对称加解密相关接口 */ @@ -300,7 +302,7 @@ interface SydSYMApi { * @param outFilePath 解密后的数据文件所在的路径 * @return 解密是否成功。false为失败,true为成功 */ - boolean SM4DecryptFileDataCBC(int keyType, String key, int inType, int outType, String inFilePath, String outFilePath); + boolean SM4DecryptFileDataCBC(int keyType, String key, int inType, int outType, String inFilePath, String outFilePath) throws UnsupportedEncodingException; /** * 使用SM4算法进行解密,其中加密模式为CBC, diff --git a/src/main/java/com/sunyard/util/FileUtil.java b/src/main/java/com/sunyard/util/FileUtil.java index 72ea3a3..4811a87 100644 --- a/src/main/java/com/sunyard/util/FileUtil.java +++ b/src/main/java/com/sunyard/util/FileUtil.java @@ -65,9 +65,9 @@ public class FileUtil { * @param data 数据 */ public static void printDataToFile(String path, String data) { - OutputStreamWriter osw = null; + BufferedWriter osw = null; try { - osw = new OutputStreamWriter(new FileOutputStream(path)); + osw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), "UTF-8")); osw.write(data); diff --git a/src/main/java/racal/sunyard/main/SydApi4j.java b/src/main/java/racal/sunyard/main/SydApi4j.java index 9520ab6..2f5ba6a 100644 --- a/src/main/java/racal/sunyard/main/SydApi4j.java +++ b/src/main/java/racal/sunyard/main/SydApi4j.java @@ -1608,7 +1608,7 @@ public class SydApi4j implements SydApi { iv = niv; //存储iv retWrap.put("iv", iv); - System.out.println("下一iv长度:" + iv.length); +// System.out.println("下一iv长度:" + iv.length); } } @@ -4318,7 +4318,7 @@ public class SydApi4j implements SydApi { */ // 测试通过 @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) throws UnsupportedEncodingException { byte[] msg = FileUtil.getDataFromFile(inFilePath); @@ -4329,7 +4329,7 @@ public class SydApi4j implements SydApi { // 调用加密接口将数据加密 byte[] result = SM4DecryptDataCBC(keyType, key, inType, outType, Util.hexString2Bytes(new String(msg))); - String str = new String(Util.hexString2Bytes(Util.bytes2HexString(result))); + String str = new String(result, "utf-8"); FileUtil.printDataToFile(outFilePath, str); diff --git a/src/test/java/com/sunyard/sydapi/example/ABCNorthBranch.java b/src/test/java/com/sunyard/sydapi/example/ABCNorthBranch.java index 0da81fc..ed969a9 100644 --- a/src/test/java/com/sunyard/sydapi/example/ABCNorthBranch.java +++ b/src/test/java/com/sunyard/sydapi/example/ABCNorthBranch.java @@ -7,6 +7,8 @@ import org.junit.Before; import org.junit.Test; import racal.sunyard.main.SydApi4j; +import java.io.UnsupportedEncodingException; + import static com.sunyard.sydapi.util.PrintUtil.printByteArray; import static com.sunyard.sydapi.util.PrintUtil.printRetWrap; @@ -146,7 +148,7 @@ public class ABCNorthBranch { // 测试通过 // E0 @Test - public void testSM4DecryptFileDataCBC() { + public void testSM4DecryptFileDataCBC() throws UnsupportedEncodingException { String outFilePath = "C:\\Users\\edgar\\Desktop\\fsdownload\\test\\in2.txt"; String outFilePath2 = "C:\\Users\\edgar\\Desktop\\fsdownload\\test\\out1.txt"; diff --git a/src/test/java/com/sunyard/sydapi/test/TestAllMethods.java b/src/test/java/com/sunyard/sydapi/test/TestAllMethods.java index fd98fea..56d08c1 100644 --- a/src/test/java/com/sunyard/sydapi/test/TestAllMethods.java +++ b/src/test/java/com/sunyard/sydapi/test/TestAllMethods.java @@ -663,7 +663,7 @@ public class TestAllMethods { // 测试通过 // E0 @Test - public void testSM4DecryptFileDataCBC() { + public void testSM4DecryptFileDataCBC() throws UnsupportedEncodingException { String key = "X6CEE744329499B426CEE744329499B42"; String outFilePath = "C:\\Users\\edgar\\Desktop\\fsdownload\\test\\in2.txt"; String outFilePath2 = "C:\\Users\\edgar\\Desktop\\fsdownload\\test\\out1.txt"; @@ -671,6 +671,19 @@ public class TestAllMethods { System.out.println(b); } + @Test + public void testaaa() throws UnsupportedEncodingException { + String key = "X6CEE744329499B426CEE744329499B42"; + String inFilePath = "C:\\Users\\edgar\\Desktop\\fsdownload\\test\\in1.txt"; + String outFilePath = "C:\\Users\\edgar\\Desktop\\fsdownload\\test\\in2.txt"; + String outFilePath2 = "C:\\Users\\edgar\\Desktop\\fsdownload\\test\\out1.txt"; + + boolean b = api.SM4EncryptFileDataCBC(0, key, 0, 0, inFilePath, outFilePath); + System.out.println(b); + boolean c = api.SM4DecryptFileDataCBC(0, key, 0, 0, outFilePath, outFilePath2); + System.out.println(c); + } + // 测试通过 // Y0 @Test