运行一次加密,再运行一次解密: 要不来试试{:3_57:}{:3_57:}
package cn.hyh_04;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class EncryptionDemo01 {
public static void main(String[] args) throws IOException {
File file = new File("E:\\aaa");
encrypt(file,null,true);
}
//加密功能:
public static void encrypt(File file,File newFile,boolean flag) throws IOException {
File parent;
if (flag) {
parent = file.getParentFile();
newFile = new File(parent,"mid"+file.getName());
newFile.mkdir();
}else{
parent = file.getParentFile();
newFile = new File(newFile,file.getName());
newFile.mkdir();
}
File[] fileArr = file.listFiles();
for (File sonArr : fileArr) {
if (sonArr.isDirectory()) {
encrypt(sonArr,newFile,false);
}else {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sonArr));
File encryptFile = new File(newFile, sonArr.getName());
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(encryptFile));
byte[] arr = new byte[1024];
int len;
while ((len = bis.read(arr)) != -1) {
byte[] encryptionArr = new byte[arr.length];
for (int i = 0;i<arr.length;i++) {
encryptionArr[i] = (byte) (arr[i]^1^2^3^4);
}
bos.write(encryptionArr,0,len);
}
bis.close();
bos.close();
}
}
if (flag) {
delete(file);
System.out.println(newFile.renameTo(file));
}
}
//删除功能:
public static void delete (File file) {
File[] fileArr = file.listFiles();
for (File sonFile : fileArr) {
if (!sonFile.delete()) {
delete(sonFile);
}
}
file.delete();
}
}
|
|