使用AES对文件的加解密
2013-11-19 10:47
435 查看
使用AES对文件的加解密:
Java代码
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/**
* 使用AES对文件进行加密和解密
*
*/
public
class CipherUtil {
/**
* 使用AES对文件进行加密和解密
*
*/
private
static String type =
"AES";
/**
* 把文件srcFile加密后存储为destFile
* @param srcFile 加密前的文件
* @param destFile 加密后的文件
* @param privateKey 密钥
* @throws GeneralSecurityException
* @throws IOException
*/
public
void encrypt(String srcFile, String destFile, String privateKey)
throws GeneralSecurityException, IOException {
Key key = getKey(privateKey);
Cipher cipher = Cipher.getInstance(type +
"/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(mkdirFiles(destFile));
crypt(fis, fos, cipher);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis !=
null) {
fis.close();
}
if (fos !=
null) {
fos.close();
}
}
}
/**
* 把文件srcFile解密后存储为destFile
* @param srcFile 解密前的文件
* @param destFile 解密后的文件
* @param privateKey 密钥
* @throws GeneralSecurityException
* @throws IOException
*/
public
void decrypt(String srcFile, String destFile, String privateKey)
throws GeneralSecurityException, IOException {
Key key = getKey(privateKey);
Cipher cipher = Cipher.getInstance(type +
"/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(mkdirFiles(destFile));
crypt(fis, fos, cipher);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis !=
null) {
fis.close();
}
if (fos !=
null) {
fos.close();
}
}
}
/**
* 根据filePath创建相应的目录
* @param filePath 要创建的文件路经
* @return file 文件
* @throws IOException
*/
private File mkdirFiles(String filePath)
throws IOException {
File file = new File(filePath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
file.createNewFile();
return file;
}
/**
* 生成指定字符串的密钥
* @param secret 要生成密钥的字符串
* @return secretKey 生成后的密钥
* @throws GeneralSecurityException
*/
private
static Key getKey(String secret)
throws GeneralSecurityException {
KeyGenerator kgen = KeyGenerator.getInstance(type);
kgen.init(128, new SecureRandom(secret.getBytes()));
SecretKey secretKey = kgen.generateKey();
return secretKey;
}
/**
* 加密解密流
* @param in 加密解密前的流
* @param out 加密解密后的流
* @param cipher 加密解密
* @throws IOException
* @throws GeneralSecurityException
*/
private
static
void crypt(InputStream in, OutputStream out, Cipher cipher)
throws IOException, GeneralSecurityException {
int blockSize = cipher.getBlockSize() *
1000;
int outputSize = cipher.getOutputSize(blockSize);
byte[] inBytes =
new
byte[blockSize];
byte[] outBytes =
new
byte[outputSize];
int inLength =
0;
boolean more =
true;
while (more) {
inLength = in.read(inBytes);
if (inLength == blockSize) {
int outLength = cipher.update(inBytes,
0, blockSize, outBytes);
out.write(outBytes, 0, outLength);
} else {
more = false;
}
}
if (inLength >
0)
outBytes = cipher.doFinal(inBytes, 0, inLength);
else
outBytes = cipher.doFinal();
out.write(outBytes);
}
}
Java代码
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/**
* 使用AES对文件进行加密和解密
*
*/
public
class CipherUtil {
/**
* 使用AES对文件进行加密和解密
*
*/
private
static String type =
"AES";
/**
* 把文件srcFile加密后存储为destFile
* @param srcFile 加密前的文件
* @param destFile 加密后的文件
* @param privateKey 密钥
* @throws GeneralSecurityException
* @throws IOException
*/
public
void encrypt(String srcFile, String destFile, String privateKey)
throws GeneralSecurityException, IOException {
Key key = getKey(privateKey);
Cipher cipher = Cipher.getInstance(type +
"/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(mkdirFiles(destFile));
crypt(fis, fos, cipher);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis !=
null) {
fis.close();
}
if (fos !=
null) {
fos.close();
}
}
}
/**
* 把文件srcFile解密后存储为destFile
* @param srcFile 解密前的文件
* @param destFile 解密后的文件
* @param privateKey 密钥
* @throws GeneralSecurityException
* @throws IOException
*/
public
void decrypt(String srcFile, String destFile, String privateKey)
throws GeneralSecurityException, IOException {
Key key = getKey(privateKey);
Cipher cipher = Cipher.getInstance(type +
"/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(mkdirFiles(destFile));
crypt(fis, fos, cipher);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis !=
null) {
fis.close();
}
if (fos !=
null) {
fos.close();
}
}
}
/**
* 根据filePath创建相应的目录
* @param filePath 要创建的文件路经
* @return file 文件
* @throws IOException
*/
private File mkdirFiles(String filePath)
throws IOException {
File file = new File(filePath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
file.createNewFile();
return file;
}
/**
* 生成指定字符串的密钥
* @param secret 要生成密钥的字符串
* @return secretKey 生成后的密钥
* @throws GeneralSecurityException
*/
private
static Key getKey(String secret)
throws GeneralSecurityException {
KeyGenerator kgen = KeyGenerator.getInstance(type);
kgen.init(128, new SecureRandom(secret.getBytes()));
SecretKey secretKey = kgen.generateKey();
return secretKey;
}
/**
* 加密解密流
* @param in 加密解密前的流
* @param out 加密解密后的流
* @param cipher 加密解密
* @throws IOException
* @throws GeneralSecurityException
*/
private
static
void crypt(InputStream in, OutputStream out, Cipher cipher)
throws IOException, GeneralSecurityException {
int blockSize = cipher.getBlockSize() *
1000;
int outputSize = cipher.getOutputSize(blockSize);
byte[] inBytes =
new
byte[blockSize];
byte[] outBytes =
new
byte[outputSize];
int inLength =
0;
boolean more =
true;
while (more) {
inLength = in.read(inBytes);
if (inLength == blockSize) {
int outLength = cipher.update(inBytes,
0, blockSize, outBytes);
out.write(outBytes, 0, outLength);
} else {
more = false;
}
}
if (inLength >
0)
outBytes = cipher.doFinal(inBytes, 0, inLength);
else
outBytes = cipher.doFinal();
out.write(outBytes);
}
}
相关文章推荐
- 终于把你必须知道的.NET看完了
- 如何设置gen_server在退出时执行相关操作
- Poj 1015
- 《文字与段落——使用HTML标记完成页面排版》
- 面向对象编程(OOP)
- Android 滑动切换页面-ViewFlipper
- python学习
- AS3 Graphics 多次绘制
- HDOJ 2222 Keywords Search
- python 解析 crontab配置
- Android异步消息处理机制完全解析,带你从源码的角度彻底理解
- iframe防止嵌套
- text rendering
- mac系统如何显示和隐藏文件
- mysql和eclipse都设置了utf8编码,保存时中文依然乱码
- AS3 读写 C++ 64位数字
- 面试中如何运用实践经验
- 分享一篇关于iOS7力学的文章
- 很2的SQL问题
- 0. R中有用的包总结