import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.spec.SecretKeySpec;
public class AES {
public static byte[] defPassword;
private static final int CACHE_SIZE = 1024;
public static void init(){
byte[] key = { 70,117,99,107,95,115,69,120,121,45,97,76,108,33,80,119};
defPassword = key;
}
//解密文件//
public static void decryptFile(byte[] paramArrayOfByte, String sourceFilePath, String destFilePath) throws Exception {
File sourceFile = new File(sourceFilePath);
File destFile = new File(destFilePath);
if (sourceFile.exists() && sourceFile.isFile()) {
if (!destFile.getParentFile().exists()) {
destFile.getParentFile().mkdirs();
}
destFile.createNewFile();
FileInputStream in = new FileInputStream(sourceFile);
FileOutputStream out = new FileOutputStream(destFile);
//获取密钥//
init();
byte[] arrayOfByte = defPassword;
SecretKeySpec secretKeySpec = new SecretKeySpec(defPassword, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
CipherOutputStream cout = new CipherOutputStream(out, cipher);
byte[] cache = new byte[CACHE_SIZE];
int nRead = 0;
while ((nRead = in.read(cache)) != -1) {
cout.write(cache, 0, nRead);
cout.flush();
}
cout.close();
out.close();
in.close();
}
}
public static void main(String args[]) throws Exception {
decryptFile(defPassword,"D://xxxx","D://xxxx");
}
}
java AES加密解密
最新推荐文章于 2024-10-22 14:12:33 发布