Nav*_*oid 3 java encryption video android 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();
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
}
}
Run Code Online (Sandbox Code Playgroud)
但我使用的文件非常大,使用这种方法需要花费太多时间.我怎样才能加快速度呢?
这开始看起来很慢:
while((read = cis.read())!=-1)
{
fos.write(read);
fos.flush();
}
Run Code Online (Sandbox Code Playgroud)
您正在一次读取和写入一个字节并刷新流.一次做一个缓冲区:
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
Run Code Online (Sandbox Code Playgroud)
另请注意,您只是关闭fos
(不是cis
或fis
),您应该在finally
块中关闭所有这些.
归档时间: |
|
查看次数: |
645 次 |
最近记录: |