Android Data Encryption and Decryption

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


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


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


Add the JitPack repository to your build file
Add the 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


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

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.


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.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 javax.crypto.Cipher;

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {


        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() {
            public void onClick(View v) {

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

                String resString = edt.getText().toString();
                if (radioEncrypt.isChecked()) {
                    resString = codia.encode(resString);
                } else if (radioDecrypt.isChecked()) {
                    resString = codia.decode(resString);
        res.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                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(;
        edt = (EditText) findViewById(;
        radioEncrypt = (RadioButton) findViewById(;
        radioDecrypt = (RadioButton) findViewById(;
        res = (TextView) findViewById(;
        btn = (Button) findViewById(;

Download full code here.


(b). Seguro

  • seguro
  • Example

Seguro Build Status

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

Can write to Memory/SharedPreferences/SDCard


seguro = Seguro.Builder()
    .enableEncryption(encryptKey = true, encryptValue = true)

Persistence types:


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


Persists the values to the memory


Persists the values to your SharedPreferences


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

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


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



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


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 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 java.util.*

class MainActivity : AppCompatActivity() {

    private lateinit var seguro: Seguro

    override fun onCreate(savedInstanceState: Bundle?) {


        seguro = Seguro.Builder()
            .enableEncryption(encryptKey = true, encryptValue = true)
//            .setPersistenceType(Seguro.PersistenceType.InMemory)
//            .setPersistenceType(Seguro.PersistenceType.SharedPreferences(applicationContext))

        // READ
        readButton.setOnClickListener {

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

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

            textView.text = ""
            textView.text = result

        wipeButton.setOnClickListener {
            textView.text = "all data wiped"

        // WRITE
        writeButton.setOnClickListener {

            val timeDelta = TimeDelta()

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


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

            textView.text = "I WROTE TO PERSISTENCE"

    private fun checkPermissions() {
            ).withListener(object : MultiplePermissionsListener {
                override fun onPermissionsChecked(report: MultiplePermissionsReport) {}
                override fun onPermissionRationaleShouldBeShown(
                    permissions: List,
                    token: PermissionToken
                ) {

    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( Say hello or post me a suggestion: . 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)
Shopping cart