java版AES文件加密速度问题

sds12345678 2007-07-06 05:32:34
简单的一个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);
}
}
...全文
954 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
我把这你的方法改进了一下:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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 Test {

  public static void main(String[] args) throws Exception {
    String pwd = "123";
    File file = new File("k.txt");
    System.out.println(file.length());
    System.out.println();
    long t0 = System.currentTimeMillis();
    encryptfile(pwd, file);
    System.out.println(System.currentTimeMillis() - t0);
  }

  public static void encryptfile(String pwd, File file) throws Exception {
    // 读取文件
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
    byte[] bytIn = new byte[(int) file.length()];
    bis.read(bytIn);
    bis.close();
    
    // 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);

    File file1 = new File(file.getName() + ".aes");
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file1));
    bos.write(bytOut);
    bos.close();
  }
}

为了对比,进行了测试,采用两个文件,一个为 524288 字节(512 kB)和 1048576 字节(1 MB)进行测试,分别测试其运行时间,各运行十次,求其平均时间,把你的方法称为方法Ⅰ,我的称为方法Ⅱ,测试结果如下表:

  ----------+------------+-------------
        | 512kB(ms) |  1MB(ms)
  ----------+------------+-------------
   方法Ⅰ  |  27437  |  53275
  ----------+------------+-------------
   方法Ⅱ  |   610  |   711
  ----------+------------+-------------

从上表可以看出,采用 read() 的方式,其所耗费的时间与文件大小有着密切的关系,几乎是以一种相同的倍率增加。

由于方法Ⅱ采用了缓冲流(BufferedInputStream、BufferedOutputStream)和一次性读取数据 read(byte[]) 读取文件的字节流,其所耗费的时间与文件大小的关系就不太明显了。

从上面分析可以看出,方法Ⅰ的效率问题主要是在字节读取方面的,通过方法Ⅱ改进之后,速度有了明显的提高。
  • 打赏
  • 举报
回复
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();
}

改为

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fileIn));
byte[] bytIn = new byte[(int) fileIn.length()];
bis.read(b);
bis.close();

写也是类似。

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