Android RSA AES Example with unit tests
Branch: master
Clone or download
Latest commit 87102a1 Feb 15, 2017
Type Name Latest commit message Commit time
Failed to load latest commit information.
app add in AES/CBC/PKCS7Padding and test Feb 11, 2017
assets add in sample gif Feb 11, 2017
gradle/wrapper update gradle Jan 13, 2017
.gitignore Update .gitignore Sep 4, 2014 Update Feb 15, 2017
build.gradle unit tests Sep 4, 2014 first commit Apr 30, 2014
gradlew unit tests Sep 4, 2014
gradlew.bat first commit Apr 30, 2014

Android Arsenal

Android Encryption Example

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(),

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:

  • AES 256 bit key
  • CBC/CTR/ECB example
  • using salt for key derivation
  • streams for arbitrary data sizes
  • unit tests
  • RSA 2048 bit
  • Spongy Castle (Android version of Bouncy Castle encryption library)



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: Illegal key size or default parameters


To run the unit tests

./gradlew clean test

To build and install apk:

./gradlew clean installDebug