java aes 解密 文件_Java AES文件加解密
转自:http://www.webtag123.com/java/4049.html
AESUtils.java
package demo.security;
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;
/**
*
* AES加密解密工具包
*
*
* @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;
/**
*
* 生成随机密钥
*
*
* @return
* @throws Exception
*/
public static String getSecretKey() throws Exception {
return getSecretKey(null);
}
/**
*
* 生成密钥
*
*
* @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());
}
/**
*
* 加密
*
*
* @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);
}
/**
*
* 文件加密
*
*
* @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();
}
}
/**
*
* 解密
*
*
* @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);
}
/**
*
* 文件解密
*
*
* @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();
}
}
/**
*
* 转换密钥
*
*
* @param key
* @return
* @throws Exception
*/
private static Key toKey(byte[] key) throws Exception {
SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);
return secretKey;
}
}
Base64Utils.java(依赖javabase64-1.3.1.jar)
package demo.security;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import it.sauronsoftware.base64.Base64;
/**
*
* BASE64编码解码工具包
*
*
* 依赖javabase64-1.3.1.jar
*
*
* @author IceWee
* @date 2012-5-19
* @version 1.0
*/
public class Base64Utils {
/**
* 文件读取缓冲区大小
*/
private static final int CACHE_SIZE = 1024;
/**
*
* BASE64字符串解码为二进制数据
*
*
* @param base64
* @return
* @throws Exception
*/
public static byte[] decode(String base64) throws Exception {
return Base64.decode(base64.getBytes());
}
/**
*
* 二进制数据编码为BASE64字符串
*
*
* @param bytes
* @return
* @throws Exception
*/
public static String encode(byte[] bytes) throws Exception {
return new String(Base64.encode(bytes));
}
/**
*
* 将文件编码为BASE64字符串
*
*
* 大文件慎用,可能会导致内存溢出
*
*
* @param filePath 文件绝对路径
* @return
* @throws Exception
*/
public static String encodeFile(String filePath) throws Exception {
byte[] bytes = fileToByte(filePath);
return encode(bytes);
}
/**
*
* BASE64字符串转回文件
*
*
* @param filePath 文件绝对路径
* @param base64 编码字符串
* @throws Exception
*/
public static void decodeToFile(String filePath, String base64) throws Exception {
byte[] bytes = decode(base64);
byteArrayToFile(bytes, filePath);
}
/**
*
* 文件转换为二进制数组
*
*
* @param filePath 文件路径
* @return
* @throws Exception
*/
public static byte[] fileToByte(String filePath) throws Exception {
byte[] data = new byte[0];
File file = new File(filePath);
if (file.exists()) {
FileInputStream in = new FileInputStream(file);
ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
byte[] cache = new byte[CACHE_SIZE];
int nRead = 0;
while ((nRead = in.read(cache)) != -1) {
out.write(cache, 0, nRead);
out.flush();
}
out.close();
in.close();
data = out.toByteArray();
}
return data;
}
/**
*
* 二进制数据写文件
*
*
* @param bytes 二进制数据
* @param filePath 文件生成目录
*/
public static void byteArrayToFile(byte[] bytes, String filePath) throws Exception {
InputStream in = new ByteArrayInputStream(bytes);
File destFile = new File(filePath);
if (!destFile.getParentFile().exists()) {
destFile.getParentFile().mkdirs();
}
destFile.createNewFile();
OutputStream out = new FileOutputStream(destFile);
byte[] cache = new byte[CACHE_SIZE];
int nRead = 0;
while ((nRead = in.read(cache)) != -1) {
out.write(cache, 0, nRead);
out.flush();
}
out.close();
in.close();
}
}
AESTester.java
package demo.security;
public class AESTester {
static String key;
static {
try {
key = AESUtils.getSecretKey();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
long begin = System.currentTimeMillis();
encryptFile();
decryptFile();
test();
long end = System.currentTimeMillis();
System.err.println("耗时:" + (end-begin)/1000 + "秒");
}
static void encryptFile() throws Exception {
String sourceFilePath = "D:/demo.mp4";
String destFilePath = "D:/demo_encrypted.mp4";
AESUtils.encryptFile(key, sourceFilePath, destFilePath);
}
static void decryptFile() throws Exception {
String sourceFilePath = "D:/demo_encrypted.mp4";
String destFilePath = "D:/demo_decrypted.mp4";
AESUtils.decryptFile(key, sourceFilePath, destFilePath);
}
static void test() throws Exception {
String source = "这是一行测试DES加密/解密的文字,你看完也等于没看,是不是啊?!";
System.err.println("原文:\t" + source);
byte[] inputData = source.getBytes();
inputData = AESUtils.encrypt(inputData, key);
System.err.println("加密后:\t" + Base64Utils.encode(inputData));
byte[] outputData = AESUtils.decrypt(inputData, key);
String outputStr = new String(outputData);
System.err.println("解密后:\t" + outputStr);
}
}
java aes 解密 文件_Java AES文件加解密相关推荐
- java调用微信加密_java微信消息加解密
今天心血来潮就信手拈来学了下微信消息加解密的知识,忽然觉得微信真的好强大.可能在大部分项目微信消息的加解密都用不上,但是仍然不排除有使用到的情况,如涉及金钱方面的微信应用包括商城类.金融类还有其他安全 ...
- SM4、AES,RSA,DES等加解密,以及一些其他常用工具方法整理
工作中接触到了SM4,AES,RSA等算法的加解密,这里整理下来,以备后续其他地方需要使用到. 主要用到的第三方包为hutool 后台引入依赖的方式为: <!-- hutool工具包 --> ...
- java 动态读取文件_Java窗体动态加载磁盘文件的实现方法
在使用图形界面操作系统时,当打开一个文件夹系统会自动列出该文件夹下的所有文件及子文件夹.本实例实现了类似的功能:首先让用户选择一个文件夹,程序会动态列出该文件夹下的所有文件:如果该文件是隐藏文件,就在 ...
- java 数字信封_GitHub - zhopen/eos-crypto-java: EOS 公钥加密,私钥解密。基于ECC+AES 实现的双向验证加解密。数字信封的 加解密。...
/** * * sender * * EOS8g1u3ktAGHs4QsVp9aeaWNebFLtprQHwpaSjegx6iEuoTNhjXU * 5KTZYCDdcfNrmEpcf97SJBCtT ...
- java 数字信封_GitHub - yanjunli/eos-crypto-java: EOS 公钥加密,私钥解密。基于ECC+AES 实现的双向验证加解密。数字信封的 加解密。...
/** * * sender * * EOS8g1u3ktAGHs4QsVp9aeaWNebFLtprQHwpaSjegx6iEuoTNhjXU * 5KTZYCDdcfNrmEpcf97SJBCtT ...
- Crypto++入门学习笔记(DES、AES、RSA、SHA-256)(加解密)
转自http://www.cppblog.com/ArthasLee/archive/2010/12/01/135186.html 最近,基于某些原因和需要,笔者需要去了解一下Crypto++库,然后 ...
- java aes 工具类_Java AES加密算法工具类
packageutil;importjava.security.Key;importjavax.crypto.Cipher;importjavax.crypto.KeyGenerator;import ...
- java获取扩展名_Java获取文件扩展名称
有时在处理文件时,需要根据文件类型对它们进行不同的处理. java.io.File没有任何获取文件扩展名的方法,这里提供了一个实用工具方法来获取文件扩展名. Java获取文件扩展名 在句点(.)之后, ...
- java中实现选择文件_Java 实现文件选择对话框及功能
时间:2018-10-02 概述:文件选择器 Java实现文件选择器,就是大家熟悉的打开文件.选择文件的对话框,本例子分为两部分来进行,一个部分是选择器对话框构建部分,另一部分是文件过滤部分,用于过滤 ...
最新文章
- Java中集合类型线程安全性
- 如何在机器学习的框架里实现隐私保护?
- ASP.NET Web Pages – 文件简介
- 机器学习基础专题:高斯判别分析
- 影响网站转化率的10大误区(上)
- Linux C一站式学习 第八章第三小节习题一答案
- QT动态连接库的编写
- java 线程 api_Java核心API之线程(上)
- 计算机应用基础自考,自考计算机应用基础
- Spring多数据源解决方案
- java canvas 画圆_java – 如何在Android中通过canvas绘制圆?
- 3.26 Tensorflow 实验记录
- vue中使用阿里矢量库彩色图标办法
- 线性代数【19】叉积
- echarts3 地图文字位置设置
- 大数据杀熟行为10月1日起明令禁止!
- 消费卡“裸奔”倒计时!商务部:排查风险,异常发卡企业将上“黑名单”
- P528 List接口常用实现类的对比及源码分析
- 大年初五,2017年数据科学圈哪件事最令你激动?
- JavaScript的显示和隐藏
热门文章
- npm 引用子项目模块_Java / Web项目中的NPM模块Browser-Sync
- java8并行流_Java 8:CompletableFuture与并行流
- antlr 语言 库_关于ANTLR的通用库的需求:使用反射来构建元模型
- sql 解析 java_将Java 8流解析为SQL
- spring一站式开发_Spring开发人员知道的一件事
- javafx 使用_何时使用JavaFX代替HTML
- java私有属性和私有方法_Java私有,受保护,公共和默认
- jpa 返回数据转换_如何使用JPA类型转换器加密数据
- AWS Messaging Services:选择合适的服务
- Spring-Boot 2.1.x和主要的bean定义