How to Generate Public-Private Key in Android

M Farhan Majid
1 min readOct 24, 2021

Android, with its keystore system, enables you to generate public and private keys for cryptographic operations, such as data encryption and document signing.

This article will show you how you can do that. Note, however, that this method currently only supports API level ≥ 23.

Where’s the source code Lebowski?

Step by Step Explanation

Here’s how you do it. Let’s say you have an Android project with minSdkVersion of 23. Then, create a new file called SecurityUtil.kt and copy paste the code snippet below. This file contains static methods for generating new public and private key (keypair) and retrieve those keys.

To use it in your application, simply use SecurityUtil object like shown below:

That’s it! Now you can use the public and private key for encrypting data or signing documents!

As always, thanks for reading.

--

--