Android encryption/decryption using AES


h4rd4r7c0r3:

Is there a good example of how to encrypt and decrypt images and other files using AES on Android ?

Nacho L.:

Warning: this answer contains code you shouldn't use because it's not secure (use SHA1PRNG for key derivation, and use AES in ECB mode)

Instead (since 2016) use PBKDF2WithHmacSHA1 for key derivation and AES in CBC or GCM mode (GCM provides both privacy and integrity)

You can use the following functions:

private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    byte[] encrypted = cipher.doFinal(clear);
    return encrypted;
}

private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec);
    byte[] decrypted = cipher.doFinal(encrypted);
    return decrypted;
}

Call them like this:

ByteArrayOutputStream baos = new ByteArrayOutputStream();  
bm.compress(Bitmap.CompressFormat.PNG, 100, baos); // bm is the bitmap object   
byte[] b = baos.toByteArray();  

byte[] keyStart = "this is a key".getBytes();
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(keyStart);
kgen.init(128, sr); // 192 and 256 bits may not be available
SecretKey skey = kgen.generateKey();
byte[] key = skey.getEncoded();    

// encrypt
byte[] encryptedData = encrypt(key,b);
// decrypt
byte[] decryptedData = decrypt(key,encryptedData);

This should work, I'm using similar code in my project now.

Related


Android encryption/decryption using AES

h4rd4r7c0r3: Is there a good example of how to encrypt and decrypt images and other files using AES on Android ? Nacho L.: Warning: this answer contains code you shouldn't use because it's not secure (use SHA1PRNG for key derivation, and use AES in ECB mode) I

Android encryption/decryption using AES

h4rd4r7c0r3: Is there a good example of how to encrypt and decrypt images and other files using AES on Android ? Nacho L.: Warning: this answer contains code that you shouldn't use because it's not secure (use SHA1PRNG for key derivation, and use AES in ECB mo

Decrypt AES key using RSA in Android

Sijo Chosan I'm trying to encrypt a small file using AES (128 bit) and then encrypt the AES key using RSA (1024 bit). This is good. As a logical next step, I tried decrypting the AES key using RSA. Decrypting with RSA returns a 128 byte block, but my AES key i

Decrypt files using AES method in Android

Faisal Shaikh: I have encrypted a file using AES encryption in php using the following code. $ALGORITHM = 'AES-128-CBC'; $IV = '12dasdq3g5b2434b'; $password = '123'; openssl_encrypt($contenuto, $ALGORITHM, $password, 0, $IV); Now I'm trying to decrypt it in A

Decrypt files using AES method in Android

Faisal Shaikh: I have encrypted a file using AES encryption in php using the following code. $ALGORITHM = 'AES-128-CBC'; $IV = '12dasdq3g5b2434b'; $password = '123'; openssl_encrypt($contenuto, $ALGORITHM, $password, 0, $IV); Now I'm trying to decrypt it in A

Decrypt files using AES method in Android

Faisal Shaikh: I encrypted a file using AES encryption in php using the following code. $ALGORITHM = 'AES-128-CBC'; $IV = '12dasdq3g5b2434b'; $password = '123'; openssl_encrypt($contenuto, $ALGORITHM, $password, 0, $IV); Now I'm trying to decrypt it in Androi

Decrypt files using AES method in Android

Faisal Shaikh: I encrypted a file using AES encryption in php using the following code. $ALGORITHM = 'AES-128-CBC'; $IV = '12dasdq3g5b2434b'; $password = '123'; openssl_encrypt($contenuto, $ALGORITHM, $password, 0, $IV); Now I'm trying to decrypt it in Androi

Android log encryption using AES and RSA

Akinka Patil I want to mail encrypted log files from my application. Since the log can be larger, I encrypt the data with AES and encrypt the key with RSA. Since the AES key is required to decrypt the logs, I will send the encrypted key and log in the same fil

Decrypt AES key using RSA in Android

Sijo Chosan I'm trying to encrypt a small file using AES (128 bit) and then encrypt the AES key using RSA (1024 bit). This is good. As a logical next step, I tried decrypting the AES key using RSA. Decrypting with RSA returns a 128 byte block, but my AES key i

Decrypt files using AES method in Android

Faisal Shaikh: I have encrypted a file using AES encryption in php using the following code. $ALGORITHM = 'AES-128-CBC'; $IV = '12dasdq3g5b2434b'; $password = '123'; openssl_encrypt($contenuto, $ALGORITHM, $password, 0, $IV); Now I'm trying to decrypt it in A

Decrypt files using AES method in Android

Faisal Shaikh: I have encrypted a file using AES encryption in php using the following code. $ALGORITHM = 'AES-128-CBC'; $IV = '12dasdq3g5b2434b'; $password = '123'; openssl_encrypt($contenuto, $ALGORITHM, $password, 0, $IV); Now I'm trying to decrypt it in A

Android: AES encryption and decryption using GCM mode in Android?

Uday I am trying to encrypt and decrypt a string using AES algorithm and GCM mode. My code is able to encrypt the string, but I am not able to decrypt the encoded data. Steps to follow: createkey() encrypted (string) decrypt(encded_data); my code: build key pu

Android: AES encryption and decryption using GCM mode in Android?

Uday I am trying to encrypt and decrypt a string using AES algorithm and GCM mode. My code is able to encrypt the string but not able to decrypt the encoded data. Steps to follow: createkey() encrypted (string) decrypt(encded_data); my code: build key public v

Android: AES encryption and decryption using GCM mode in Android?

Uday I am trying to encrypt and decrypt a string using AES algorithm and GCM mode. My code is able to encrypt the string but not able to decrypt the encoded data. Steps to follow: createkey() encrypted (string) decrypt(encded_data); my code: build key public v

Android: AES encryption and decryption using GCM mode in Android?

Uday I am trying to encrypt and decrypt a string using AES algorithm and GCM mode. My code is able to encrypt the string, but I am not able to decrypt the encoded data. Steps to follow: createkey() encrypted (string) decrypt(encded_data); my code: build key pu

Android: AES encryption and decryption using GCM mode in Android?

Uday I am trying to encrypt and decrypt a string using AES algorithm and GCM mode. My code is able to encrypt the string, but I am not able to decrypt the encoded data. Steps to follow: createkey() encrypted (string) decrypt(encded_data); my code: build key pu

Android: AES encryption and decryption using GCM mode in Android?

Uday I am trying to encrypt and decrypt a string using AES algorithm and GCM mode. My code is able to encrypt the string, but I am not able to decrypt the encoded data. Steps to follow: createkey() encrypted (string) decrypt(encded_data); my code: build key pu

Implementing AES encryption in Android

MSR I found this Java AES encryption on the internet. Can I modify and use this Java code in an Android application? Actually I don't know if it will fit my android device. If it can be used, does it mean that this Java code can be modified? import java.io.Uns

Encrypt files with AES on Android

Linux4 hope So I'm working on a personal project for myself and I'm trying to encrypt files on my phone. These files can be anything like documents, photos, etc. Now I am trying to get it working. Whenever I run encrypt it seems to work fine and encrypt the fi

Android AES decryption

Adrian Rowe I'm a PHP programmer trying to write an Android application. I'm doing fine so far...now I'm stuck... I am calling this class below to decrypt a string that was previously encrypted in php. The class can send and receive the string normally, but fo

Store AES key in Android

Marius I want to store AES key in AndroidKeyStore on pre-M device I am trying to use the generated keyKeyGenerator KeyGenerator keyGen = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES); keyGen.init(256); SecretKey secretKey = keyGen.generateKey(); B

