Key Management Services Android Demo using Alibaba Cloud KMS Service

Image for post
Image for post

By Sai Sarath Chandra Alibaba Cloud Tech Share Author and Alibaba Cloud MVP

In this tutorial, we will explore how to use the Alibaba Cloud Key Management Service (KMS) to create, control, and manage your encryption keys on your mobile device.

Prerequsities:

• You need an Alibaba Cloud Account. If you need you can get one with $300 by signing up here.
• Need Android Studio 3.0.0
• Basic knowledge of Android/Java.

Tutorial:

1. Activate KMS Service

a.Please login to Alibaba Cloud and Navigate to console and Click on KMS Service.

Image for post
Image for post

b.Activate the service

Image for post
Image for post

c.Please make a note of the region you want to create the keys in. I created in the “Hangzhou” region.

Image for post
Image for post

d.You can follow the below Illustrative guide for the obtaining access keys.
https://www.alibabacloud.com/help/doc-detail/51665.htm?spm=a3c0i.o51500en.b99.9.733cc1c22ZHdyM
e.Please make a note of all keys.

2. Creating Android Project

a.Create an Android Project with the name you want. I gave the name “KMSSample”.

Image for post
Image for post

b.Select the Min SDK and the Device compatibility, I am targeting the devices with minimum API level 19 and later. With only phones and tablets.

Image for post
Image for post

c.Select Empty activity and proceed further.

Image for post
Image for post

d.Keep all the defaults as shown click finish.

Image for post
Image for post

3. Setting up Code:

a.We have only one java file and the whole code related to application will be in one single file. We need to update the code in the MainActivity.java. We will go through the detailed walkthrough later. Please copy the below code into the MainActivity.java file

b.We have only one activity_main.xml file which has the all the UI related code “activity_main.xml”.Please copy the below code and paste in the corresponding activity file.

c.Now we need to udpate the strings.xml file with the relevant details.

d.Make sure you downloading dependencies :

4. Building and Running the Code

a.We need to include 2 jars which ensures the proper functioning of the project
b.Download aliyun-java-sdk-core-3.2.8.jar from
http://central.maven.org/maven2/com/aliyun/aliyun-java-sdk-core/3.2.8/
c.Download aliyun-java-sdk-kms-2.4.0.jar from
http://central.maven.org/maven2/com/aliyun/aliyun-java-sdk-kms/2.4.0/
d.Add the jars into the libs folder in the projectview and add the jars to the dependencies

Image for post
Image for post

e.Add the following code into your app level gradle file.

and Make sure you build is successful.

f.Make sure you added the permission for Internet access to the Application in the AndroidManifest using the following line:

<uses-permission android:name="android.permission.INTERNET"/>

5. Code Level Explanation

a.In onCreate method we obtain references to all of the objects in the UI and create and add assign the onClickListener to all the buttons.
b.We have four Async Taks which which are responsible for the functionality

▪ Encrypt Credentials: Creates the key on the cloud and updates the and we use the corresponding key to encrypt the username and password. You the key will not be stored anywhere in your code
▪ Decrypt Credentials: Takes the cipherBlob and calls the decrypt API to decrypt the information and displays back the same on the screen. Escape characters like n r t are preserved, even after the decrypting, which enables us to have the formatted text encrypted.
▪ Encrypt Form Data: This class does the encryption of the complete form data this is primarily to demonstrate the uniqueness of the keys we are getting from the server.
▪ Decrypt Form Data: This class does the decryption of the form data. We are storing all the encrypted data in the sharedPreferences and we are fetching it when we are calling the Decrypt tasks.

Alibaba Cloud KMS service provides the awesome service of fetching the keys and deleting them at blazing speed. This provided the application high level of security.
You can see all the methods are self explanatory. If everything is done correctly all your compilation issues will go away and the application starts installing in by clicking the small play(run) button in the status bar of the android studio.

• You can see all the methods are self described.
• If everything is done correctly all your compilation issues will go away and the application starts installing in by clicking the small play(run) button in the status bar of the android studio.

Image for post
Image for post

I strongly recommend to clone the repo you will eliminate lot of manual errors and get this app running in minutes.

Please take a look at this repo for the final code repo and let me know if you face any issues or raise any pull requests for improvements.

Reference:

https://www.alibabacloud.com/blog/Key-Management-Services-Android-Demo-using-Alibaba-Cloud-KMS-Service_p309618?spm=a2c41.11214674.0.0

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store