0
votes

When I try to decrypt a file on my computer the file gets corrupted. Same code in Android Java works perfectly when encrypting and decrypting a file. I convert file to byte[], then encrypt and save it. Then load the saved file and decrypt it and save it again. The text encryption and decryption works fine and does not corrupt the file. Encrypting and decrypting file on PC does not corrupt it. sending PC encrypted file to Android does not corrupt when Android decrypts it. But when I send Android encrypted file and try to decrypt on PC it is corrupt.

EDIT:

Decryption code:

    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, key); 
    byte[] byteRaw = cipher.doFinal(encrypted); 
    return byteRaw;

I know using ECB is unsecure compared to CBC but i am doing it as demonstration that you can encrypt and decrypt files on android and PC

This is how i save the byte to file:

File file = new File("decrypted.jpg"); 
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
        bos.write(byteRaw);
        bos.flush();
        bos.close(); 

The code for encryption and decryption is exactly the same on android and PC, thus theoretically should work on both devices.

2
Do you use the same key on Android and PC? Decryption only works when exact the same key is used - that is the "key" feature of encryption...Robert
Yes i did use same key using a same password with same salt and algorithm "PBKDF2WithHmacSHA1". Decrypting base64 strings of encrypted text works perfectly on PCalikhtag
Where is the encryption code?President James K. Polk

2 Answers

0
votes

if you are using java to decrypt/encrypt using Jvm you might need to concider enabling AES in your Java Virtual machine by modifying some jar files in your jvm

see this link, you will find a README File that will explain all you need to know.

Greetings

0
votes

Well I am embarrassed, I was passing encrypted data to save from my GUI instead of decrypted file data. Wasted almost a whole day on one error of passing the wrong arguiment.

Thanks for help. SOLVED