java aes文件加密_java实现AES加密解密--文件加解密

packagedemo.security;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.InputStream;importjava.io.OutputStream;importjava.security.Key;importjava.security.SecureRandom;importjavax.crypto.Cipher;importjavax.crypto.CipherInputStream;importjavax.crypto.CipherOutputStream;importjavax.crypto.KeyGenerator;importjavax.crypto.SecretKey;importjavax.crypto.spec.SecretKeySpec;/***

* AES加密解密工具包

*

*

*@authorIceWee

* @date 2012-5-18

*@version1.0*/

public classAESUtils {private static final String ALGORITHM = "AES";private static final int KEY_SIZE = 128;private static final int CACHE_SIZE = 1024;/***

* 生成随机密钥

*

*

*@return*@throwsException*/

public static String getSecretKey() throwsException {return getSecretKey(null);

}/***

* 生成密钥

*

*

*@paramseed 密钥种子

*@return*@throwsException*/

public static String getSecretKey(String seed) throwsException {

KeyGenerator keyGenerator=KeyGenerator.getInstance(ALGORITHM);

SecureRandom secureRandom;if (seed != null && !"".equals(seed)) {

secureRandom= newSecureRandom(seed.getBytes());

}else{

secureRandom= newSecureRandom();

}

keyGenerator.init(KEY_SIZE, secureRandom);

SecretKey secretKey=keyGenerator.generateKey();returnBase64Utils.encode(secretKey.getEncoded());

}/***

* 加密

*

*

*@paramdata

*@paramkey

*@return*@throwsException*/

public static byte[] encrypt(byte[] data, String key) throwsException {

Key k=toKey(Base64Utils.decode(key));byte[] raw =k.getEncoded();

SecretKeySpec secretKeySpec= newSecretKeySpec(raw, ALGORITHM);

Cipher cipher=Cipher.getInstance(ALGORITHM);

cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);returncipher.doFinal(data);

}/***

* 文件加密

*

*

*@paramkey

*@paramsourceFilePath

*@paramdestFilePath

*@throwsException*/

public static void encryptFile(String key, String sourceFilePath, String destFilePath) throwsException {

File sourceFile= newFile(sourceFilePath);

File destFile= newFile(destFilePath);if (sourceFile.exists() &&sourceFile.isFile()) {if (!destFile.getParentFile().exists()) {

destFile.getParentFile().mkdirs();

}

destFile.createNewFile();

InputStream in= newFileInputStream(sourceFile);

OutputStream out= newFileOutputStream(destFile);

Key k=toKey(Base64Utils.decode(key));byte[] raw =k.getEncoded();

SecretKeySpec secretKeySpec= newSecretKeySpec(raw, ALGORITHM);

Cipher cipher=Cipher.getInstance(ALGORITHM);

cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);

CipherInputStream cin= newCipherInputStream(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();

}

}/***

* 解密

*

*

*@paramdata

*@paramkey

*@return*@throwsException*/

public static byte[] decrypt(byte[] data, String key) throwsException {

Key k=toKey(Base64Utils.decode(key));byte[] raw =k.getEncoded();

SecretKeySpec secretKeySpec= newSecretKeySpec(raw, ALGORITHM);

Cipher cipher=Cipher.getInstance(ALGORITHM);

cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);returncipher.doFinal(data);

}/***

* 文件解密

*

*

*@paramkey

*@paramsourceFilePath

*@paramdestFilePath

*@throwsException*/

public static void decryptFile(String key, String sourceFilePath, String destFilePath) throwsException {

File sourceFile= newFile(sourceFilePath);

File destFile= newFile(destFilePath);if (sourceFile.exists() &&sourceFile.isFile()) {if (!destFile.getParentFile().exists()) {

destFile.getParentFile().mkdirs();

}

destFile.createNewFile();

FileInputStream in= newFileInputStream(sourceFile);

FileOutputStream out= newFileOutputStream(destFile);

Key k=toKey(Base64Utils.decode(key));byte[] raw =k.getEncoded();

SecretKeySpec secretKeySpec= newSecretKeySpec(raw, ALGORITHM);

Cipher cipher=Cipher.getInstance(ALGORITHM);

cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);

CipherOutputStream cout= newCipherOutputStream(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();

}

}/***

* 转换密钥

*

*

*@paramkey

*@return*@throwsException*/

private static Key toKey(byte[] key) throwsException {

SecretKey secretKey= newSecretKeySpec(key, ALGORITHM);returnsecretKey;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值