Android RSA AES Example with unit tests
This example encrypts the inputted string using AES, encrypts the key via RSA, and does the reverse when the decrypt button is clicked.
We start by encrypting the plain text with AES
byte[] iv = AESEncryptDecrypt.aesEncrypt(plainTextInputStream,
"secret key".toCharArray(),
"AES/ECB/PKCS5PADDING",
encOutputStream);
We then combine the outputted IV and the key we used:
byte[] combined = Util.concat("secret key".getBytes(), iv);
Lastly we encryt the IV and key using an RSA public key:
byte[] encryptedAESKeyIV = RSAEncryptDecrypt.encryptRSA(combined, rsaKey.getPublic());
========================
This is an encryption example of RSA and AES (CBC, ECB, CTR) 256 bit key on android with unit tests. I have tried to provide a good and secure example by showcasing:
In order to build the apk and run tests you must have the JCE (Java Cryptogrpahy Extension) Unlimited Strength policy jars installed for your JRE runtime.
If you do not do this then you will see the error:
java.lang.RuntimeException: java.security.InvalidKeyException: Illegal key size or default parameters
To run the unit tests
./gradlew clean test
To build and install apk:
./gradlew clean installDebug