simbiose / Encryption

Encryption is a simple way to encrypt and decrypt strings on Android and Java project.
MIT License
355 stars 79 forks source link

last block incomplete in decryption #5

Closed jotagutierrezmar closed 9 years ago

jotagutierrezmar commented 9 years ago

Hi,

I'm using this as a class (seen it in stackOverFlow), but it's not working for me.

I have an application that makes a xml file and then sends it to another device. I encrypted the file in the first one, then the second one has to decrypt it and read it and then open another application which has to decrypt it one more time. This another app is the one that encrypted the file in the first device.

I have tried to use just one device (I create the file, then move it manually to a directory and read it from the application) and it works ok, but when i send the file from one device to another I get a "last block incomplete in decryption" exception. Why is this? how do I solve it?

thanks in advance

jotagutierrezmar commented 9 years ago

Ok, my mistake, I was using a wrong variable (D'oh!!!)

Is there a way to make the encryption/decryption faster? thanks!

ademar111190 commented 9 years ago

Thanks for feedback

about spent time issue you can reduce the the iteration count "the default is 65536", a little benchmark:

Encryption encryptionDefaultIteration = Encryption.getDefault("key", "salt", new byte[16]);

Encryption encryptionCustomIteration = Encryption.Builder
        .getDefaultBuilder("key", "salt", new byte[16])
        .setIterationCount(1)
        .build();

long startTime = Calendar.getInstance().getTimeInMillis();
for (int i = 0; i < 100; i++) {
    encryptionCustomIteration.encrypt("Some string");
}
Log.d(TAG, String.format("Time with custom iteration %d", Calendar.getInstance().getTimeInMillis() - startTime));

startTime = Calendar.getInstance().getTimeInMillis();
for (int i = 0; i < 100; i++) {
    encryptionDefaultIteration.encrypt("Some string");
}
Log.d(TAG, String.format("Time with default iteration %d", Calendar.getInstance().getTimeInMillis() - startTime));

the output, like you can see the big iteration count is the performance bad guy:

Time with custom iteration 881
Time with default iteration 3443157

You can change, or you can uses the asynchronous approach, like it:

encryption.encryptAsync("textToEncrypt", new Encryption.Callback() {
    @Override public void onSuccess(String encryptedText) {}
    @Override public void onError(Exception exception) {}
}
jotagutierrezmar commented 9 years ago

Thank you very much, works perfectly!

Could you please explain me what is actually the iterations numbers? Shouldn't it be fixed?

ademar111190 commented 9 years ago

It is an information used by android class PBEKeySpec and It is because the PKCS #5. To sum up, the higher iterations is more hard is to attack the generated key used to encrypt the information, so it is the why the default Iteration count be too big, currently it is 65536 "2^16".