使用AES算法对文件进行加密/解密的操作(JAVA)
来源:互联网 发布:阿里云 代码托管 编辑:程序博客网 时间:2024/06/11 12:42
很简单,直接上代码。
/** * 初始化 AES Cipher * @param sKey * @param cipherMode * @return */ public Cipher initAESCipher(String sKey, int cipherMode) { //创建Key gen KeyGenerator keyGenerator = null; Cipher cipher = null; try { keyGenerator = KeyGenerator.getInstance("AES"); keyGenerator.init(128, new SecureRandom(sKey.getBytes())); SecretKey secretKey = keyGenerator.generateKey(); byte[] codeFormat = secretKey.getEncoded(); SecretKeySpec key = new SecretKeySpec(codeFormat, "AES"); cipher = Cipher.getInstance("AES"); //初始化 cipher.init(cipherMode, key); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (NoSuchPaddingException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (InvalidKeyException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } return cipher; }
/** * 对文件进行AES加密 * @param sourceFile * @param fileType * @param sKey * @return */ public File encryptFile(File sourceFile,String fileType, String sKey){ //新建临时加密文件 File encrypfile = null; InputStream inputStream = null; OutputStream outputStream = null; try { inputStream = new FileInputStream(sourceFile); encrypfile = File.createTempFile(sourceFile.getName(), fileType); outputStream = new FileOutputStream(encrypfile); Cipher cipher = initAESCipher(sKey,Cipher.ENCRYPT_MODE); //以加密流写入文件 CipherInputStream cipherInputStream = new CipherInputStream(inputStream, cipher); byte[] cache = new byte[1024]; int nRead = 0; while ((nRead = cipherInputStream.read(cache)) != -1) { outputStream.write(cache, 0, nRead); outputStream.flush(); } cipherInputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } 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 encrypfile; }
/** * 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; }
注意:对文件的读写操作一定要指定编码,否则会导致很多未知的编码问题。
- 对称加密算法AES------使用AES算法对文件进行加密/解密的操作(JAVA)
- 使用AES算法对文件进行加密/解密的操作(JAVA)
- 使用对称加密aes对文件进行zip加密解密
- iOS中使用RNCryptor对文件进行AES加密/解密
- java对文件进行加密解密操作
- 使用AES算法完成对数据的加密和解密
- 使用java进行 AES 加密 解密
- AES加密解密算法的Java实现
- AES加密解密算法文件
- Java加密解密算法-AES加密解密
- Java AES加密解密算法
- 使用AES对文件的加解密
- 使用CryptoAPI对文件进行加密解密
- 对使用zend加密后的php文件进行解密
- java delphi aes 加密与解密文件兼容算法
- java中使用异或的方式对文件进行加密解密
- Java的AES加密解密
- 使用Python进行AES加密和解密
- Linux内核数据包处理流程-数据包接收(1)
- 解决tomcat启动时隐藏命令行
- C++编程规范 4 类
- C++编程规范 5 作用域、模板和C++其他特性
- ZZ 常用算法经典代码(C++版)
- 使用AES算法对文件进行加密/解密的操作(JAVA)
- C++ 6 资源分配和释放
- 一个是阆苑仙葩,一个是美玉无瑕
- C++ 编程规范 7 异常与错误处理
- C++编程规范 8 标准库
- C++编程规范 9 程序效率
- Linux内核数据包处理流程-数据包接收(2)
- WebApp与Native App有何区别
- android 图片平铺实现