AES 文件加密和字符串加密 JAVA版
来源:互联网 发布:mac ae破解版安装教程 编辑:程序博客网 时间:2024/05/17 23:24
import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import java.security.InvalidKeyException;import java.security.NoSuchAlgorithmException;import java.security.SecureRandom;import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.CipherInputStream;import javax.crypto.CipherOutputStream;import javax.crypto.IllegalBlockSizeException;import javax.crypto.KeyGenerator;import javax.crypto.NoSuchPaddingException;import javax.crypto.SecretKey;import javax.crypto.spec.SecretKeySpec;/** * * @author Ylca * */public class AES {/** * 加密字符串 * * @param content 需要加密的内容 * @param password 密码 * @return */public static byte[] encrypt(String content, String sKey) {try {// 初始化 加密器Cipher cipher = initAESCipher(sKey, Cipher.ENCRYPT_MODE);byte[] byteContent = content.getBytes("utf-8");byte[] result = cipher.doFinal(byteContent);return result; // 加密} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (IllegalBlockSizeException e) {e.printStackTrace();} catch (BadPaddingException e) {e.printStackTrace();}return null;}/** * 解密字符串 * * @param content * @param password * @return */public static byte[] decrypt(byte[] content, String sKey) {try {// 初始化 加密器Cipher cipher = initAESCipher(sKey, Cipher.DECRYPT_MODE);byte[] result = cipher.doFinal(content);return result; // 加密} catch (IllegalBlockSizeException e) {e.printStackTrace();} catch (BadPaddingException e) {e.printStackTrace();}return null;}/** * 对文件进行AES加密 * * @param sourceFile * @param fileType * @param sKey * @return */public static File encryptFile(File sourceFile, File encrypfile, String sKey) {InputStream inputStream = null;OutputStream outputStream = null;try {inputStream = new FileInputStream(sourceFile);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 要解密的文件路径 * @param decryptFile 解密后的文件路径 * @param sKey * @return */public static File decryptFile(File sourceFile, File decryptFile,String sKey) {InputStream inputStream = null;OutputStream outputStream = null;try {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 Cipher * * @param sKey * @param cipherMode * @return */public static Cipher initAESCipher(String sKey, int cipherMode) {// 创建Key genKeyGenerator 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;}public static void main(String[] args) throws Exception {String cKey = "00b09e37363e596e1f25b23c78e49939238b";//未加密文件路径File oldfile = new File("C:\\Users\\Administrator\\Desktop\\1.mp4");//加密后的文件路径File encrypfile = new File("C:\\Users\\Administrator\\Desktop\\encryp.mp4");//解密后的文件路径File decrypfile = new File("C:\\Users\\Administrator\\Desktop\\decryp.mp4");//加密文件encryptFile(oldfile,encrypfile,cKey);//解密文件decryptFile(encrypfile, decrypfile,cKey);}
阅读全文
0 0
- AES 文件加密和字符串加密 JAVA版
- JAVA AES算法加密字符串
- AES加密-java版
- java版AES文件加密速度问题
- AES 加密,net和java 通用加密
- <转载>JAVA AES DES MD5 加密字符串
- AES文件加密解密
- AES加密文件
- aes加密文件
- AES算法加密文件
- 文件AES加密、解密
- 用AES来加密和解密文件
- AES加密解密(互通.NET和JAVA)
- AES 加密 PHP 和 JAVA 互通
- JAVA实现AES加密和解密
- JAVA中AES加密和解密
- java和js实现aes加密解密
- Java实现AES加密和解密
- 133个Java面试问题列表
- g'f'd's'g'he'r'tu'e'y
- MARKDOWM
- 20170629 上海外滩
- JAVA多线程和并发基础面试问答
- AES 文件加密和字符串加密 JAVA版
- php 设计模式(数据库对象模式)
- macOS+Windows+iOS: 应用程序兼容性列表
- Vungle SDK的崩溃问题
- ListView中adapter加载数据的两种方式
- php 设计模式(注册器模式+工厂模式+数据库映射模式,单例模式+工厂模式+注册器模式混合使用)
- java foreach
- php 设计模式(观察者模式)
- java hashCode 理解