Java AES文件和文本加解密
来源:互联网 发布:中科院在职研究生知乎 编辑:程序博客网 时间:2024/04/24 14:16
在项目开发中,有时会遇到对文件进行加密、或者文本进行加密,目前加密方式最好的还是AES方式,AES和DES基本相同,只是位数不同而已,AES具体是怎么加密大家可以自行百度搜搜即可
部分代码:
import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.security.Key;import java.security.SecureRandom;import javax.crypto.Cipher;import javax.crypto.CipherInputStream;import javax.crypto.CipherOutputStream;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;import javax.crypto.spec.SecretKeySpec;/** * <p> * AES加密解密工具包 * </p> * * @author IceWee * @date 2012-5-18 * @version 1.0 */public class AESUtils { private static final String ALGORITHM = "AES"; private static final int KEY_SIZE = 128; private static final int CACHE_SIZE = 1024; /** * <p> * 生成随机密钥 * </p> * * @return * @throws Exception */ public static String getSecretKey() throws Exception { return getSecretKey(null); } /** * <p> * 生成密钥 * </p> * * @param seed 密钥种子 * @return * @throws Exception */ public static String getSecretKey(String seed) throws Exception { KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM); SecureRandom secureRandom; if (seed != null && !"".equals(seed)) { secureRandom = new SecureRandom(seed.getBytes()); } else { secureRandom = new SecureRandom(); } keyGenerator.init(KEY_SIZE, secureRandom); SecretKey secretKey = keyGenerator.generateKey(); return Base64Utils.encode(secretKey.getEncoded()); } /** * <p> * 加密 * </p> * * @param data * @param key * @return * @throws Exception */ public static byte[] encrypt(byte[] data, String key) throws Exception { Key k = toKey(Base64Utils.decode(key)); byte[] raw = k.getEncoded(); SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); return cipher.doFinal(data); } /** * <p> * 文件加密 * </p> * * @param key * @param sourceFilePath * @param destFilePath * @throws Exception */ public static void encryptFile(String key, String sourceFilePath, String destFilePath) throws Exception { File sourceFile = new File(sourceFilePath); File destFile = new File(destFilePath); if (sourceFile.exists() && sourceFile.isFile()) { if (!destFile.getParentFile().exists()) { destFile.getParentFile().mkdirs(); } destFile.createNewFile(); InputStream in = new FileInputStream(sourceFile); OutputStream out = new FileOutputStream(destFile); Key k = toKey(Base64Utils.decode(key)); byte[] raw = k.getEncoded(); SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); CipherInputStream cin = new CipherInputStream(in, cipher); byte[] cache = new byte[CACHE_SIZE]; int nRead = 0; while ((nRead = cin.read(cache)) != -1) { out.write(cache, 0, nRead); out.flush(); } out.close(); cin.close(); in.close(); } } /** * <p> * 解密 * </p> * * @param data * @param key * @return * @throws Exception */ public static byte[] decrypt(byte[] data, String key) throws Exception { Key k = toKey(Base64Utils.decode(key)); byte[] raw = k.getEncoded(); SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); return cipher.doFinal(data); } /** * <p> * 文件解密 * </p> * * @param key * @param sourceFilePath * @param destFilePath * @throws Exception */ public static void decryptFile(String key, String sourceFilePath, String destFilePath) throws Exception { File sourceFile = new File(sourceFilePath); File destFile = new File(destFilePath); if (sourceFile.exists() && sourceFile.isFile()) { if (!destFile.getParentFile().exists()) { destFile.getParentFile().mkdirs(); } destFile.createNewFile(); FileInputStream in = new FileInputStream(sourceFile); FileOutputStream out = new FileOutputStream(destFile); Key k = toKey(Base64Utils.decode(key)); byte[] raw = k.getEncoded(); SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); CipherOutputStream cout = new CipherOutputStream(out, cipher); byte[] cache = new byte[CACHE_SIZE]; int nRead = 0; while ((nRead = in.read(cache)) != -1) { cout.write(cache, 0, nRead); cout.flush(); } cout.close(); out.close(); in.close(); } } /** * <p> * 转换密钥 * </p> * * @param key * @return * @throws Exception */ private static Key toKey(byte[] key) throws Exception { SecretKey secretKey = new SecretKeySpec(key, ALGORITHM); return secretKey; } }
下载在源码包
阅读全文
0 0
- Java AES文件和文本加解密
- Java AES文件加解密
- 文件AES加解密
- AES加解密(java)
- java AES 加解密代码
- java AES对称加解密
- Java使用AES加解密
- Java实现AES加解密
- java实现AES加解密
- java调用AES算法产生密钥并加解密文件
- java写的AES文件加解密模块
- 使用AES对文件的加解密
- Java实现的AES加解密程序
- VC与JAVA互通AES加解密
- 用java实现AES加解密算法
- Java 加解密技术系列之 AES
- Delphi-JAVA互加解密AES算法
- java,php,js;AES 互通加解密
- 剑指offer—序列化二叉树
- 将视频监控带入数字时代
- eclipse birt报表预览出现Unhandled event loop exception java.lang.StackOverflowError异常
- 复制一组字符串中的后n个到另一组字符串中
- MySQL 性能优化的最佳 20+ 条经验
- Java AES文件和文本加解密
- AI一分钟 | 用AI拍色情片?神奇女侠的脸被移植到AV女优身上;内江出租车引进人脸识别技术,在全黑情况下可拍出80%影像
- IIC和SPI通信
- Android控件设置篇
- 超实用总结:AI实践者需要用到的10个深度学习方法
- 上海走美杯报名时间、流程及费用
- CAD输出图至Word
- JS移动端浏览器取消右划后退的几种方法
- @XmlAccessorType使用