import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* AES加密模块
* 假设源文件为"G:/test/test.txt",调用加密模块方法为:AES.encryptFile(密钥,"G:/test/test.txt",目标路径);
* 解密同上,只是方法不同
* @author shuaicenglou
*
*/
public class AES {
public static final String VIPARA = "0102030405060708";
private static final String TAG = AES.class.getSimpleName();
/**
* 初始化 AES Cipher
*
* @param sKey
* @param cipherMode
* @return
*/
private static Cipher initAESCipher(String sKey, int cipherMode) {
//创建Key gen
KeyGenerator keyGenerator = null;
Cipher cipher = null;
try {
IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes());
SecretKeySpec key = new SecretKeySpec(sKey.getBytes(), "AES");
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(cipherMode, key, zeroIv);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (NoSuchPaddingException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (InvalidKeyException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
return cipher;
}
/**
* 对文件进行AES加密
*
* @param key 密钥
* @param sourceFilePath 需要加密的文件路径
* @param destFilePath 加密之后的文件存储路径
* @return
*/
public static void encryptFile(String key, String sourceFilePath, String destFilePath) {
System.out.printf(sourceFilePath);
FileInputStream in = null;
FileOutputStream out = null;
File destFile = null;
File sourceFile = null;
try {
sourceFile = new File(sourceFilePath);
System.out.printf( sourceFilePath + "---" + sourceFile.getAbsolutePath());
destFile = new File(destFilePath);
if (sourceFile.exists() && sourceFile.isFile()) {
if (!destFile.getParentFile().exists()) {
destFile.getParentFile().mkdirs();
}
destFile.createNewFile();
in = new FileInputStream(sourceFile);
out = new FileOutputStream(destFile);
Cipher cipher = initAESCipher(key, Cipher.ENCRYPT_MODE);
//以加密流写入文件
CipherInputStream cipherInputStream = new CipherInputStream(in, cipher);
byte[] cache = new byte[1024];
int nRead = 0;
while ((nRead = cipherInputStream.read(cache)) != -1) {
out.write(cache, 0, nRead);
out.flush();
}
cipherInputStream.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
try {
in.close();
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
/**
* AES方式解密文件
*
* @param key
* @param sourceFilePath
* @param destFilePath
* @return
*/
public static void decryptFile(String key, String sourceFilePath, String destFilePath) {
FileInputStream in = null;
FileOutputStream out = null;
File destFile = null;
File sourceFile = null;
try {
sourceFile = new File(sourceFilePath);
destFile = new File(destFilePath);
if (sourceFile.exists() && sourceFile.isFile()) {
if (!destFile.getParentFile().exists()) {
destFile.getParentFile().mkdirs();
}
destFile.createNewFile();
in = new FileInputStream(sourceFile);
out = new FileOutputStream(destFile);
Cipher cipher = initAESCipher(key, Cipher.DECRYPT_MODE);
CipherOutputStream cipherOutputStream = new CipherOutputStream(out, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = in.read(buffer)) >= 0) {
cipherOutputStream.write(buffer, 0, r);
}
cipherOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
}
附一个命令行版本的可执行jar:AES文件加密
2019.09.22更新:
加解密字符串已完善,通过将得到的byte[]转为16进制字符串实现
中英文均可加解密。
另实现图形界面的加解密程序,效果如下: