使用AES对文件的加解密
来源:互联网 发布:淘宝卖欢乐豆软件 编辑:程序博客网 时间:2024/05/16 23:51
使用AES对文件的加解密:
- 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对文件进行加密和解密
- *
- */
- publicclass CipherUtil {
- /**
- * 使用AES对文件进行加密和解密
- *
- */
- privatestatic String type ="AES";
- /**
- * 把文件srcFile加密后存储为destFile
- * @param srcFile 加密前的文件
- * @param destFile 加密后的文件
- * @param privateKey 密钥
- * @throws GeneralSecurityException
- * @throws IOException
- */
- publicvoid 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
- */
- publicvoid 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
- */
- privatestatic 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
- */
- privatestaticvoid crypt(InputStream in, OutputStream out, Cipher cipher)throws IOException, GeneralSecurityException {
- int blockSize = cipher.getBlockSize() *1000;
- int outputSize = cipher.getOutputSize(blockSize);
- byte[] inBytes =newbyte[blockSize];
- byte[] outBytes =newbyte[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);
- }
- }
- 使用AES对文件的加解密
- 文件AES加解密
- 使用OpenSSL库的AES加解密
- AES 对字符串 加解密
- 使用 openssl AES RSA 对文件以及数据进行加解密
- 使用 openssl AES RSA 对文件以及数据进行加解密
- 【AES】使用OpenSSL库的AES加解密
- Java AES文件加解密
- 对称加密算法AES------使用AES算法对文件进行加密/解密的操作(JAVA)
- Java使用AES加解密
- Delphi关于改进AES算法的加解密文件功能
- java写的AES文件加解密模块
- iOS中使用RNCryptor对文件进行AES加密/解密
- 使用对称加密aes对文件进行zip加密解密
- Java AES文件和文本加解密
- 使用AES算法对文件进行加密/解密的操作(JAVA)
- 使用Crypto++实现AES加解密
- OpenSSL之AES加解密API使用
- iframe防止嵌套
- IPHONE开发 @synthesize 变量下划线
- Poj 1015
- JSP地址栏传递中文字符乱码解决
- 自旋锁(spinlock) 解释得经典,透彻
- 使用AES对文件的加解密
- 未与信任SQL Server连接相关联的解决方法
- Linux内核访问外设I/O--动态映射(ioremap)和静态映射(map_desc)
- [C#]字符串比较--显示相同[In VS],比较结果却不同
- python学习
- 算法练习之水题
- 此上下文中不允许异步操作。启动异步操作的页必须将 Async 属性设置为 true,并且异步操作只能在 PreRenderComplete 事件之前的页上启动。
- android 实现zip文件的解压
- 数据库的一点基础笔记