Jenya  Kirmiza
Jenya Kirmiza

Reputation: 531

encryption lasts too long android

I've encrypted and decrypted 20 mb file following way

public boolean decryptFile() {

    long millis=Calendar.getInstance().getTimeInMillis();
    try{
        String path=Environment.getExternalStorageDirectory().getAbsolutePath();

    InputStream fis = new FileInputStream(path+"/Download/circus.pbf");
    File outfile = new File(path+"/Download/circus.zip");
    int read = 0;
    if (!outfile.exists())
        outfile.createNewFile();

    FileOutputStream fos = new FileOutputStream(outfile);

    IvParameterSpec ive = new IvParameterSpec(key2.getBytes("UTF-8"));

    SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes("UTF-8"),
            "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ive);
    CipherInputStream cis = new CipherInputStream(fis, cipher);
    int b;
    byte[] d = new byte[8];
    while ((b = cis.read(d)) != -1) {
        fos.write(d, 0, b);
    }
    fos.flush();
    fos.close();
    cis.close();
        Log.e("Decryption:", (Calendar.getInstance().getTimeInMillis() - millis) / (1000 + 0.0) + " sec");
        return true;

}
    catch(IOException ex){
        ex.printStackTrace();
    }
    catch(InvalidAlgorithmParameterException ex){
        ex.printStackTrace();
    }
    catch(NoSuchPaddingException ex){
        ex.printStackTrace();
    }
    catch(InvalidKeyException ex){
        ex.printStackTrace();
    }
    catch(NoSuchAlgorithmException ex){
        ex.printStackTrace();
    }
    return false;
}


public boolean encryptFile()  {
    long millis= Calendar.getInstance().getTimeInMillis();

    // Here you read the cleartext.
    try {

        String path=Environment.getExternalStorageDirectory().getAbsolutePath();
        InputStream fis = new FileInputStream(path+"/Download/circus.zip");

        /*File folder=new File(dir);
        folder.mkdir();*/

        File outfile = new File(path+"/Download/circus.pbf");
        int read = 0;
        if (!outfile.exists())
            outfile.createNewFile();

        FileOutputStream encfos = new FileOutputStream(outfile);


        IvParameterSpec ive = new IvParameterSpec(key2.getBytes("UTF-8"));

        SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes("UTF-8"),
                "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, ive);

        // Wrap the output stream
        CipherOutputStream cos = new CipherOutputStream(encfos, cipher);
        // Write bytes
        int b;
        byte[] d = new byte[8];
        while ((b = fis.read(d)) != -1) {
            cos.write(d, 0, b);
        }

        cos.flush();
        cos.close();
        fis.close();
        Log.e("Encryption:",(Calendar.getInstance().getTimeInMillis()-millis)/(1000+0.0)+" sec");
        return true;
    }
    catch(IOException ex){
        ex.printStackTrace();
    }
    catch(InvalidAlgorithmParameterException ex){
        ex.printStackTrace();
    }
    catch(NoSuchPaddingException ex){
        ex.printStackTrace();
    }
    catch(InvalidKeyException ex){
        ex.printStackTrace();
    }
    catch(NoSuchAlgorithmException ex){
        ex.printStackTrace();
    }
    return false;
}

And I got 325 sec decrypting time. It's too long. How can I use decrypting not on all file but partially on some selected bytes

E/Decryption:﹕ 325.862 sec

Please recommend me some other encryption method or partial encryption

Upvotes: 2

Views: 715

Answers (2)

TmKVU
TmKVU

Reputation: 3000

How secure does the data have to be encrypted? If you would really like a huge performance boost you could encrypt using ECB mode instead of CBC, but please note that this would be less secure since two blocks with the exact same bytes will be outputted as the exact same encrypted block since the same key is used.

The main difference between ECB and CBC is that ECB encrypts every block using the same key and can therefore encrypt all blocks in paralel, where CBC requires the result of the previous block as an input the the encryption algorithm.

For your request:

How can I use decrypting not on all file but partially on some selected bytes?

I would like to point out that this would allow potential hackers to retrieve plaintext information which in turn could be used to infer information from the encrypted blocks. They could more easily 'guess' the next sequence of words. and compare this to the encrypted value. You would be better of using ECB. The performance boost may even allow an increase of the key size (your current key size is not clear from the question).

Upvotes: 1

fhissen
fhissen

Reputation: 357

Is there a reason why you are using such a small byte buffer?

byte[] d = new byte[8];

I suggest you play around with the array size, e.g.

byte[] d = new byte[16 * 1024];

or

byte[] d = new byte[1024 * 1024];

This should enhance the performance.

Upvotes: 3

Related Questions