简单的文件加密解密
2017-09-18 11:24
246 查看
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.spec.SecretKeySpec;
public class FileDES {
/**加密解密的key*/
private Key mKey;
/**解密的密码*/
private Cipher mDecryptCipher;
/**加密的密码*/
private Cipher mEncryptCipher;
public FileDES(String key) throws Exception
{
initKey(key);
initCipher();
}
/**
* 创建一个加密解密的key
* @param keyRule
*/
public void initKey(String keyRule) {
byte[] keyByte = keyRule.getBytes();
// 创建一个空的八位数组,默认情况下为0
byte[] byteTemp = new byte[8];
// 将用户指定的规则转换成八位数组
for (int i = 0; i < byteTemp.length && i < keyByte.length; i++) {
byteTemp[i] = keyByte[i];
}
mKey = new SecretKeySpec(byteTemp, "DES");
}
/***
* 初始化加载密码
* @throws Exception
*/
private void initCipher() throws Exception
{
mEncryptCipher = Cipher.getInstance("DES");
mEncryptCipher.init(Cipher.ENCRYPT_MODE, mKey);
mDecryptCipher = Cipher.getInstance("DES");
mDecryptCipher.init(Cipher.DECRYPT_MODE, mKey);
}
/**
* 加密文件
* @param in
* @param savePath 加密后保存的位置
*/
public void doEncryptFile(InputStream in,String savePath)
{
if(in==null)
{
System.out.println("inputstream is null");
return;
}
try {
CipherInputStream cin = new CipherInputStream(in, mEncryptCipher);
OutputStream os = new FileOutputStream(savePath);
byte[] bytes = new byte[1024];
int len = -1;
while((len=cin.read(bytes))>0)
{
os.write(bytes, 0, len);
os.flush();
}
os.close();
cin.close();
in.close();
System.out.println("加密成功");
} catch (Exception e) {
System.out.println("加密失败");
e.printStackTrace();
}
}
/**
* 加密文件
* @param filePath 需要加密的文件路径
* @param savePath 加密后保存的位置
* @throws FileNotFoundException
*/
public void doEncryptFile(String filePath,String savePath) throws FileNotFoundException
{
doEncryptFile(new FileInputStream(filePath), savePath);
}
/**
* 解密文件
* @param in
*/
public void doDecryptFile(InputStream in)
{
if(in==null)
{
System.out.println("inputstream is null");
return;
}
try {
CipherInputStream cin = new CipherInputStream(in, mDecryptCipher);
BufferedReader reader = new BufferedReader(new InputStreamReader(cin)) ;
String line = null;
while((line=reader.readLine())!=null)
{
System.out.println(line);
}
reader.close();
cin.close();
in.close();
System.out.println("解密成功");
} catch (Exception e) {
System.out.println("解密失败");
e.printStackTrace();
}
}
/**
* 解密文件
* @param filePath 文件路径
* @throws Exception
*/
public void doDecryptFile(String filePath) throws Exception
{
doDecryptFile(new FileInputStream(filePath));
}
public static void main(String[] args)throws Exception {
FileDES fileDES = new FileDES("resoft.com");
fileDES.doEncryptFile("D:/aaa.xls", "D:/bbb.xls"); //加密
// fileDES.doDecryptFile("d:/b"); //解密
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.spec.SecretKeySpec;
public class FileDES {
/**加密解密的key*/
private Key mKey;
/**解密的密码*/
private Cipher mDecryptCipher;
/**加密的密码*/
private Cipher mEncryptCipher;
public FileDES(String key) throws Exception
{
initKey(key);
initCipher();
}
/**
* 创建一个加密解密的key
* @param keyRule
*/
public void initKey(String keyRule) {
byte[] keyByte = keyRule.getBytes();
// 创建一个空的八位数组,默认情况下为0
byte[] byteTemp = new byte[8];
// 将用户指定的规则转换成八位数组
for (int i = 0; i < byteTemp.length && i < keyByte.length; i++) {
byteTemp[i] = keyByte[i];
}
mKey = new SecretKeySpec(byteTemp, "DES");
}
/***
* 初始化加载密码
* @throws Exception
*/
private void initCipher() throws Exception
{
mEncryptCipher = Cipher.getInstance("DES");
mEncryptCipher.init(Cipher.ENCRYPT_MODE, mKey);
mDecryptCipher = Cipher.getInstance("DES");
mDecryptCipher.init(Cipher.DECRYPT_MODE, mKey);
}
/**
* 加密文件
* @param in
* @param savePath 加密后保存的位置
*/
public void doEncryptFile(InputStream in,String savePath)
{
if(in==null)
{
System.out.println("inputstream is null");
return;
}
try {
CipherInputStream cin = new CipherInputStream(in, mEncryptCipher);
OutputStream os = new FileOutputStream(savePath);
byte[] bytes = new byte[1024];
int len = -1;
while((len=cin.read(bytes))>0)
{
os.write(bytes, 0, len);
os.flush();
}
os.close();
cin.close();
in.close();
System.out.println("加密成功");
} catch (Exception e) {
System.out.println("加密失败");
e.printStackTrace();
}
}
/**
* 加密文件
* @param filePath 需要加密的文件路径
* @param savePath 加密后保存的位置
* @throws FileNotFoundException
*/
public void doEncryptFile(String filePath,String savePath) throws FileNotFoundException
{
doEncryptFile(new FileInputStream(filePath), savePath);
}
/**
* 解密文件
* @param in
*/
public void doDecryptFile(InputStream in)
{
if(in==null)
{
System.out.println("inputstream is null");
return;
}
try {
CipherInputStream cin = new CipherInputStream(in, mDecryptCipher);
BufferedReader reader = new BufferedReader(new InputStreamReader(cin)) ;
String line = null;
while((line=reader.readLine())!=null)
{
System.out.println(line);
}
reader.close();
cin.close();
in.close();
System.out.println("解密成功");
} catch (Exception e) {
System.out.println("解密失败");
e.printStackTrace();
}
}
/**
* 解密文件
* @param filePath 文件路径
* @throws Exception
*/
public void doDecryptFile(String filePath) throws Exception
{
doDecryptFile(new FileInputStream(filePath));
}
public static void main(String[] args)throws Exception {
FileDES fileDES = new FileDES("resoft.com");
fileDES.doEncryptFile("D:/aaa.xls", "D:/bbb.xls"); //加密
// fileDES.doDecryptFile("d:/b"); //解密
}
}
相关文章推荐
- QT文件的转码实现简单加密解密
- DES 方式:文件加密与解密的简单测试
- C语言实现简单文件加密解密
- C++ 简单的文件加密和解密 实例
- C++ 简单的文件加密和解密 实例
- 简单的class文件加密解密
- C++实现文件简单加密与解密(读取文件中的字符,对字符转换,然后写入,完成加密,解密反之)
- Qt对中文文件的简单加密解密
- C++ 简单的文件加密和解密 实例
- Visual Studio 2017中使用正则修改部分内容 如何使用ILAsm与ILDasm修改.Net exe(dll)文件 C#学习-图解教程(1):格式化数字字符串 小程序开发之图片转Base64(C#、.Net) jquery遍历table为每一个单元格取值及赋值 。net加密解密相关方法 .net关于坐标之间一些简单操作
- 对文件进行简单加密和解密操作
- 简单的加密解密文件
- java对文件简单的加密解密(异或运算)
- 简单的对图片文件进行加密解密
- 简单的对图片文件进行加密解密
- C++ 简单的文件加密和解密 实例
- IO流简单应用(文件加密和解密)
- Qt对中文文件的简单加密解密
- C++ 简单的文件加密和解密 实例
- 个人笔记-简单文件加密解密