使用AES算法对文件进行加密/解密的操作(JAVA)
很简单,直接上代码。
/** * AES方式解密文件 * @param sourceFile * @return */ public File decryptFile(File sourceFile,String fileType,String sKey){ File decryptFile = null; InputStream inputStream = null; OutputStream outputStream = null; try { decryptFile = File.createTempFile(sourceFile.getName(),fileType); Cipher cipher = initAESCipher(sKey,Cipher.DECRYPT_MODE); inputStream = new FileInputStream(sourceFile); outputStream = new FileOutputStream(decryptFile); CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, cipher); byte [] buffer = new byte [1024]; int r; while ((r = inputStream.read(buffer)) >= 0) { cipherOutputStream.write(buffer, 0, r); } cipherOutputStream.close(); } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. }finally { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } return decryptFile; }