Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Key Management Services Android Demo Using Alibaba Cloud KMS Service

DZone's Guide to

Key Management Services Android Demo Using Alibaba Cloud KMS Service

Learn how to use Alibaba Cloud's Key Management Service to manage encryption keys on your Android device.

· Mobile Zone ·
Free Resource

Written by Sai Sarath Chandra, Alibaba Cloud Tech Share author. Tech Share is Alibaba Cloud’s incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

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.

Prerequisites

  • You need an Alibaba Cloud Account. If you need, you can sign up for a free trial here.

  • Android Studio 3.0.0

  • Basic knowledge of Android/Java

1. Activate KMS Service

Please login to Alibaba Cloud, navigate to the console, and click on "KMS Service."

Image title

Activate the service.

Image title

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

Image title

You can follow this illustrative guide for obtaining access keys.

Image title

Please make a note of all keys.

Image title

2. Creating an Android Project

Create an Android project with the name you want. I gave it the name "KMSSample."

Image title

Select the minimum SDK and the device compatibility. I am targeting devices with a minimum API level 19 and later, for only phones and tablets.

Image title

Select "Empty activity" and proceed.

Image title

Keep all the defaults as shown and click "finish."

Image title

3. Setting Up the Code

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

package sample.alibabacloud.kmssample;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.TextInputEditText;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.kms.model.v20160120.CreateKeyRequest;
import com.aliyuncs.kms.model.v20160120.CreateKeyResponse;
import com.aliyuncs.kms.model.v20160120.DecryptRequest;
import com.aliyuncs.kms.model.v20160120.DecryptResponse;
import com.aliyuncs.kms.model.v20160120.DescribeKeyRequest;
import com.aliyuncs.kms.model.v20160120.DescribeKeyResponse;
import com.aliyuncs.kms.model.v20160120.EncryptRequest;
import com.aliyuncs.kms.model.v20160120.EncryptResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    static DefaultAcsClient kmsClient;
    private static final String TAG = "MainActivity";
    String regionId ;
    String accessKeyId ;
    String accessKeySecret ;
    TextView output;
    TextInputEditText userName,passWord,email,address;
    Button eCredentials,dCredentials, eForm, dForm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        regionId = getString(R.string.regionId);
        accessKeyId = getString(R.string.AccessKey);
        accessKeySecret = getString(R.string.AccessKeySecret);
        output = findViewById(R.id.output);
        userName = findViewById(R.id.userName);
        passWord = findViewById(R.id.passWord);
        email = findViewById(R.id.email);
        address = findViewById(R.id.address);
        eCredentials = findViewById(R.id.eCredentials);
        dCredentials = findViewById(R.id.dCredentials);
        eForm = findViewById(R.id.eForm);
        dForm = findViewById(R.id.dForm);
        eCredentials.setOnClickListener(this);
        dCredentials.setOnClickListener(this);
        eForm.setOnClickListener(this);
        dForm.setOnClickListener(this);
        Log.d(TAG, "===========================================");
        Log.d(TAG, "KMS Service started");
        Log.d(TAG, "===========================================\n");
        /**
         * RegionId: "cn-hangzhou" and "ap-southeast-1", eg. "cn-hangzhou"
         */
         kmsClient = kmsClient(regionId, accessKeyId, accessKeySecret);
    }
    private static DefaultAcsClient kmsClient(String regionId, String accessKeyId, String accessKeySecret) {
        /**
         * Construct an Aliyun Client:
         * Set RegionId, AccessKeyId and AccessKeySecret
         */
        IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }
    private static CreateKeyResponse CreateKey(String keyDesc, String keyUsage) throws ClientException {
        final CreateKeyRequest ckReq = new CreateKeyRequest();
        ckReq.setProtocol(ProtocolType.HTTPS);
        ckReq.setAcceptFormat(FormatType.JSON);
        ckReq.setMethod(MethodType.POST);
        ckReq.setDescription(keyDesc);
        ckReq.setKeyUsage(keyUsage);
        final CreateKeyResponse response = kmsClient.getAcsResponse(ckReq);
        return response;
    }
    private static DescribeKeyResponse DescribeKey(String keyId) throws ClientException {
        final DescribeKeyRequest decKeyReq = new DescribeKeyRequest();
        decKeyReq.setProtocol(ProtocolType.HTTPS);
        decKeyReq.setAcceptFormat(FormatType.JSON);
        decKeyReq.setMethod(MethodType.POST);
        decKeyReq.setKeyId(keyId);
        final DescribeKeyResponse decKeyRes = kmsClient.getAcsResponse(decKeyReq);
        return decKeyRes;
    }
    private static EncryptResponse Encrypt(String keyId, String plainText) throws ClientException {
        final EncryptRequest encReq = new EncryptRequest();
        encReq.setProtocol(ProtocolType.HTTPS);
        encReq.setAcceptFormat(FormatType.JSON);
        encReq.setMethod(MethodType.POST);
        encReq.setKeyId(keyId);
        encReq.setPlaintext(plainText);
        final EncryptResponse encResponse = kmsClient.getAcsResponse(encReq);
        return encResponse;
    }
    private static DecryptResponse Decrypt(String cipherBlob) throws ClientException {
        final DecryptRequest decReq = new DecryptRequest();
        decReq.setProtocol(ProtocolType.HTTPS);
        decReq.setAcceptFormat(FormatType.JSON);
        decReq.setMethod(MethodType.POST);
        decReq.setCiphertextBlob(cipherBlob);
        final DecryptResponse decResponse = kmsClient.getAcsResponse(decReq);
        return decResponse;
    }
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if(id == R.id.eCredentials){
            EncryptCredentials encryptCredentials = new EncryptCredentials();
            encryptCredentials.execute(kmsClient);
        }else if(id == R.id.dCredentials){
            DecryptCredentials decryptCredentials = new DecryptCredentials(this);
            decryptCredentials.execute(kmsClient);
        }else if(id == R.id.eForm){
            EncryptFormData encryptFormData = new EncryptFormData();
            encryptFormData.execute(kmsClient);
        }else if(id == R.id.dForm) {
            DecryptFormData decryptFormData = new DecryptFormData(this);
            decryptFormData.execute(kmsClient);
        }
    }
    class EncryptCredentials extends AsyncTask<DefaultAcsClient,Void,Void>{
        String keyId = null;
        String cipherBlob = null;
        @Override
        protected Void doInBackground(DefaultAcsClient... defaultAcsClients) {
            kmsClient = defaultAcsClients[0];
            /*Create a Key*/
        try {
            final CreateKeyResponse response = CreateKey("testkey", "ENCRYPT/DECRYPT");
            /**
             * Parse response and do more further
             */
            System.out.println(response.getKeyMetadata());
            CreateKeyResponse.KeyMetadata meta = response.getKeyMetadata();
            System.out.println("CreateTime: " + meta.getCreationDate());
            System.out.println("Description: " + meta.getDescription());
            System.out.println("KeyId: " + meta.getKeyId());
            keyId = meta.getKeyId();
            System.out.println("KeyState: " + meta.getKeyState());
            System.out.println("KeyUsage: " + meta.getKeyUsage());
            System.out.println("===========================================");
            System.out.println("Create MasterKey Success!");
            System.out.println("===========================================\n");
        } catch (ClientException eResponse) {
            System.out.println("Failed.");
            System.out.println("Error code: " + eResponse.getErrCode());
            System.out.println("Error message: " + eResponse.getErrMsg());
        }
        /*Describe the Key */
            try {
                final DescribeKeyResponse decKeyRes = DescribeKey(keyId);
                /**
                 * Parse response and do more further
                 */
                Log.d(TAG, "DescribeKey Response: ");
                DescribeKeyResponse.KeyMetadata meta = decKeyRes.getKeyMetadata();
                Log.d(TAG, "KeyId: " + meta.getKeyId());
                Log.d(TAG, "Description: " + meta.getDescription());
                Log.d(TAG, "KeyState: " + meta.getKeyState());
                Log.d(TAG, "KeyUsage: " + meta.getKeyUsage());
                Log.d(TAG, "===========================================");
                Log.d(TAG, "Describe the MasterKey success!");
                Log.d(TAG, "===========================================\n");
            } catch (ClientException eResponse) {
                Log.d(TAG, "Failed.");
                Log.d(TAG, "Error code: " + eResponse.getErrCode());
                Log.d(TAG, "Error message: " + eResponse.getErrMsg());
            }
            /**
             * Encrypt the plain text and got a cipher one
             */
            try {
                EncryptResponse encResponse = Encrypt(keyId, getCredentials());
                cipherBlob = encResponse.getCiphertextBlob();
                Log.d(TAG, "CiphertextBlob: " + cipherBlob);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        output.setText("The Encrypt Credentials are\n"+cipherBlob);
                        setEncryptCredentials(cipherBlob);
                    }
                });
                Log.d(TAG, "KeyId: " + encResponse.getKeyId());
                Log.d(TAG, "===========================================");
                Log.d(TAG, "Encrypt the plain text success!");
                Log.d(TAG, "===========================================\n");
            } catch (ClientException eResponse) {
                Log.d(TAG, "Failed.");
                Log.d(TAG, "Error code: " + eResponse.getErrCode());
                Log.d(TAG, "Error message: " + eResponse.getErrMsg());
            }
            return null;
        }
    }
    class DecryptCredentials extends AsyncTask<DefaultAcsClient,Void,Void>{
        public MainActivity mainActivity;
        public DecryptCredentials(MainActivity activity){
            this.mainActivity = activity;
        }
        @Override
        protected Void doInBackground(DefaultAcsClient... defaultAcsClients) {
            try {
                final DecryptResponse decResponse = Decrypt(mainActivity.getEncryptCredentials());
                Log.d(TAG, "Plaintext: " + decResponse.getPlaintext());
                String verifyPlainText = decResponse.getPlaintext();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        output.setText("The Decrypt Credentials are \n"+decResponse.getPlaintext());
                    }
                });
                int isMatch = verifyPlainText.compareTo(mainActivity.getCredentials());
                Log.d(TAG, "KeyId: " + decResponse.getKeyId());
                Log.d(TAG, "===========================================");
                Log.d(TAG, "Decrypt the cipher text success, result " + (isMatch == 0 ? "match" : "mismatch" + "\n"));
                Log.d(TAG, "===========================================\n");
            } catch (ClientException eResponse) {
                Log.d(TAG, "Failed.");
                Log.d(TAG, "Error code: " + eResponse.getErrCode());
                Log.d(TAG, "Error message: " + eResponse.getErrMsg());
            }
            return null;
        }
    }
    class EncryptFormData extends AsyncTask<DefaultAcsClient,Void,Void>{
        String keyId = null;
        String cipherBlob = null;
        @Override
        protected Void doInBackground(DefaultAcsClient... defaultAcsClients) {
            kmsClient = defaultAcsClients[0];
            /*Create a Key*/
            try {
                final CreateKeyResponse response = CreateKey("testkey", "ENCRYPT/DECRYPT");
                /**
                 * Parse response and do more further
                 */
                System.out.println(response.getKeyMetadata());
                CreateKeyResponse.KeyMetadata meta = response.getKeyMetadata();
                System.out.println("CreateTime: " + meta.getCreationDate());
                System.out.println("Description: " + meta.getDescription());
                System.out.println("KeyId: " + meta.getKeyId());
                keyId = meta.getKeyId();
                System.out.println("KeyState: " + meta.getKeyState());
                System.out.println("KeyUsage: " + meta.getKeyUsage());
                System.out.println("===========================================");
                System.out.println("Create MasterKey Success!");
                System.out.println("===========================================\n");
            } catch (ClientException eResponse) {
                System.out.println("Failed.");
                System.out.println("Error code: " + eResponse.getErrCode());
                System.out.println("Error message: " + eResponse.getErrMsg());
            }
        /*Describe the Key */
            try {
                final DescribeKeyResponse decKeyRes = DescribeKey(keyId);
                /**
                 * Parse response and do more further
                 */
                Log.d(TAG, "DescribeKey Response: ");
                DescribeKeyResponse.KeyMetadata meta = decKeyRes.getKeyMetadata();
                Log.d(TAG, "KeyId: " + meta.getKeyId());
                Log.d(TAG, "Description: " + meta.getDescription());
                Log.d(TAG, "KeyState: " + meta.getKeyState());
                Log.d(TAG, "KeyUsage: " + meta.getKeyUsage());
                Log.d(TAG, "===========================================");
                Log.d(TAG, "Describe the MasterKey success!");
                Log.d(TAG, "===========================================\n");
            } catch (ClientException eResponse) {
                Log.d(TAG, "Failed.");
                Log.d(TAG, "Error code: " + eResponse.getErrCode());
                Log.d(TAG, "Error message: " + eResponse.getErrMsg());
            }
            /**
             * Encrypt the plain text and got a cipher one
             */
            try {
                EncryptResponse encResponse = Encrypt(keyId, getFormData());
                cipherBlob = encResponse.getCiphertextBlob();
                Log.d(TAG, "CiphertextBlob: " + cipherBlob);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        output.setText("The Encrypt Credentials are\n"+cipherBlob);
                        setEncryptedFormData(cipherBlob);
                    }
                });
                Log.d(TAG, "KeyId: " + encResponse.getKeyId());
                Log.d(TAG, "===========================================");
                Log.d(TAG, "Encrypt the plain text success!");
                Log.d(TAG, "===========================================\n");
            } catch (ClientException eResponse) {
                Log.d(TAG, "Failed.");
                Log.d(TAG, "Error code: " + eResponse.getErrCode());
                Log.d(TAG, "Error message: " + eResponse.getErrMsg());
            }
            return null;
        }
    }
    class DecryptFormData extends AsyncTask<DefaultAcsClient,Void,Void>{
        private MainActivity mainActivity;
        public DecryptFormData(MainActivity activity){
            this.mainActivity = activity;
        }
        @Override
        protected Void doInBackground(DefaultAcsClient... defaultAcsClients) {
            try {
                final DecryptResponse decResponse = Decrypt(mainActivity.getEncryptedFormData());
                Log.d(TAG, "Plaintext: " + decResponse.getPlaintext());
                String verifyPlainText = decResponse.getPlaintext();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        output.setText("The Decrypt Credentials are \n"+decResponse.getPlaintext());
                    }
                });
                int isMatch = verifyPlainText.compareTo(mainActivity.getCredentials());
                Log.d(TAG, "KeyId: " + decResponse.getKeyId());
                Log.d(TAG, "===========================================");
                Log.d(TAG, "Decrypt the cipher text success, result " + (isMatch == 0 ? "match" : "mismatch" + "\n"));
                Log.d(TAG, "===========================================\n");
            } catch (ClientException eResponse) {
                Log.d(TAG, "Failed.");
                Log.d(TAG, "Error code: " + eResponse.getErrCode());
                Log.d(TAG, "Error message: " + eResponse.getErrMsg());
            }
            return null;
        }
    }
    public String getCredentials(){
            return userName.getText() + "\n" +passWord.getText();
    }
    public void setEncryptCredentials(String credentials){
        SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.FILE_NAME),MODE_PRIVATE);
        SharedPreferences.Editor editor  = sharedPreferences.edit();
        editor.putString(getString(R.string.CREDENTIALS),credentials);
        editor.apply();
    }
    public String getEncryptCredentials(){
        SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.FILE_NAME),MODE_PRIVATE);
        return sharedPreferences.getString(getString(R.string.CREDENTIALS), null);
    }
    public String getFormData(){
        return userName.getText()+"\n"+passWord.getText()+"\n"+email.getText()+"\n"+address.getText();
    }
    public void setEncryptedFormData(String formData){
        SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.FILE_NAME),MODE_PRIVATE);
        SharedPreferences.Editor editor  = sharedPreferences.edit();
        editor.putString(getString(R.string.FORM_DATA),formData);
        editor.apply();
    }
    public String getEncryptedFormData(){
        SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.FILE_NAME),MODE_PRIVATE);
        return sharedPreferences.getString(getString(R.string.FORM_DATA), null);
    }
}

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 it in the corresponding activity file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="12dp"
    tools:context="sample.alibabacloud.kmssample.MainActivity">
    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/userNameLayout"
        android:layout_marginBottom="8dp">
        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/userName"
            android:hint="Enter UserName"/>
    </android.support.design.widget.TextInputLayout>
    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:id="@+id/passWordLayout"
        android:layout_below="@id/userNameLayout"
        >
        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/passWord"
            android:hint="Enter Password"/>
    </android.support.design.widget.TextInputLayout>
    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:id="@+id/emailLayout"
        android:layout_below="@id/passWordLayout">
        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:id="@+id/email"
            android:hint="Enter Email"/>
    </android.support.design.widget.TextInputLayout>
    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:id="@+id/addressLayout"
        android:layout_below="@id/emailLayout">
        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:id="@+id/address"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:hint="Enter Address"/>
    </android.support.design.widget.TextInputLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/btnLayout"
        android:layout_marginBottom="12dp"
        android:layout_below="@id/addressLayout">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:id="@+id/eCredentials"
                android:text="Encrypt Credentials"/>
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:id="@+id/dCredentials"
                android:text="Decrypt \n Credentials"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:id="@+id/eForm"
                android:text="Encrypt Form"/>
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:id="@+id/dForm"
                android:text="Decrypt Form"/>
        </LinearLayout>
    </LinearLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:layout_below="@id/btnLayout"
        android:id="@+id/output"
        android:textSize="14sp"
        android:textColor="#000000"
        android:text="The O/P is"/>
