|
@@ -6,36 +6,70 @@ import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
|
-public class Test {
|
|
|
- static final String key="EC/Z+S7c3EFJa2dtvLyekg==";
|
|
|
- public static String encryptAES(String data, String key) throws Exception {
|
|
|
+/**
|
|
|
+ * 测试加解密方法
|
|
|
+ */
|
|
|
+public class EncryptionTest {
|
|
|
+ static final String key = "EC/Z+S7c3EFJa2dtvLyekg==";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 加密
|
|
|
+ *
|
|
|
+ * @param data
|
|
|
+ * @param key
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static String encryptAES(String data, String key) {
|
|
|
byte[] keyByte = Base64.decode(key);
|
|
|
AES aes = SecureUtil.aes(keyByte);
|
|
|
byte[] encryptData = aes.encrypt(data); //加密
|
|
|
String encryptDataEncode = Base64.encode(encryptData);
|
|
|
- return encryptDataEncode;
|
|
|
+ return encryptDataEncode;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试
|
|
|
+ *
|
|
|
+ * @param args
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
System.out.println("开始");
|
|
|
- String sp_jia = "D:\\test\\test+\\100w.txt";//原始文件
|
|
|
+ String sp_jia = "D:\\test\\test+\\100w.txt";//加密后文件
|
|
|
String sp = "D:\\test\\100w.txt";//原始文件
|
|
|
- String sp_jie = "D:\\test\\test-\\100w.txt";//原始文件
|
|
|
- String readToString=readToString(sp);//读取文件
|
|
|
- String jiami=encryptAES(readToString,null);
|
|
|
- getFile(jiami,sp_jia);//加密
|
|
|
+ String sp_jie = "D:\\test\\test-\\100w.txt";//解密后文件
|
|
|
+ String readToString = readToString(sp);//读取文件
|
|
|
+ String jiami = encryptAES(readToString, key);
|
|
|
+ getFile(jiami, sp_jia);//加密
|
|
|
System.out.println("写入完毕");
|
|
|
- String readToString_jia=readToString(sp_jia);//读取文件
|
|
|
- String jiemi=jmencryptAES(readToString_jia,null);
|
|
|
- getFile(jiemi,sp_jie);
|
|
|
+ String readToString_jia = readToString(sp_jia);//读取文件
|
|
|
+ String jiemi = jmencryptAES(readToString_jia, key);
|
|
|
+ getFile(jiemi, sp_jie);//解密
|
|
|
System.out.println("完毕");
|
|
|
}
|
|
|
- public static void getFile(String name,String path) throws IOException {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 写入文件
|
|
|
+ *
|
|
|
+ * @param name
|
|
|
+ * @param path
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static void getFile(String name, String path) throws IOException {
|
|
|
Writer writer = new FileWriter(path, true);
|
|
|
BufferedWriter bufw = new BufferedWriter(writer);
|
|
|
bufw.write(name);
|
|
|
bufw.newLine();
|
|
|
bufw.flush();
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 读取文件
|
|
|
+ *
|
|
|
+ * @param fileName
|
|
|
+ * @return
|
|
|
+ */
|
|
|
public static String readToString(String fileName) {
|
|
|
String encoding = "UTF-8";
|
|
|
File file = new File(fileName);
|
|
@@ -58,12 +92,21 @@ public class Test {
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
- public static String jmencryptAES(String data, String key) throws Exception {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解密
|
|
|
+ *
|
|
|
+ * @param data
|
|
|
+ * @param key
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static String jmencryptAES(String data, String key) {
|
|
|
byte[] data2 = Base64.decode(data);
|
|
|
byte[] key2 = Base64.decode(key);
|
|
|
AES aes2 = SecureUtil.aes(key2);
|
|
|
byte[] decrypt = aes2.decrypt(data2);
|
|
|
- return new String (decrypt);
|
|
|
+ return new String(decrypt);
|
|
|
}
|
|
|
}
|
|
|
|