gpt4 book ai didi

java - 加速加密?

转载 作者:太空宇宙 更新时间:2023-11-03 12:23:10 24 4
gpt4 key购买 nike

我有这段用于加密视频文件的代码。

public static void encryptVideos(File fil,File outfile)
{
try{
FileInputStream fis = new FileInputStream(fil);
//File outfile = new File(fil2);
int read;
if(!outfile.exists())
outfile.createNewFile();
FileOutputStream fos = new FileOutputStream(outfile);
FileInputStream encfis = new FileInputStream(outfile);
Cipher encipher = Cipher.getInstance("AES");
KeyGenerator kgen = KeyGenerator.getInstance("AES");
//byte key[] = {0x00,0x32,0x22,0x11,0x00,0x00,0x00,0x00,0x00,0x23,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
SecretKey skey = kgen.generateKey();
//Lgo
encipher.init(Cipher.ENCRYPT_MODE, skey);
CipherInputStream cis = new CipherInputStream(fis, encipher);
while((read = cis.read())!=-1)
{
fos.write(read);
fos.flush();
}
fos.close();
}catch (Exception e) {
// TODO: handle exception
}
}

但是我用的文件很大,用这个方法太费时间了。我怎样才能加快速度?

最佳答案

好吧,这看起来很慢:

while((read = cis.read())!=-1)
{
fos.write(read);
fos.flush();
}

您一次读取和写入一个字节并刷新流。一次做一个缓冲区:

byte[] buffer = new byte[8192]; // Or whatever
int bytesRead;
while ((bytesRead = cis.read(buffer)) != -1)
{
fos.write(buffer, 0, bytesRead);
}
fos.flush(); // Not strictly necessary, but can avoid close() masking issues

另请注意,您关闭fos(不是cisfis),您应该在 finally block 中关闭它们。

关于java - 加速加密?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9497876/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com