Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3425396
  • 博文数量: 864
  • 博客积分: 14125
  • 博客等级: 上将
  • 技术积分: 10634
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-27 16:53
个人简介

https://github.com/zytc2009/BigTeam_learning

文章分类

全部博文(864)

文章存档

2023年(1)

2021年(1)

2019年(3)

2018年(1)

2017年(10)

2015年(3)

2014年(8)

2013年(3)

2012年(69)

2011年(103)

2010年(357)

2009年(283)

2008年(22)

分类: Java

2010-11-11 14:16:18

简单的一个java版的AES文件加密demo, 运行正常, 但文件一大速度就会很慢,不知道是否能优化一下,以提高增快加密的速度或许是我的代码写法有问题, 希望各位大俠指正

  
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class AES {

// 加密文件
public static void encryptfile(String pwd, File fileIn) throws Exception {
try {
            //读取文件
FileInputStream fis = new FileInputStream(fileIn);
byte[] bytIn = new byte[(int) fileIn.length()];
for (int i = 0; i < fileIn.length(); i++) {
bytIn[i] = (byte) fis.read();
}
            //AES加密
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(pwd.getBytes()));
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            //写文件
byte[] bytOut = cipher.doFinal(bytIn);
FileOutputStream fos = new FileOutputStream(fileIn.getPath()
+ ".aes");
for (int i = 0; i < bytOut.length; i++) {
fos.write((int) bytOut[i]);
}
fos.close();
fis.close();
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
public static void main(String[] args) throws Exception {
AES aes = new AES();
String pwd = "123";
File file = new File("d:/xxx.doc");
aes.encryptfile(pwd, file);
}
}
阅读(920) | 评论(0) | 转发(0) |
0

上一篇:AES算法的JAVA实现

下一篇:文件加密解密

给主人留下些什么吧!~~