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(wri
/**
* 解密
*
* @param sSrc
* @param key
* @param ivs
* @return
*/
public static String decrypt(String sSrc, String key, String ivs) {
try {
byte[] raw = key.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance(CipherMode);
IvParameterSpec iv = new IvParameterSpec(ivs.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);// 先用base64解密
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original, "utf-8");
return originalString;
} catch (Exception ex) {
Log.e("AES", "解密异常:" + ex.getMessage());
return null;
}
}
/**
* 加密
*
* @param encData
* @param secretKey
* @param vector
* @return
*/
public static String Encrypt(String encData, String secretKey, String vector) {
try {
if (secretKey == null)