首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >加速加密?

加速加密?
EN

Stack Overflow用户
提问于 2012-02-29 19:04:23
回答 3查看 667关注 0票数 3

我有这个加密视频文件的代码。

代码语言:javascript
复制
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
  }
}

但是我使用的文件非常大,使用这种方法需要太多时间。我怎么才能加快速度呢?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-02-29 19:08:42

嗯,这看起来非常慢:

代码语言:javascript
复制
while((read = cis.read())!=-1)
{
    fos.write(read);
    fos.flush();
}

您一次只读写一个字节,然后刷新流。一次做一个缓冲区:

代码语言:javascript
复制
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块中关闭所有它们。

票数 5
EN

Stack Overflow用户

发布于 2012-02-29 19:15:13

您可以使用android NDK来编写应用程序的这一部分与C++,以获得显著的性能提升。这看起来是一种可以从中受益的情况。而且NDK可能已经有类似的东西了。

票数 2
EN

Stack Overflow用户

发布于 2015-07-12 17:55:21

你应该试试Facebook的隐藏功能。它的速度令人难以置信!

https://github.com/facebook/conceal

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9497876

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档