package com.baobaotao.place;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
public class DESEncryptUtil {
public static Key createKey() throws NoSuchAlgorithmException {
Security.insertProviderAt(new com.sun.crypto.provider.SunJCE(), 1);
KeyGenerator generator = KeyGenerator.getInstance("DES");
generator.init(new SecureRandom());
Key key = generator.generateKey();
return key;
}
public static Key getKey(InputStream is) {
try {
ObjectInputStream ois = new ObjectInputStream(is);
return (Key) ois.readObject();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
private static byte[] doEncrypt(Key key, byte[] data) {
try {
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] raw = cipher.doFinal(data);
return raw;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static InputStream doDecrypt(Key key, InputStream in) {
try {
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] tmpbuf = new byte[1024];
int count = 0;
while ((count = in.read(tmpbuf)) != -1) {
bout.write(tmpbuf, 0, count);
tmpbuf = new byte[1024];
}
in.close();
byte[] orgData = bout.toByteArray();
byte[] raw = cipher.doFinal(orgData);
ByteArrayInputStream bin = new ByteArrayInputStream(raw);
return bin;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static void main(String[] args) throws Exception {
// args = new String[]{
// "decrypt",
// "D:\\masterSpring\\chapter5\\src\\com\\baobaotao\\place\\en_car.properties",
// "D:\\masterSpring\\chapter5\\src\\com\\baobaotao\\place\\key.dat"};
// args = new String[]{
// "encrypt",
// "D:\\masterSpring\\chapter5\\src\\com\\baobaotao\\place\\car.properties",
// "D:\\masterSpring\\chapter5\\src\\com\\baobaotao\\place\\key.dat"};
// args = new String[]{
// "key",
// "D:\\masterSpring\\chapter5\\src\\com\\baobaotao\\place\\key.dat"};
if (args.length == 2 && args[0].equals("key")) {// 生成密钥文件
Key key = DESEncryptUtil.createKey();
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(args[1]));
oos.writeObject(key);
oos.close();
System.out.println("成功生成密钥文件。");
} else if (args.length == 3 && args[0].equals("encrypt")) {//对文件进行加密
File file = new File(args[1]);
FileInputStream in = new FileInputStream(file);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] tmpbuf = new byte[1024];
int count = 0;
while ((count = in.read(tmpbuf)) != -1) {
bout.write(tmpbuf, 0, count);
tmpbuf = new byte[1024];
}
in.close();
byte[] orgData = bout.toByteArray();
Key key = getKey(new FileInputStream(args[2]));
byte[] raw = DESEncryptUtil.doEncrypt(key, orgData);
file = new File(file.getParent() + "\\en_" + file.getName());
FileOutputStream out = new FileOutputStream(file);
out.write(raw);
out.close();
System.out.println("成功加密,加密文件位于:"+file.getAbsolutePath());
} else if (args.length == 3 && args[0].equals("decrypt")) {
File file = new File(args[1]);
FileInputStream fis = new FileInputStream(file);
Key key = getKey(new FileInputStream(args[2]));
InputStream raw = DESEncryptUtil.doDecrypt(key, fis);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] tmpbuf = new byte[1024];
int count = 0;
while ((count = raw.read(tmpbuf)) != -1) {
bout.write(tmpbuf, 0, count);
tmpbuf = new byte[1024];
}
raw.close();
byte[] orgData = bout.toByteArray();
file = new File(file.getParent() + "\\rs_" + file.getName());
FileOutputStream fos = new FileOutputStream(file);
fos.write(orgData);
System.out.println("成功解密,解密文件位于:"+file.getAbsolutePath());
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
《精通Spring 2.x-企业应用开发详解》源代码2
共946个文件
class:353个
java:277个
xml:150个
4星 · 超过85%的资源 需积分: 3 270 下载量 110 浏览量
2008-09-20
19:49:25
上传
评论 2
收藏 19.84MB RAR 举报
温馨提示
《精通Spring 2.x-企业应用开发详解》7到13章的源代码。
资源推荐
资源详情
资源评论
收起资源包目录
《精通Spring 2.x-企业应用开发详解》源代码2 (946个子文件)
TopicAspectj.aj 530B
TopicAspectj.aj 530B
DESEncryptUtil.class 5KB
TopicJdbcDao.class 4KB
PostJdbcDao.class 4KB
LocaleSample.class 3KB
ForumJdbcDao.class 3KB
Car.class 3KB
Car.class 3KB
Post.class 3KB
PostMixDao.class 3KB
DecryptPropertyPlaceholderConfigurer.class 3KB
Boss.class 3KB
ForumJdbcDao.class 3KB
ForumJdbcDao.class 3KB
TestBeanAttrDI.class 2KB
Topic.class 2KB
ForumHibernateDao.class 2KB
I18nGreeting.class 2KB
TestDiType.class 2KB
Topic.class 2KB
PostJpaemDao.class 2KB
TestFactoryBean.class 2KB
ForumIbatisDao.class 2KB
BbtForumImpl.class 2KB
ForumHibernateDao.class 2KB
CalendarExample.class 2KB
CalendarExample.class 2KB
ReflectTest.class 2KB
HibernateBaseDao.class 2KB
ControllablePerformaceMonitor.class 2KB
ForumJdbcDao2.class 2KB
MyInstantiationAwareBeanPostProcessor.class 2KB
AdviceMethods.class 2KB
Test.class 2KB
Test.class 2KB
ForumJdbcDao3.class 2KB
TestMixPostService.class 2KB
PostJdbcDao$1.class 2KB
BbtForumImpl.class 2KB
BbtForumImpl.class 2KB
Post.class 2KB
TestApplicationContext.class 2KB
CglibProxy.class 2KB
TopicJpaDao.class 2KB
BeanLifeCycle.class 2KB
Post.class 2KB
Post.class 2KB
Post.class 2KB
TestForumJdbcDao.class 2KB
ForumJdbcOODao.class 2KB
PollTopic.class 2KB
Topic.class 2KB
Topic.class 2KB
Car.class 2KB
LoginController.class 2KB
TestAnnoAutowire.class 2KB
TestBbtForumJta.class 2KB
TestBbtForumJta.class 2KB
TestBbtDao.class 2KB
ForumJdbcOODao$GetTopicNum.class 2KB
JDBCJobStoreRunner.class 2KB
JDBCJobStoreRunner.class 2KB
PostJdbcDao$2.class 2KB
ForumJdbcOODao$ForumQuery.class 2KB
PrivateCarReflect.class 2KB
MyJob.class 2KB
MyJob.class 2KB
UserDaoImpl.class 2KB
CronTriggerRunner.class 2KB
CronTriggerRunner.class 2KB
SimpleTriggerRunner.class 2KB
SimpleTriggerRunner.class 2KB
TestInjectFun.class 2KB
MyBeanPostProcessor.class 2KB
ForumJdbcOODao$ForumInsert.class 2KB
MyBeanFactoryPostProcessor.class 2KB
TestPostJdbcDao.class 2KB
Car.class 2KB
ForumJdbcDao3$1.class 2KB
Forum.class 2KB
MyBeanPostProcessor.class 2KB
MyBeanFactoryPostProcessor.class 2KB
GreetingDynamicPointcut.class 2KB
Boss.class 2KB
TestTagDepend.class 2KB
ForumHibernateDao2.class 1KB
TestBbtForumAspectj.class 1KB
TestBbtForumAspectj.class 1KB
Forum.class 1KB
TestBbtForumPfb.class 1KB
TestBbtForumPfb.class 1KB
TestBbtForumTx.class 1KB
TestBbtForumTx.class 1KB
Topic.class 1KB
TestPropertyPlace.class 1KB
ForumIbatisDao$1.class 1KB
TestTool.class 1KB
TestForumJdbcDao3.class 1KB
TestAutowire.class 1KB
共 946 条
- 1
- 2
- 3
- 4
- 5
- 6
- 10
资源评论
- wuqj0002013-01-22可惜只有部分代码
- 南角影2014-06-09省却了敲代码的痛苦,辛苦
- qqufriend2012-09-14如果有Pdf 就更好了
- liduo1112012-06-16可惜只有部分代码
- jiang_xiaohan2012-09-11做单项技术提升有价值
zhanhua
- 粉丝: 1
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功