</RelativeLayout>

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

<resources>
    <string name="app_name">KMSSample</string>
    <!-- Alibaba KMS Service details-->
    <!-- Please replace this details with your own-->
    <!--Public Endpoint-->
    <string name="regionId">UPDATE YOUR REGION ID HERE Ex. cn-hangzhou</string>
    <!-- Access ID -->
    <string name="AccessKey">UPDATE YOUR ACCESS ID</string>
    <!-- Access key Secret -->
    <string name="AccessKeySecret">UPDATE YOUR ACCESS KEY HERE</string>
    <string name="FILE_NAME">LOGIN_DETAILS</string>
    <string name="CREDENTIALS">CREDENTIALS</string>
    <string name="FORM_DATA">FORM_DATA</string>
</resources>

Make sure you download the dependencies.

4. Building and Running the Code

We need to include two jars, which ensure the proper functioning of the project.

Download aliyun-java-sdk-core-3.2.8.jar.

Download aliyun-java-sdk-kms-2.4.0.jar.

Add the jars into the libs folder in the projectview and add the jars to the dependencies:

Image title

Add the following code into your app level gradle file.

apply plugin: 'com.android.application'
android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "sample.alibabacloud.kmssample"
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:design:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    implementation files('libs/aliyun-java-sdk-kms-2.4.0.jar')
    implementation files('libs/aliyun-java-sdk-core-3.2.8.jar')
}

Make sure your build is successful.

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

In the onCreate method, we obtain references to all of the objects in the UI and create and assign the onClickListener to all the buttons.

We have four Async Taks which are responsible for the functionality:

  1. Encrypt Credentials: Creates the key on the cloud and updates it; we use the corresponding key to encrypt the username and password. Your key will not be stored anywhere in your code

  2. Decrypt Credentials: Takes the cipherBlob and calls the decrypt API to decrypt the information, then displays it on the screen. Escape characters like n r t are preserved, even after the decrypting, which enables us to have the formatted text encrypted.

  3. Encrypt Form Data: This class encrypts the complete form data, primarily to demonstrate the uniqueness of the keys we are getting from the server.

  4. Decrypt Form Data: This class decrypts the form data. We are storing all the encrypted data in sharedPreferences and fetching it when we call the Decrypt tasks.

Alibaba Cloud KMS service provides the awesome service of fetching the keys and deleting them at blazing speed. This provides the application with a high level of security.

As 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 by clicking the small play (run) button in the status bar of Android studio.

Image title

I strongly recommend cloning the repo — you will eliminate a 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.

Topics:
kms ,security ,android ,mobile ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}