java 实现文件内容的加密和解密
package com.umapp.test; 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; public class TestDES {
Key key;
public TestDES(String str) {
getKey(str);//生成密匙
}
/**
* 根据参数生成KEY
*/
public void getKey(String strKey) {
try {
KeyGenerator _generator = KeyGenerator.getInstance("DES");
_generator.init(new SecureRandom(strKey.getBytes()));
this.key = _generator.generateKey();
_generator = null;
} catch (Exception e) {
throw new RuntimeException("Error initializing SqlMap class. Cause: " + e);
}
} /**
* 文件file进行加密并保存目标文件destFile中
*
* @param file 要加密的文件 如c:/test/srcFile.txt
* @param destFile 加密后存放的文件名 如c:/加密后文件.txt
*/
public void encrypt(String file, String destFile) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
// cipher.init(Cipher.ENCRYPT_MODE, getKey());
cipher.init(Cipher.ENCRYPT_MODE, this.key);
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(destFile);
CipherInputStream cis = new CipherInputStream(is, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = cis.read(buffer)) > 0) {
out.write(buffer, 0, r);
}
cis.close();
is.close();
out.close();
}
/**
* 文件采用DES算法解密文件
*
* @param file 已加密的文件 如c:/加密后文件.txt
* * @param destFile
* 解密后存放的文件名 如c:/ test/解密后文件.txt
*/
public void decrypt(String file, String dest) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, this.key);
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(dest);
CipherOutputStream cos = new CipherOutputStream(out, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = is.read(buffer)) >= 0) {
System.out.println();
cos.write(buffer, 0, r);
}
cos.close();
out.close();
is.close();
}
public static void main(String[] args) throws Exception {
TestDES td = new TestDES("aaa");
td.encrypt("e:/r.txt", "e:/r解密.txt"); //加密
td.decrypt("e:/r解密.txt", "e:/r1.txt"); //解密 }
}
java 实现文件内容的加密和解密的更多相关文章
- 【开发技术】对文件内容进行加密-java
http://hi.baidu.com/java0804ms/item/111ea834fbd4d2f596f88d5a 实现效果:对文件内容进行加密,使之直接打开成为乱码,不以明文显示 实现步骤:1 ...
- java sm4国密算法加密、解密
java sm4国密算法加密.解密 CreationTime--2018年7月5日09点20分 Author:Marydon 1.准备工作 所需jar包: bcprov-jdk15on-1.59. ...
- java代码实现对excel加密、解密(设置或去除打开密码)
使用jxcell组件来完成对excel加密.解密的功能. jxcell.jar[点击下载](此jar没有使用限制,你懂得) 具体代码如下: import java.io.IOException; im ...
- 总结C语言在嵌入式开发中应用的知识点(文件数据的加密与解密)
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...
- JAVA的非对称加密算法RSA——加密和解密
原文转载至:https://www.cnblogs.com/OnlyCT/p/6586856.html 第一部分:RSA算法原理与加密解密 一.RSA加密过程简述 A和B进行加密通信时,B首先要生成一 ...
- JAVA的对称加密算法AES——加密和解密
出自: http://blog.csdn.net/hongtashan11/article/details/6599645 http://www.cnblogs.com/liunanjava/p/42 ...
- java swing文件内容检索工具
Java相关技术 - 文件内容检索工具 拿到一个几百M甚至上G的project让你去学习 有时候你会想知道某个关键词是在哪个文件里 比如:spring MVC配置的@RequestMapping,你从 ...
- Java解析文件内容
本文主要实现对.chk文件的解析,将其内容读出来,存入到一个Map中,文件内容实例为: A0500220140828.CHK A05002 |34622511 |373532879 |3 识别分隔符| ...
- java读取文件内容常见几种方式
①随机读取文件内容 ②以行为单位读取文件,常用于读面向行的格式化文件 ③以字符为单位读取文件,常用于读文本,数字等类型的文件 ④以字节为单位读取文件,常用于读二进制文件,如图片.声音.影像等文件 pa ...
随机推荐
- ubuntu常见错误–Could not get lock /var/lib/dpkg/lock解决
ubuntu常见错误–Could not get lock /var/lib/dpkg/lock解决 通过终端安装程序sudo apt-get install xxx时出错: E: C ...
- 数据类中引用virtual
public class City { [Key] public int CityID { set; get; } [Display(Name = "城市名称")] [Requir ...
- 一个tcp连接可以发多少http请求
-----来自:松若章 -----zhuanlan.zhihu.com/p/61423830 曾经有这么一道经典面试题:从 URL 在浏览器被被输入到页面展现的过程中发生了什么?相信大多数准备过的同学 ...
- spring自定义bean工厂模式解耦
在resources下创建bean.properties accountService=cn.flypig666.service.impl.AccountServiceImpl accountDao= ...
- Django 补充知识
目录 Django基于配置文件的编程思想 初步实现 大佬实现 跨站请求伪造csrf 什么是csrf? 前端如何解决 ajax解决 csrf相关的装饰器 FBV方式装饰器 CVB方式装饰器 Django ...
- pb_ds(平板电视)简介
据说NOI赛制可以用pbds,故整理常用方法: 1.splay 所需声明及头文件: #include <ext/pb_ds/tree_policy.hpp> #include <ex ...
- 深入浅出 Java Concurrency (24): 并发容器 part 9 双向队列集合 Deque[转]
有一段时间没有更新了.接着上节继续吧. Queue除了前面介绍的实现外,还有一种双向的Queue实现Deque.这种队列允许在队列头和尾部进行入队出队操作,因此在功能上比Queue显然要更复杂.下图描 ...
- 杂项-公司:Facebook
ylbtech-杂项-公司:Facebook Facebook(脸书)是美国的一个社交网络服务网站 ,创立于2004年2月4日,总部位于美国加利福尼亚州帕拉阿图,2012年3月6日发布Windows版 ...
- 浅谈response和request方法
一:概述 Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象. 按这个理解的话一次请求生成一个request和res ...
- 元素显示v-show
<!DOCTYPE html> <html lang="zh"> <head> <title></title> < ...