java——文件加密
来源:互联网 发布:js 字段默认值 编辑:程序博客网 时间:2024/06/04 00:21
本次介绍一下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”文件,在使用改代码时可以根据自己的需要进行变更!
0 0
- java——文件加密
- C#—非对称加密:加密文件
- C#—非对称加密:加密文件
- java文件加密|spring属性文件加密
- JAVA文件加密器
- JAVA文件_加密
- java 加密文件Pdf
- 加密Java class文件
- Java文件加密器
- java 文件加密解密
- Java RC4 文件加密
- java文件加密
- java 文件加密
- Java 文件加密
- JAVA数据加密——MD5加密,SHA加密,BASE64解密,BASE64加密,HMAC加密
- Java实现MD5加密_字符串加密_文件加密
- java安全——加密
- java——MD5加密
- VC++ 跟随父窗口调整控件大小
- linux||mac os如何自定义shell命令
- 详解CSS中clear属性both、left、right值的含义
- 实例说明linux硬盘分布情况
- Load Crypto.Cipher.ARC4 Failed
- java——文件加密
- JDK1.6官方下载JDK1.6API(chm)下载_JDK6APICHM中文参考下载
- android View.INVISIBLE与View.GONE的差别
- JS编程必备
- mysql中如何快速查找某个字段所在的表
- Google Protocol Buffers 入门
- CSS清除浮动的4种方法
- 64位IIS7下的PHP 5.4配置及如何连接SQL Server 2008
- 使用jQuery模拟鼠标点击a标签事件