代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
DES加密文件
作者:
汪超
/ 发布于
2013/8/20
/
678
只需给定三个参数,一个是密钥的文件地址(代码中有方法生成密钥,保存到文件),一个是需要加密的文件路径,一个是解密后文件放置的路径。简单易用
package com.gwsi.hbtax.xfaj.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.security.GeneralSecurityException; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import javax.crypto.spec.SecretKeySpec; import lantrack.common.config.Config; public class FileKey { public String keyFile = (new StringBuilder(Config.EWEBEDIT_UPLOAD_PATH)).append("\\xfaj\\moban\\key.txt").toString(); public String getKeyFile(){ return this.keyFile; } public void setKeyFile(String keyFile){ this.keyFile=keyFile; } //用java生成一个key并保存到一个二进制文件中去的方法如下: public static void saveBytePriveKey(String file) { try { String keyString="de23a211"; byte[] keyData=keyString.getBytes(); SecretKey key=new SecretKeySpec(keyData,"DES"); // KeyGenerator keyGen = KeyGenerator.getInstance("DES"); // // SecretKey key = keyGen.generateKey();// 生成私钥Key FileOutputStream fop = new FileOutputStream(file); fop.write(key.getEncoded()); fop.close(); } catch (Exception e1) { e1.printStackTrace(); } } //从密钥文件中读取内容生成密钥 public static SecretKey getBytePriveKey(String file) throws Exception { File keyf = new File(file); long length = keyf.length(); byte[] bytes = new byte[(int) length]; FileInputStream fis = new FileInputStream(keyf); // Read in the bytes int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead = fis.read(bytes, offset, bytes.length - offset)) >= 0) { offset += numRead; } DESKeySpec dks = new DESKeySpec(bytes); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(dks); return key; } //文件加密 public static void encryptFile(String plainFile, String encryptedFile, String keyFile) { try { SecretKey key = getBytePriveKey(keyFile); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, key); FileInputStream fis = new FileInputStream(plainFile); FileOutputStream fos = new FileOutputStream(encryptedFile); crypt(fis, fos, cipher); fis.close(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } public static void crypt(InputStream in, OutputStream out, Cipher cipher) throws IOException, GeneralSecurityException { int blockSize = cipher.getBlockSize(); int outputSize = cipher.getOutputSize(blockSize); System.out.println("blockSize " + blockSize + " outputSize" + outputSize); byte[] inBytes = new byte[blockSize]; byte[] outBytes = new byte[outputSize]; int inLength = 0; boolean more = true; while (more) { inLength = in.read(inBytes); if (inLength == blockSize) { int outLength = cipher.update(inBytes, 0, blockSize, outBytes);//加密流的解密 out.write(outBytes, 0, outLength); } else { more = false; } } if (inLength > 0) outBytes = cipher.doFinal(inBytes, 0, inLength); else outBytes = cipher.doFinal(); out.write(outBytes); } //文件解密 public static void decryptFile(String encryptedFile, String decryptedFile, String keyFile) { try { SecretKey key = getBytePriveKey(keyFile); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, key); FileInputStream fis = new FileInputStream(encryptedFile); FileOutputStream fos = new FileOutputStream(decryptedFile); crypt(fis, fos, cipher); fis.close(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { FileKey fk= new FileKey(); String keyFile=fk.getKeyFile(); String plainFile="D:\\1.txt"; String encryptedFile="D:\\2.txt"; fk.encryptFile(plainFile, encryptedFile, keyFile); //解密文件地址 // String path= // (new StringBuilder(Config.EWEBEDIT_UPLOAD_PATH)).append("\\xfaj\\temp\\temp.").append("txt").toString(); // System.out.println(path); // fk.decryptFile(encryptedFile, path, keyFile); } }
试试其它关键字
DES
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
汪超
贡献的其它代码
(
1
)
.
DES加密文件
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3