Store AES key in Android

Marius I want to store AES key in AndroidKeyStore on pre-M device I am trying to use the generated keyKeyGenerator KeyGenerator keyGen = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES); keyGen.init(256); SecretKey secretKey = keyGen.generateKey(); B

AES encryption test (android)

Sergey 136 I have developed an encryption program for Android that uses a symmetric key to encrypt and decrypt data (AES algorithm). So I was asked to verify that not only does reverse engineering work, but also that the data can be decrypted using only one ke

AES encryption testing (android)

sergey136 I have developed an encryption program for android which is using a symmetric key to both encrypt and decrypt the data (AES algorithm). So I have been asked to verify that not only a reverse engineering is working correctly but also that only one key

Encrypt files with AES on Android

Linux4 hope So I'm working on a personal project for myself and I'm trying to encrypt files on my phone. These files can be anything like documents, photos, etc. Now I am trying to get it working. Whenever I run encrypt it seems to work fine and encrypt the fi

Implementing AES encryption in Android

MSR I found this Java AES encryption on the internet. Can I modify and use this Java code in an Android application? Actually I don't know if it will fit my android device. If it can be used, does it mean that this Java code can be modified? import java.io.Uns

AES encryption on Android

Buddha Gabriel I have to encrypt and decrypt text on android device. I found some solutions, but when I encrypt the text again, the result is different. Can anyone tell me why? Here is my code: public class AESDemo { private static final String password

Encrypt files with AES on Android

Linux4Hope: So I'm working on a personal project for myself and I'm trying to encrypt files on my phone. These files can be anything like documents, photos, etc. Currently, I'm trying to get it working. Whenever I run encrypt it seems to work fine and encrypt

Android AES encryption key

David R. I am developing an android application and I want to use AES-128 for encryption. I use hex for the key and text. I use this website: http://testprotect.com/appendix/AEScalc , calculate the AES encryption with the following key: "c4dcc3c6ce0acaec4327b6