Android Data Encryption and Decryption

1
User Management System
Learn Kotlin, Retrofit, MVVM and MySQL using this all-in-one app. It is designed to be beginner friendly.

If you are like me and has spent some time doing both android and backend web development, you know that security issues, encryption, hashing etc are discussed and emphasised alot in web than in mobile development. This makes sense since the capacity of damage in the web is much greater due to it's interconnected nature.

However, in android there is also an opportunity to encrypt and decrypt sensitive texts like passwords. Mostly if you are building an app that has some sort of user management, you tend to either use a third party service like firebase authentication or roll out your own custom mgmt system. If it is the latter then you will probably have to store user details locally in sharedpreferences. That is a good opportunity to encrypt your data.

Moreover, you may want to send encrypted data over the web rather than plain text. You may decrypt it in the server before persisting to the database or even save the encrypted data directly to database for maximum security.

In this piece we look at some options for you to safely and reliably encrypt and decrypt your data in android.

(a). Codia

  • Codia
  • Example

Codia

Easy and Fast android - php - java Encryption Decryption library

  • Support UTF-8 Characters Arabic Chinese Farsi & .... Characters
  • Without adding additional characters
  • Extremely fast encrypt 100MB string per second
  • Optimized for JSON encryption
  • Min Sdk Version api 1

    Do not forget the star:)⭐️

APK Demo

How to download

Gradle

Add it in your root build.gradle at the end of repositories:
    allprojects {
         repositories {
             ...
             maven { url 'https://jitpack.io' }
         }
    }
add this line to your module build.gradle dependecies block:
    compile 'com.github.alirezaashrafi:codia:2.0.0'

Maven

Add the JitPack repository to your build file
  <repositories>
    <repository>
      <id>jitpack.io</id>
      <url>https://jitpack.io</url>
    </repository>
  </repositories>
Add the dependency
  <dependency>
    <groupId>com.github.alirezaashrafi</groupId>
    <artifactId>codia</artifactId>
    <version>2.0.0</version>
  </dependency>

android & java example

    Codia codia = new Codia("Your Own Private Key");

    String text = "Easy and Fast android - php - java Encryption Decryption library";
    String encoded = codia.encode(text);
    String decoded = codia.decode(encoded);

php example

    require_once __DIR__.'/lib/Codia.php';
    $codia = new Codia("Your Own Private Key");

    $text = "Easy and Fast android - php - java Encryption Decryption library";
    $encodedText = $codia->encode($text);
    $decodedText = $codia->decode($encodedText);

kotlin example

    val text = "Easy and Fast android - php - java Encryption Decryption library"

    val codia = Codia("Your Own Private Key")
    val encodedText = codia.encode(text)
    val decodedText = codia.decode(encodedText)

sample Encryption and Decryption process diagram


Licence

Copyright 2018 Alireza Ashrafi

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.


Author


If you liked this library, do not forget to star and follow me ⭐️❤️️💙

Eventually see my other libraries and projects

Easy and Fast android - php - java utf-8 Encryption Decryption library
No. Key Value
1. Created 2018-02-16T19:06:20Z
2. Updated 2019-07-28T18:51:13Z
3. Stars 6
5. Forks 2
6. Language Java
7. Lib Author alirezaashrafi

This Library was written By: alirezaashrafi

Kotlin Encryption Example


package com.alirezaashrafi.codia

import com.alirezaashrafi.library.Codia

/**
 * Android Created by AlirezaAshrafi on 2/16/2018.
 */

class Kotlin {

    private fun kotlin() {
        val text = "Easy and Fast android - php - java Encryption Decryption library"

        val codia = Codia("Your Own Private Key")
        val encodedText = codia.encode(text)
        val decodedText = codia.decode(encodedText)
    }
}

Java Encryption  Example


package com.alirezaashrafi.codia;

import android.content.ClipboardManager;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.alirezaashrafi.library.Codia;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;

import javax.crypto.Cipher;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();

        final Codia codia = new Codia("Your Own Private Key");
        String text = "Easy and Fast android - php - java Encryption Decryption library";
        final String encoded = codia.encode(text);
        String decoded = codia.decode(encoded);



        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                final Codia codia = new Codia(edtKey.getText().toString());

                String resString = edt.getText().toString();
                edt.setText("");
                if (radioEncrypt.isChecked()) {
                    resString = codia.encode(resString);
                } else if (radioDecrypt.isChecked()) {
                    resString = codia.decode(resString);
                }
                res.setText(resString);
            }
        });
        res.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                cm.setText(res.getText());
                Toast.makeText(getApplicationContext(), "Copied to clipboard", Toast.LENGTH_SHORT).show();
            }
        });

    }

    private EditText edtKey;
    private EditText edt;
    private RadioButton radioEncrypt;
    private RadioButton radioDecrypt;
    private TextView res;
    private Button btn;

    public void initViews() {
        edtKey = (EditText) findViewById(R.id.edtKey);
        edt = (EditText) findViewById(R.id.edt);
        radioEncrypt = (RadioButton) findViewById(R.id.radioEncrypt);
        radioDecrypt = (RadioButton) findViewById(R.id.radioDecrypt);
        res = (TextView) findViewById(R.id.res);
        btn = (Button) findViewById(R.id.btn);
    }
}

Download full code here.

 

(b). Seguro

  • seguro
  • Example

Seguro Build Status
Download

Secure persistence using AES+CBC encryption on Android with no dependencies.

Can write to Memory/SharedPreferences/SDCard

Usage

