package com.zhao;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.security.AlgorithmParameters;
import java.util.Base64;
import java.util.Base64.Decoder;
import java.util.Base64.Encoder;
import java.util.UUID;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.io.FileUtils;
/**
* @author Yongqian.Zhao
*
*File encryption and decryption
*/
public class AESFileUtil {
/**
* AES type
*/
private static final String AES_TYPE = "AES/CBC/PKCS5Padding";
/**
* key spec
*/
private static final String AES = "AES";
/**
* key
*/
private static final String KEY = "ACADEMYDKACADEMY";
/**
* Encryption method
*
* @param data
* @return
*/
public static String enCrypt(byte[] data) {
try {
Cipher cipher = Cipher.getInstance(AES_TYPE);
byte[] bytes = new byte[data.length];
System.arraycopy(data, 0, bytes, 0, data.length);
SecretKeySpec keyspec = new SecretKeySpec(KEY.getBytes(), AES);
AlgorithmParameters params = cipher.getParameters();
byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
IvParameterSpec ivspec = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
byte[] enBytes = cipher.doFinal(bytes);
Encoder encoder = Base64.getEncoder();
return encoder.encodeToString(enBytes);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Decryption method
*
* @param data
* @return String
*/
public static String desCrypt(String data) {
try {
Decoder decoder = Base64.getDecoder();
byte[] bytes = decoder.decode(data);
Cipher cipher = Cipher.getInstance(AES_TYPE);
SecretKeySpec keyspec = new SecretKeySpec(KEY.getBytes(), AES);
AlgorithmParameters params = cipher.getParameters();
byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
IvParameterSpec ivspec = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
return new String(cipher.doFinal(bytes));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Save files with encryption and decryption
*
* @param file
* @param type
* @param flag
* @return File
*/
public static File enDescrySaveAsFile(File file, String type ,String flag) {
File localFile = null;
try {
String fileName = new StringBuffer(UUID.randomUUID().toString()).append(type).toString();
localFile = new File(fileName);
byte[] fileByte = FileUtils.readFileToByteArray(file);
String fileText = "";
if ("encryption".equalsIgnoreCase(flag)) {
fileText = enCrypt(fileByte);
}
else if("decryption".equalsIgnoreCase(flag)) {
fileText = desCrypt(new String(fileByte));
}
FileUtils.writeStringToFile(localFile, fileText, StandardCharsets.UTF_8);
return localFile;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (file.exists()) {
file.delete();
}
}
return localFile;
}
public static void main(String[] args) throws Exception {
// File file = new File("./sample.pdf");
// File saveAsFile = enDescrySaveAsFile(file, ".pdf", "encryption");
File file = new File("./688f15f8-6b04-4d50-8a8b-7a59b8b818f3.pdf");
File saveAsFile = enDescrySaveAsFile(file, ".pdf", "decryption");
System.out.println(saveAsFile);
}
}