Video Encryption Decryption takes too much time

Snehal Mahajan

New member
Aug 11, 2018
2
0
0
Visit site
This is code for encryption of video

FileInputStream fis = new FileInputStream(extStore+"/digilearn/try.mp4");
FileOutputStream fos = new FileOutputStream(extStore+"/digilearn/enctry.mp4");
SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(),
"AES");

Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, sks);

CipherOutputStream cos = new CipherOutputStream(fos, cipher);
int b;
byte[] d = new byte[8192];
while ((b = fis.read(d)) != -1) {
cos.write(d, 0, b);
}
// Flush and close streams.
cos.flush();
cos.close();
fis.close();

This is the code for decryption of video

File extStore = Environment.getExternalStorageDirectory();
FileInputStream fis = new FileInputStream(extStore+"/digilearn/enctry.mp4");

FileOutputStream fos = new FileOutputStream(extStore+"/digilearn/dectry.mp4");
SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(),
"AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, sks);
CipherInputStream cis = new CipherInputStream(fis, cipher);
int b;
byte[] d = new byte[2*8192];
while ((b = cis.read(d)) != -1) {
fos.write(d, 0, b);
}
fos.flush();
fos.close();
cis.close();

with above code encryption and decryption both works but it take too much time when encrypted video file from sd card with video size is 1 GB.
 

ManiacJoe

Trusted Member
Aug 5, 2015
6,326
3
38
Visit site
Your programming question is best asked in one of the Developer forums.

As you have noticed, the SD card is slower than the internal storage.
As you have also noticed, reading and writing 1 GB of data takes a noticeable amount of time. At USB3 speeds of 100 MB per second, you are looking at 10 seconds.

How much time is this actually taking?
What max time are you hoping for?
What task are you doing where the timing is important?
 

ManiacJoe

Trusted Member
Aug 5, 2015
6,326
3
38
Visit site
10 minutes does seem long. What speed is the SD card rated for?

2-3 seconds will never happen for 1 gig of data. That is just not possible.

Which phone are you testing on?
Which version of android?

The way this normally is done is to start the decryption in one thread, let it get a head start, then use a second thread to start playing the file while the decryption continues in the background on the assumption that the decryption will be faster than the playback.

I am going to request the Mods move this thread into one of the Developer forums so that better eyes can see it.
 

Rukbat

Retired Moderator
Feb 12, 2012
44,529
26
0
Visit site
In case anyone is still looking here, AES encryption isn't going to happen on a 1GB file in 2-3 seconds on any phone. And probably not on any normal desktop either.