Java 加速加密?
我有这个加密视频文件的代码Java 加速加密?,java,android,video,encryption,aes,Java,Android,Video,Encryption,Aes,我有这个加密视频文件的代码 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(); FileOu
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 (不是<代码> CIS < /COD>或
百科全书是资源密集型的。整个想法是,你正在以一种复杂的方式破坏内容。你的速度是多少?你是否尝试过映射文件并对ByteBuffer返回的文件进行加密(ps dunno,这是否适用于android,忽略的标记)?我是新手,我不知道它的可能well,似乎已经定义。你能公布一下你目前的加密速度吗?我的文件大约是300mb。这种方法使它比以前慢earlier@Navdroid:这比一次读取和写入一个字节慢?我觉得这很难相信。我对这方面还不熟悉,先生。我也觉得这很令人困惑,但旧的方法比你的方法快…@Navdroid:我怀疑你当时可能把诊断搞糟了。此外,您还可以尝试使用不同的缓冲区大小-例如,您可能希望尝试64K。不,我检查了我的代码,但没有其他问题,我尝试增加缓冲区大小,但速度仍然不一样