废话不多说 直接撸代码
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
class UtilOfAES {
public static void main(String[] a)throws Exception{
//加密
UtilOfAES.encryptFile("b358335d7e60a77a".getBytes(), "d:/demo.xlsx", "d:/demo.csv", "CBC");
//解密
UtilOfAES.decryptFile("b358335d7e60a77a".getBytes(), "d:/demo.csv", "d:/demo1.xlsx", "CBC");
System.exit(0);
}
private static final String ALGORITHM = "AES";
private static final int KEY_SIZE = 128;
private static final int CACHE_SIZE = 1024;
/**
* <p>
* 生成随机密钥
* </p>
*
* @return
* @throws Exception
*/
public static byte[] getSecretKey() throws Exception {
return getSecretKey(null);
}
/**
* <p>
* 生成密钥(返回 hex_key)
* </p>
*
* @param seed 密钥种子
* @return
* @throws Exception
*/
public static byte[] getSecretKey(String seed) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
SecureRandom secureRandom;
if (seed != null && !"".equals(seed)) {
secureRandom = new SecureRandom(seed.getBytes());
} else {
secureRandom = new SecureRandom();
}
keyGenerator.init(KEY_SIZE, secureRandom);
SecretKey secretKey = keyGenerator.generateKey();
return secretKey.getEncoded();
}
/**
* <p>
* 文件加密
* </p>
*
* @param key
* @param sourceFilePath
* @param destFilePath
* @throws Exception
*/
public static void encryptFile(byte[] key, String sourceFilePath, String destFilePath, String aesMode) 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();
InputStream in = new FileInputStream(sourceFile);
OutputStream out = new FileOutputStream(destFile);
Key k = toKey(key);
byte[] raw = k.getEncoded();
SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM);
Cipher cipher = Cipher.getInstance("AES/"+aesMode.toUpperCase()+"/PKCS5Padding");
if(aesMode.equalsIgnoreCase("CBC")){
//aesMode=='CBC'
IvParameterSpec iv = new IvParameterSpec(raw);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, iv);
}else{
//aesMode=='ECB'
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
}
CipherInputStream cin = new CipherInputStream(in, cipher);
byte[] cache = new byte[CACHE_SIZE];
int nRead = 0;
while ((nRead = cin.read(cache)) != -1) {
out.write(cache, 0, nRead);
out.flush();
}
out.close();
cin.close();
in.close();
}
}
/**
* <p>
* 文件解密
* </p>
*
* @param key
* @param sourceFilePath
* @param destFilePath
* @throws Exception
*/
public static void decryptFile(byte[] key, String sourceFilePath, String destFilePath, String aesMode) 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);
Key k = toKey(key);
byte[] raw = k.getEncoded();
SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM);
Cipher cipher = Cipher.getInstance("AES/"+aesMode.toUpperCase()+"/PKCS5Padding");
if(aesMode.equalsIgnoreCase("CBC")){
//aesMode=='CBC'
// IvParameterSpec iv = new IvParameterSpec(raw);
IvParameterSpec iv = new IvParameterSpec("11YTe56789AwCD0F".getBytes());
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, iv);
}else{
//aesMode=='ECB'
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();
}
}
/**
* <p>
* 转换密钥
* </p>
*
* @param key
* @return
* @throws Exception
*/
private static Key toKey(byte[] key) throws Exception {
if(key==null)
throw new Exception("invalid key");
//不足16位则补0
byte[] mykey = new byte[16];
for(int i=0; i<mykey.length; i++){
mykey[i] = 0;
}
for(int i=0; i<key.length; i++){
if(i>=16)
break;
mykey[i] = key[i];
}
SecretKey secretKey = new SecretKeySpec(mykey, ALGORITHM);
return secretKey;
}
}
实现效果
1、demo.xlsx为加密前的源文件
2、demo.csv为加密后的csv文件
3、demo1.xlsx为解密后的文件