package com.xy.jx.util;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* Created by xuxiang on 2017/6/10.
*/
public class FileAESUtil {
/**
* 加密用的Key 可以用26个字母和数字组成 此处使用AES-128-CBC加密模式,key需要为16位。
**/
private static String sKey = "写入您的加密key";//key,可自行修改
private static String ivParameter = "写入您的偏移量";//偏移量,可自行修改
/**
* 算法/模式/填充
**/
private static final String CipherMode = "AES/CBC/PKCS5Padding";
/**
* 加密文件
* @param readPath 源文件路径
* @param writePath 加密后文件路径
*/
public static void setAESFile(String readPath, String writePath) {
File file = new File(readPath);
if(!file.exists()) return;
File writeFile = new File(writePath);
InputStream inputStream = null;
OutputStream outputStream = null;
byte bt[] = new byte[(int) file.length()];
try {
if(!writeFile.exists()) writeFile.createNewFile();
inputStream = new FileInputStream(file);
outputStream = new FileOutputStream(writeFile);
int byteread = 0;
// 读入多个字节到字节数组中,byteread为一次读入的字节数
while ((byteread = inputStream.read(bt)) != -1) {
outputStream.write(encrypt(bt));
outputStream.flush();
}
file.delete();
}catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(outputStream != null)
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (inputStream != null)
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 解密文件
* @param readPath 源文件路径
* @param writePath 解密后文件路径
*/
public static File readAESFile(String readPath, String writePath) {
File file = new File(readPath);
if(!file.exists()) return null;
File writeFile = new File(writePath);
InputStream inputStream = null;
OutputStream outputStream = null;
byte bt[] = new byte[(int) file.length()];
try {
if(!writeFile.exists()) writeFile.createNewFile();
inputStream = new FileInputStream(file);
outputStream = new FileOutputStream(writeFile);
int byteread = 0;
// 读入多个字节到字节数组中,byteread为一次读入的字节数
while ((byteread = inputStream.read(bt)) != -1) {
byte[] decrypt = decrypt(bt);
Log.e("length", "decrypt ======" + decrypt.length);
outputStream.write(decrypt);
outputStream.flush();
}
return writeFile;
}catch (IOException e) {
e.printStackTrace();
return null;
} finally {
try {
if(outputStream != null)
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (inputStream != null)
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 加密
public static byte[] encrypt(byte[] bytes) {
try {
Cipher cipher = Cipher.getInstance(CipherMode);
byte[] raw = sKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());// 使用CBC模式,需要一个向量iv,可增加加密算法的强度
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(bytes);
return encrypted;
} catch (Exception ex) {
Log.e("AES", "加密异常:" + ex.getMessage());
return null;
}
}
// 解密
public static byte[] decrypt(byte[] bytes) {
try {
byte[] raw = sKey.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance(CipherMode);
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
//byte[] encrypted1 = new BASE64Decoder().decodeBuffer(new String(bytes, "utf-8"));// 先用base64解密
byte[] original = cipher.doFinal(bytes);
return original;
} catch (Exception ex) {
Log.e("AES", "解密异常:" + ex.getMessage());
return null;
}
}
}
No Leanote account? Sign up now.