对称加密算法AES------使用AES算法对文件进行加密/解密的操作(JAVA)

文章来源:http://blog.csdn.net/shiwenqing/article/details/7987145


很简单,直接上代码。

  1. /** 
  2.      * 初始化 AES Cipher 
  3.      * @param sKey 
  4.      * @param cipherMode 
  5.      * @return 
  6.      */  
  7.     public Cipher initAESCipher(String sKey, int cipherMode) {  
  8.         //创建Key gen  
  9.         KeyGenerator keyGenerator = null;  
  10.         Cipher cipher = null;  
  11.         try {  
  12.             keyGenerator = KeyGenerator.getInstance("AES");  
  13.             keyGenerator.init(128new SecureRandom(sKey.getBytes()));  
  14.             SecretKey secretKey = keyGenerator.generateKey();  
  15.             byte[] codeFormat = secretKey.getEncoded();  
  16.             SecretKeySpec key = new SecretKeySpec(codeFormat, "AES");  
  17.             cipher = Cipher.getInstance("AES");  
  18.             //初始化  
  19.             cipher.init(cipherMode, key);  
  20.         } catch (NoSuchAlgorithmException e) {  
  21.             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.  
  22.         } catch (NoSuchPaddingException e) {  
  23.             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.  
  24.         } catch (InvalidKeyException e) {  
  25.             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.  
  26.         }  
  27.         return cipher;  
  28.     }  


AES加密文件操作
  1. /** 
  2.      * 对文件进行AES加密 
  3.      * @param sourceFile 
  4.      * @param fileType 
  5.      * @param sKey 
  6.      * @return 
  7.      */  
  8.     public File encryptFile(File sourceFile,String fileType, String sKey){  
  9.         //新建临时加密文件  
  10.         File encrypfile = null;  
  11.         InputStream inputStream = null;  
  12.         OutputStream outputStream = null;  
  13.         try {  
  14.             inputStream = new FileInputStream(sourceFile);  
  15.             encrypfile = File.createTempFile(sourceFile.getName(), fileType);  
  16.             outputStream = new FileOutputStream(encrypfile);  
  17.             Cipher cipher = initAESCipher(sKey,Cipher.ENCRYPT_MODE);  
  18.             //以加密流写入文件  
  19.             CipherInputStream cipherInputStream = new CipherInputStream(inputStream, cipher);  
  20.             byte[] cache = new byte[1024];  
  21.             int nRead = 0;  
  22.             while ((nRead = cipherInputStream.read(cache)) != -1) {  
  23.                 outputStream.write(cache, 0, nRead);  
  24.                 outputStream.flush();  
  25.             }  
  26.             cipherInputStream.close();  
  27.         }  catch (FileNotFoundException e) {  
  28.             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.  
  29.         }  catch (IOException e) {  
  30.             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.  
  31.         } finally {  
  32.             try {  
  33.                 inputStream.close();  
  34.             } catch (IOException e) {  
  35.                 e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.  
  36.             }  
  37.             try {  
  38.                 outputStream.close();  
  39.             } catch (IOException e) {  
  40.                 e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.  
  41.             }  
  42.         }  
  43.         return encrypfile;  
  44.     }  


AES解密文件
  1. /** 
  2.      * AES方式解密文件 
  3.      * @param sourceFile 
  4.      * @return 
  5.      */  
  6.     public File decryptFile(File sourceFile,String fileType,String sKey){  
  7.         File decryptFile = null;  
  8.         InputStream inputStream = null;  
  9.         OutputStream outputStream = null;  
  10.         try {  
  11.             decryptFile = File.createTempFile(sourceFile.getName(),fileType);  
  12.             Cipher cipher = initAESCipher(sKey,Cipher.DECRYPT_MODE);  
  13.             inputStream = new FileInputStream(sourceFile);  
  14.             outputStream = new FileOutputStream(decryptFile);  
  15.             CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, cipher);  
  16.             byte [] buffer = new byte [1024];  
  17.             int r;  
  18.             while ((r = inputStream.read(buffer)) >= 0) {  
  19.                 cipherOutputStream.write(buffer, 0, r);  
  20.             }  
  21.             cipherOutputStream.close();  
  22.         } catch (IOException e) {  
  23.             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.  
  24.         }finally {  
  25.             try {  
  26.                 inputStream.close();  
  27.             } catch (IOException e) {  
  28.                 e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.  
  29.             }  
  30.             try {  
  31.                 outputStream.close();  
  32.             } catch (IOException e) {  
  33.                 e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.  
  34.             }  
  35.         }  
  36.         return decryptFile;  
  37.     }  


注意:对文件的读写操作一定要指定编码,否则会导致很多未知的编码问题。


  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值