}
2、异或加密
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
try {
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("E:\\ico2.png"));
List<Integer> list =new ArrayList<Integer>();//定义集合<字节>用来存储数据
int len; //定义变量,用来存储数据
while((len=bis.read()) !=-1) //循环读取,直到读取到末尾为止
list.add(len^123);
bis.close(); //关闭输入流
BufferedOutputStream bos =new BufferedOutputStream(new FileOutputStream("E:\\ico2.png"));
for(Integer i:list)//遍历集合,将所有数据写回文件
bos.write(i);
bos.close(); //关闭输出流
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//从文件中逐个字节读取数据,异或密码,存入集合
}
}
3、cipher 加密
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.GeneralSecurityException;
import java.security.Key;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import android.util.Log;
/**
* 使用AES对文件进行加密和解密
*
*/
public class CipherUtil {
/**
* 使用AES对文件进行加密和解密
*
*/
private static String type = "AES";
/**
* 把文件srcFile加密后存储为destFile
* @param srcFile 加密前的文件
* @param destFile 加密后的文件
* @param privateKey 密钥
* @throws GeneralSecurityException
* @throws IOException
*/
public static void encrypt(String srcFile, String destFile, String privateKey) throws GeneralSecurityException, IOException {
Key key = getKey(privateKey);
Cipher cipher = Cipher.getInstance(type + "/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(mkdirFiles(destFile));
crypt(fis, fos, cipher);
Log.i("tag", "in encrypt ----");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
fis.close();
}
if (fos != null) {
fos.close();
}
}
}
/**
* 把文件srcFile解密后存储为destFile
* @param srcFile 解密前的文件
* @param destFile 解密后的文件
* @param privateKey 密钥
* @throws GeneralSecurityException
* @throws IOException
*/
public static void decrypt(String srcFile, String destFile, String privateKey) throws GeneralSecurityException, IOException {
Key key = getKey(privateKey);
Cipher cipher = Cipher.getInstance(type + "/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(mkdirFiles(destFile));
crypt(fis, fos, cipher);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
fis.close();
}
if (fos != null) {
fos.close();
}
}
}
/**
* 根据filePath创建相应的目录
* @param filePath 要创建的文件路经
* @return file 文件
* @throws IOException
*/
private static File mkdirFiles(String filePath) throws IOException {
File file = new File(filePath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
file.createNewFile();
return file;
}
/**
* 生成指定字符串的密钥
* @param secret 要生成密钥的字符串
* @return secretKey 生成后的密钥
* @throws GeneralSecurityException
*/
private static Key getKey(String secret) throws GeneralSecurityException {
KeyGenerator kgen = KeyGenerator.getInstance(type);
kgen.init(128, new SecureRandom(secret.getBytes()));
SecretKey secretKey = kgen.generateKey();
return secretKey;
}
/**
* 加密解密流
* @param in 加密解密前的流
* @param out 加密解密后的流
* @param cipher 加密解密
* @throws IOException
* @throws GeneralSecurityException
*/
private static void crypt(InputStream in, OutputStream out, Cipher cipher) throws IOException, GeneralSecurityException {
int blockSize = cipher.getBlockSize() * 1000;
int outputSize = cipher.getOutputSize(blockSize);
byte[] inBytes = new byte[blockSize];
byte[] outBytes = new byte[outputSize];
int inLength = 0;
boolean more = true;
while (more) {
inLength = in.read(inBytes);
if (inLength == blockSize) {
int outLength = cipher.update(inBytes, 0, blockSize, outBytes);
out.write(outBytes, 0, outLength);
} else {
more = false;
}
}
if (inLength > 0)
outBytes = cipher.doFinal(inBytes, 0, inLength);
else
outBytes = cipher.doFinal();
out.write(outBytes);
}
/**
* 删除目录及文件递归函数
* @param file
*/
public static void delDir(File file){
File[] files=file.listFiles();
for(File f:files){
while(f.exists())
if(f.isFile()){
f.delete();
}else {
if(f.delete())
System.out.println(f.getName()+"deleted");
else
delDir(f);
}
}
}
/**
* 删除文件,可以是单个文件或文件夹
* @param fileName 待删除的文件名
* @return 文件删除成功返回true,否则返回false
*/
public static boolean delete(String fileName){
File file = new File(fileName);
if(!file.exists()){
System.out.println("删除文件失败:"+fileName+"文件不存在");
return false;
}else{
if(file.isFile()){
return deleteFile(fileName);
}else{
return deleteDirectory(fileName);
}
}
}
/**
* 删除单个文件
* @param fileName 被删除文件的文件名
* @return 单个文件删除成功返回true,否则返回false
*/
public static boolean deleteFile(String fileName){
File file = new File(fileName);
if(file.isFile() && file.exists()){
file.delete();
return true;
}else{
return false;
}
}
/**
* 删除目录下的文件
* @param file
*/
/**
* 删除目录(文件夹)以及目录下的文件
* @param dir 被删除目录的文件路径
* @return 目录删除成功返回true,否则返回false
*/
public static boolean deleteDirectory(String dir){
//如果dir不以文件分隔符结尾,自动添加文件分隔符
if(!dir.endsWith(File.separator)){
dir = dir+File.separator;
}
File dirFile = new File(dir);
//如果dir对应的文件不存在,或者不是一个目录,则退出
if(!dirFile.exists() || !dirFile.isDirectory()){
return false;
}
boolean flag = true;
//删除文件夹下的所有文件(包括子目录)
File[] files = dirFile.listFiles();
for(int i=0;i<files.length;i++){
//删除子文件
if(files[i].isFile()){
flag = deleteFile(files[i].getAbsolutePath());
if(!flag){
break;
}
}
//删除子目录
else{
flag = deleteDirectory(files[i].getAbsolutePath());
if(!flag){
break;
}
}
}
if(!flag){
System.out.println("删除目录失败");
return false;
}
//删除当前目录
if(dirFile.delete()){
return true;
}else{
System.out.println("删除目录"+dir+"失败!");
return false;
}
}
/**
* 将文件夹的所有文件解密到另一个文件夹
* @param pathFile
* @throws IOException
* @throws GeneralSecurityException
*/
public static int decryptDir(String srcDir,String decryptDir)
throws GeneralSecurityException, IOException {
int num = 0;
File file = new File(srcDir);
if(file==null||!file.exists()){
file.mkdir();
}
if (file != null && file.exists()) {
File[] files = file.listFiles();
//遍历当前目录下的文件和文件夹(忽略点文件),存进ArrayList
for (File f : files) {
if(f.getName().endsWith(".dat")||f.getName().endsWith(".DAT")){
String fname = f.getName().replace(".dat", ".jpg");
fname = fname.replace(".DAT", ".jpg");
decrypt(f.getPath(), decryptDir+fname, CommonUtil.KEY_STR);
num++;
}
}
}
return num;
}
}