We are using AES encryption in GCM block mode in order to encrypt a number of different kinds of data at rest on a mobile device - Android.
The key used for encryption is stored in the protected key-store offered Android so I am assuming that it is pretty robustly stored. However, we are using the same nonce/IV to encrypt different data. The nonce/IV is stored in shared preferences without any encryption. So I am assuming that it is not secure given a malicious app on a rooted android device can get into the shared preferences or another app.
What am I trying to understand is - if someone indeed gets hold of the plain-text nonce/IV and a number of different encrypted messages (which are encrypted using the same key) can they be able to decrypt the messages successfully?
We have proposed to make this better by using a different nonce for encrypting each message. However, I am not sure if this will still prevent someone from decrypting the message when they can get hold of the plain-text nonce, even when there is only one message that is encrypted with that nonce.
Here is the relevant piece of code
public synchronized String encrypt(String input) throws SecurityException {
try {
Cipher c = getCipher(Cipher.ENCRYPT_MODE);
byte[] encodedBytes = c.doFinal(input.getBytes("UTF-8"));
return Base64.encodeToString(encodedBytes, Base64.DEFAULT);
} catch (Exception e) {
throw new SecurityException(e);
} }
public synchronized String decrypt(String encrypted) throws SecurityException {
try {
Cipher c = getCipher(Cipher.DECRYPT_MODE);
byte[] decodedValue = Base64.decode(encrypted.getBytes("UTF-8"), Base64.DEFAULT);
byte[] decryptedVal = c.doFinal(decodedValue);
return new String(decryptedVal);
} catch (Exception e) {
throw new SecurityException(e);
} }
init
method which indicates how you use the IV. Maybe you posted like this as a "fill in the blanks" but at this time the code is not that useful in itself. Note that you can find much more on Java / GCM here. I'd strongly recommend to use a 12 byte nonce - the default for GCM. Note that the fact that the IV needs to be unique and that it doesn't have to be secret is true for any mode of operation, not just GCM - for CBC is also needs to be indistinguishable from random and OK, for ECB there is no IV. $\endgroup$