seguro = Seguro.Builder()
    .enableEncryption(encryptKey = true, encryptValue = true)
    .setEncryptionPassword("*QdfKPoRE[gC*vtqVxZ2Eg]ZM7TeWnHyYT")
    .enableLogging(true)
    .setPersistenceType(Seguro.PersistenceType.SDCard(".encrypted_secrets"))
    //.setPersistenceType(Seguro.PersistenceType.InMemory)
    //.setPersistenceType(Seguro.PersistenceType.SharedPreferences(applicationContext))
    .build()

Persistence types:

Seguro.PersistenceType.SDCard

Persists the values to your SDCARD in the folder you specified in the folderName

Seguro.PersistenceType.InMemory

Persists the values to the memory

Seguro.PersistenceType.SharedPreferences

Persists the values to your SharedPreferences

Writing:

seguro.Editor()
    .put("KEY_TIME", Date().time)
    .put("KEY_NAME", "Cesar Ferreira")
    .put("KEY_AGE", 31)
    .commit()

Because we chose to encrypt the key and the value, this will result in:

key: A0A7BDDA0D99CB02AC4DA421567535225F5F6013C5C699085D44A52404C97A83, value: D0EA346FE0A8D0C659848B10AADB2B057FE5E5AF3E15C13E195F4E89DA57E6BB
key: 9E9EA39C1F609E42E587458395C1C752DE8F06D2120483F1B08E883B53B8E739, value: 81A2B85774957B916A7067E31A662F699BCA9E87D01394D17F01AE506634BC78
key: 39E365FFA2B81165CB5CFE49098E2AFB520F3B129F092022D0FD3F09B3C5B0B1, value: 7BDC46776B8414C5A5B00FFFF45031E9C9FB8803DC8BF246CC311310EB8191ED

Reading:

val name = seguro.getString("KEY_NAME") // "Cesar Ferreira"
val age = seguro.getInt("KEY_AGE")      // 31

Delete:

seguro.Editor()
    .delete("KEY_NAME")
    .commit()

val name : String? = seguro.getString("KEY_NAME")      // null

Install

api "com.cesarferreira:seguro:+"

Made with ♥ by Cesar Ferreira

Secure persistence using AES+CBC encryption on Android with no dependencies.
No. Key Value
1. Created 2019-05-01T00:04:13Z
2. Updated 2020-12-11T11:02:06Z
3. Stars 34
5. Forks 2
6. Language Kotlin
7. Lib Author cesarferreira

This Library was written By: cesarferreira

Kotlin Encryption Example


package com.example.seguro

import android.Manifest
import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import cesarferreira.seguro.library.Seguro
import com.karumi.dexter.Dexter
import com.karumi.dexter.MultiplePermissionsReport
import com.karumi.dexter.PermissionToken
import com.karumi.dexter.listener.PermissionRequest
import com.karumi.dexter.listener.multi.MultiplePermissionsListener
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*


class MainActivity : AppCompatActivity() {

    private lateinit var seguro: Seguro

    @SuppressLint("MissingPermission")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        checkPermissions()

        seguro = Seguro.Builder()
            .enableEncryption(encryptKey = true, encryptValue = true)
            .setEncryptionPassword("*QdfKPoRE[gC*vtqVxZ2Eg]ZM7TeWnHyYTHU}DuEocJd6QxuZ9WJ")
            .setPersistenceType(Seguro.PersistenceType.SDCard(".${BuildConfig.APPLICATION_ID}"))
            .enableLogging(BuildConfig.DEBUG)
//            .setPersistenceType(Seguro.PersistenceType.InMemory)
//            .setPersistenceType(Seguro.PersistenceType.SharedPreferences(applicationContext))
            .build()

        // READ
        readButton.setOnClickListener {

            val timeDelta = TimeDelta()
            val result = seguro.getString(TIME_KEY) ?: "cant find the TIME"
            timeDelta.finish()

            Log.d("TIME", "READ: " + timeDelta.delta.toString() + " ms")

            textView.text = ""
            textView.text = result
        }

        wipeButton.setOnClickListener {
            textView.text = "all data wiped"
            seguro.clear()
        }

        // WRITE
        writeButton.setOnClickListener {

            val timeDelta = TimeDelta()

            seguro.Editor()
                .put(TIME_KEY, Date().time)
                .put(NAME_KEY, "Cesar Ferreira")
                .commit()

            timeDelta.finish()

            Log.d("TIME", "WRITE: " + timeDelta.delta.toString() + " ms")

            textView.text = "I WROTE TO PERSISTENCE"
        }
    }

    private fun checkPermissions() {
        Dexter.withActivity(this)
            .withPermissions(
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE
            ).withListener(object : MultiplePermissionsListener {
                override fun onPermissionsChecked(report: MultiplePermissionsReport) {}
                override fun onPermissionRationaleShouldBeShown(
                    permissions: List,
                    token: PermissionToken
                ) {
                }
            }).check()
    }

    companion object {
        internal var TIME_KEY = "KEY_TIME"
        internal var NAME_KEY = "KEY_NAME"
    }
}





Download Full code here.

Learn Android Retrofit using our course

Android MySQL Retrofit2 Multipart CRUD,Search,Pagination rating

When I was a 2nd year Software Engineering student, I buillt a now defunct online tool called Camposha(from Campus Share) using my then favorite language C#(ASP.NET) to compete OLX in my country(Kenya). The idea was to target campus students in Kenya. I got a few hundred signups but competing OLX proved too daunting. I decided to focus on my studies, learning other languages like Java,Python,Kotlin etc while meanwhile publishing tutorials at my YouTube Channel ProgrammingWizards TV which led to this site(camposha.info). Say hello or post me a suggestion: oclemmi@gmail.com . Follow me below; Github , and on my channel: ProgrammingWizards TV

We will be happy to hear your thoughts

Leave a reply

one + one =

Reset Password
Compare items
  • Total (0)
Compare
0
Shopping cart