您的位置:首页 > 编程语言 > Java开发

java——文件加密

2015-03-06 10:51 183 查看
本次介绍一下java中很常用的简单的文件加密(注意:本次只对文件加密,不支持文件夹),通过des算法进行加密!

思路:首先定义密匙KEY的值,通过传入的字符串生成KEY值,然后我们需要定义并初始化加密cipher和解密cipher,最后进行具体的加密和解密操作!

代码如下:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.zxs.wode;

import com.zxs.util.FileDelete;
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.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

/**
*
* @author zhao
*/
public class DesFileEncrypt {
private Cipher encryptCipher;//定义加密的cipher
private Cipher decryptCipher;//定义解密的cipher
private static  final String KEY="key";//定义生成密匙的字符串
private Key key;//密匙key
/*
* 根据字符串KEY生成的密匙key
*/
public Key getKey(String srcKey){
//根据传入的字符串获得字节数组
byte [] src=srcKey.getBytes();
//定义一个长度为8的字节数组
byte[] b=new byte[8];
for(int i=0;i<src.length && i<b.length;i++){
b[i]=src[i];
}
key=new SecretKeySpec(b,"DES");
return key;
}
/*
* 初始化加密cipher和解密cipher
*/
public void initCipher(){
try {
//初始化加密cipher
encryptCipher=Cipher.getInstance("DES");
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
//初始化解密cipher
decryptCipher=Cipher.getInstance("DES");
decryptCipher.init(Cipher.DECRYPT_MODE,key);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(DesFileEncrypt.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchPaddingException ex) {
Logger.getLogger(DesFileEncrypt.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidKeyException ex) {
Logger.getLogger(DesFileEncrypt.class.getName()).log(Level.SEVERE, null, ex);
}
}
/*
* 构造方法
*/
public DesFileEncrypt(){
getKey(KEY);
initCipher();
}
/*
* 加密文件
* @param file 传入需加密文件的位置
* @return destFile 返回加密后文件的位置
*/
public String encrypt(String file) throws IOException{
String destFile=null;
try {
InputStream is=new FileInputStream(file);
CipherInputStream cis=new CipherInputStream(is,encryptCipher);
destFile=file.substring(0,file.indexOf("."))+".enc";
OutputStream os=new FileOutputStream(destFile);
byte [] b=new byte[1024];
int r;
while((r=cis.read(b)) > 0){
os.write(b, 0, r);
}
os.close();
cis.close();
is.close();
/*  if(!"".equals(file)){
new FileDelete().deleteFile(file);   //加密后删除源文件
}*/
} catch (FileNotFoundException ex) {
Logger.getLogger(DesFileEncrypt.class.getName()).log(Level.SEVERE, null, ex);
}
return  destFile;
}
/*
* 解密文件
* @param destFile 传入加密文件的位置
* @return file 返回解密后文件的位置
*/
public String decrypt(String destFile) throws IOException{
String file=null;
try {
InputStream is=new FileInputStream(destFile);
CipherInputStream cis=new CipherInputStream(is,decryptCipher);
file=destFile.substring(0,destFile.indexOf("."))+".zip";
OutputStream os=new FileOutputStream(file);
byte [] b=new byte[1024];
int r;
while((r = is.read(b)) > 0){
os.write(b,0,r);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(DesFileEncrypt.class.getName()).log(Level.SEVERE, null, ex);
}
return file;
}
public static void main(String [] args) throws IOException{
DesFileEncrypt df=new DesFileEncrypt();
String file=df.encrypt("f:/18/12.txt");
//df.decrypt("F:/11_22.enc");
}
}


最后也附上了测试代码,有兴趣的可以看一下。

本代码在加密时保存为".enc"文件,解密时解压为“.zip”文件,在使用改代码时可以根据自己的需要进行变更!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息