使用AES對文件的加解密

使用AES對文件的加解密:
Java代碼 複製代碼 收藏代碼
  1. import java.io.File; 
  2. import java.io.FileInputStream; 
  3. import java.io.FileOutputStream;
  4. import java.io.IOException; 
  5. import java.security.Key; 
  6. import java.security.SecureRandom; 
  7.  
  8. import javax.crypto.Cipher; 
  9. import javax.crypto.KeyGenerator; 
  10. import javax.crypto.SecretKey; 
  11. import javax.crypto.spec.SecretKeySpec; 
  12.  
  13. /**
  14. * 使用AES對文件進行加密和解密
  15. *
  16. */ 
  17. public class CipherUtil { 
  18.     /**
  19.      * 使用AES對文件進行加密和解密
  20.      *
  21.      */ 
  22.     private static String type = "AES"
  23.  
  24.     /**
  25.      * 把文件srcFile加密後存儲爲destFile
  26.      * @param srcFile     加密前的文件
  27.      * @param destFile    加密後的文件
  28.      * @param privateKey  密鑰
  29.      * @throws GeneralSecurityException
  30.      * @throws IOException
  31.      */ 
  32.     public void encrypt(String srcFile, String destFile, String privateKey) throws GeneralSecurityException, IOException { 
  33.         Key key = getKey(privateKey); 
  34.         Cipher cipher = Cipher.getInstance(type + "/ECB/PKCS5Padding"); 
  35.         cipher.init(Cipher.ENCRYPT_MODE, key); 
  36.  
  37.         FileInputStream fis = null
  38.         FileOutputStream fos = null
  39.         try
  40.             fis = new FileInputStream(srcFile); 
  41.             fos = new FileOutputStream(mkdirFiles(destFile)); 
  42.  
  43.             crypt(fis, fos, cipher); 
  44.         } catch (FileNotFoundException e) { 
  45.             e.printStackTrace(); 
  46.         } catch (IOException e) { 
  47.             e.printStackTrace(); 
  48.         } finally
  49.             if (fis != null) { 
  50.                 fis.close(); 
  51.             } 
  52.             if (fos != null) { 
  53.                 fos.close(); 
  54.             } 
  55.         } 
  56.     } 
  57.  
  58.     /**
  59.      * 把文件srcFile解密後存儲爲destFile
  60.      * @param srcFile     解密前的文件
  61.      * @param destFile    解密後的文件
  62.      * @param privateKey  密鑰
  63.      * @throws GeneralSecurityException
  64.      * @throws IOException
  65.      */ 
  66.     public void decrypt(String srcFile, String destFile, String privateKey) throws GeneralSecurityException, IOException { 
  67.         Key key = getKey(privateKey); 
  68.         Cipher cipher = Cipher.getInstance(type + "/ECB/PKCS5Padding"); 
  69.         cipher.init(Cipher.DECRYPT_MODE, key); 
  70.  
  71.         FileInputStream fis = null
  72.         FileOutputStream fos = null
  73.         try
  74.             fis = new FileInputStream(srcFile); 
  75.             fos = new FileOutputStream(mkdirFiles(destFile)); 
  76.  
  77.             crypt(fis, fos, cipher); 
  78.         } catch (FileNotFoundException e) { 
  79.             e.printStackTrace(); 
  80.         } catch (IOException e) { 
  81.             e.printStackTrace(); 
  82.         } finally
  83.             if (fis != null) { 
  84.                 fis.close(); 
  85.             } 
  86.             if (fos != null) { 
  87.                 fos.close(); 
  88.             } 
  89.         } 
  90.     } 
  91.  
  92.     /**
  93.      * 根據filePath創建相應的目錄
  94.      * @param filePath      要創建的文件路經
  95.      * @return  file        文件
  96.      * @throws IOException
  97.      */ 
  98.     private File mkdirFiles(String filePath) throws IOException { 
  99.         File file = new File(filePath); 
  100.         if (!file.getParentFile().exists()) { 
  101.             file.getParentFile().mkdirs(); 
  102.         } 
  103.         file.createNewFile(); 
  104.  
  105.         return file; 
  106.     } 
  107.  
  108.     /**
  109.      * 生成指定字符串的密鑰
  110.      * @param secret        要生成密鑰的字符串
  111.      * @return secretKey    生成後的密鑰
  112.      * @throws GeneralSecurityException
  113.      */ 
  114.     private static Key getKey(String secret) throws GeneralSecurityException { 
  115.         KeyGenerator kgen = KeyGenerator.getInstance(type); 
  116.         kgen.init(128, new SecureRandom(secret.getBytes())); 
  117.         SecretKey secretKey = kgen.generateKey(); 
  118.         return secretKey; 
  119.     } 
  120.  
  121.     /**
  122.      * 加密解密流
  123.      * @param in        加密解密前的流
  124.      * @param out       加密解密後的流
  125.      * @param cipher    加密解密
  126.      * @throws IOException
  127.      * @throws GeneralSecurityException
  128.      */ 
  129.     private static void crypt(InputStream in, OutputStream out, Cipher cipher) throws IOException, GeneralSecurityException { 
  130.         int blockSize = cipher.getBlockSize() * 1000
  131.         int outputSize = cipher.getOutputSize(blockSize); 
  132.  
  133.         byte[] inBytes = new byte[blockSize]; 
  134.         byte[] outBytes = new byte[outputSize]; 
  135.  
  136.         int inLength = 0
  137.         boolean more = true
  138.         while (more) { 
  139.             inLength = in.read(inBytes); 
  140.             if (inLength == blockSize) { 
  141.                 int outLength = cipher.update(inBytes, 0, blockSize, outBytes); 
  142.                 out.write(outBytes, 0, outLength); 
  143.             } else
  144.                 more = false
  145.             } 
  146.         } 
  147.         if (inLength > 0
  148.             outBytes = cipher.doFinal(inBytes, 0, inLength); 
  149.         else 
  150.             outBytes = cipher.doFinal(); 
  151.         out.write(outBytes); 
  152.     } 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章