I am implementing the AES encryption algorithm in GCM operating mode in an android application.
My IDE (Intellij Idea) tells me that to use javax.crypto.spec.GCMParameterSpec
the condition android.os.Build.VERSION.SDK_INT> = android.os.Build.VERSION_CODES.KITKAT
is required.
I tried when the condition is not verified to use a javax.crypto.spec.GCMParameterSpec
of which I downloaded the source file and included it in my project, but with it the encryption operations do not work correctly (the decrypted data does not match to original data or java.security.InvalidAlgorithmParameterException: IV must be specified in GCM mode
).
Do you have any ideas to suggest on how I can also support previous versions of Android KITKAT?
Thanks in advance.
Initial versions of Android based on Java 6 did not give you GCMParameterSpec
, but they would use IvParameterSpec
instead. Besides the (usually 12 byte) IV, the GCMParameterSpec
will give you two additional operations: the support for additional data and the tag size.
Now the tag size is not too much of a problem: first of all, usually the full 128 bits / 16 bytes are used. Furthermore you can just remove those bytes from the end of the ciphertext until you reach the required tag size, e.g. remove 4 bytes / 32 bits to get a tag size of 96 bits.
The additional data is a problem, as far as I know there is no way to specify those, at least not if you require a Cipher
instance. You could of course use GCMBlockCipher
instead, but then you'd not use Cipher
and any possible acceleration provided by the platform (as Bouncy is software only).
And yes, as indicated, it is perfectly possible to implement GCM mode yourself for Android, as you don't need to sign any providers. Of course, you'd have to use a different GCMParameterSpec
implementation, and it would be a good idea only to use the provider for the older platform, so some runtime switching seems to be required.