
How to create an RSA key pair on Android to protect data
In this post I will explain how to create a RSA key pair on Android and use that key pair for sign and verify data. This RSA key pair will be stored in the Android KeyStore.
What is RSA?
RSA is a public-key or asymmetric crypto system. It uses a public key for encryption and a private key for decryption. Anyone can use the public key to encrypt a message but it can be decrypted only by the private key owner.
Android KeyStore
The Android KeyStore is a storage facility for cryptographic keys and certificates. The keys stored in the KeyStore can be used for cryptographic operations but the key material will not be extracted, that means that an attacker might use a stored key but will not be able to export it outside the device. When a key is created from an app and stored in the KeyStore, the access to the key will be restricted to the app itself.
Why to use RSA?
We can use RSA to sign and verify data, for example when we transfer some data to a server. Because RSA is a public-key system, we can use the private key to sign data in our app and send the public key to the server, so the server can verify that the data sent is genuine and has not been tampered. If any malicious users know the public key, the only thing they can do is to verify the integrity of the data but they cannot change the data because they need the private key to do so.

Generate the key
To generate a new key, we have to create a KeyPairGenerator object. We have to specify the algorithm of the key we are creating, in this case RSA, and the KeyStore where the key will be stored, in this case AndroidKeyStore.
Then we create a KeyGenParameterSpec object to set all the parameters for our key (note that this class is available for Marshmallow and later versions, see KeyPairGeneratorSpec for older versions support). It is mandatory to set the key alias (name of the key) and the purpose for the key. If the purpose is sign and we try to use the key to verify it will throw an exception, so we have to know what we are going to do with the keys, in this case sign and verify. Another mandatory parameters are the digest and the signature padding (because we are going to use the key to sign).
Then we can set some optional parameters such the self-signed certificate serial number, the certificate subject (common name), the starting and expiry dates and whether the key requires user authentication to be used.






