首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

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

2012-09-21 
使用AES算法对文件进行加密/解密的操作(JAVA)很简单,直接上代码。/*** AES方式解密文件* @param sourceFile

使用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;    }


热点排行