CipherWrapperUtil – Simplified Encryption & Decryption for Android

An open-source Kotlin utility for seamless encryption and decryption using Android’s KeyStore

CipherWrapperUtil Kotlin Android Example
Introduction

CipherWrapperUtil is a Kotlin class that can be used to encrypt/decrypt data using symmetric key cryptography. It is designed to work with Android KeyStore, which provides a secure storage facility for cryptographic keys and certificates. In general, The wrapper class for implementation for cryptography with CIPHER and Android Key Store.

Features

Symmetric Key Encryption – Utilizes symmetric key cryptography for efficient data encryption and decryption.

Android KeyStore Integration – Seamlessly works with Android’s KeyStore to securely generate and store cryptographic keys.

Customizable Transformations – Supports various cipher transformations, with a default of AES/CBC/PKCS7Padding for balanced security and performance.

Lightweight and Efficient – Designed for optimal performance with minimal overhead.

How to use

To use CipherWrapperUtil in your Android project, simply copy the CipherWrapperUtil class into your project and instantiate it with the desired cipher transformation (e.g. CipherWrapperUtil.TRANSFORMATION_SYMMETRIC).

Encryption

To encrypt data, call the encrypt function with the alias of the entry in which the generated key will appear in Android KeyStore, and the data to encrypt as a string. The function will return a Pair containing the encrypted data as a ByteArray and the cipher IV as a String.

              val cipher = CipherWrapperUtil(CipherWrapperUtil.TRANSFORMATION_SYMMETRIC)
  val alias = "myAlias"
  val input = "secret message"
  val (encryptedData, iv) = cipher.encrypt(alias, input)
        
Decryption

To decrypt data, call the decrypt function with the alias of the entry in which the generated key will appear in Android KeyStore, the encrypted data as a ByteArray, and the cipher IV generated during encryption as a String. The function will return the decrypted data as a string.

              val cipher = CipherWrapperUtil(CipherWrapperUtil.TRANSFORMATION_SYMMETRIC)
  val alias = "myAlias"
  val decryptedData = cipher.decrypt(alias, encryptedData, iv)
        
Use Cases

Android Data Storage:

  • Shared Preferences
  • DataStore
  • SQLite Databases
  • Room Database
  • Realm Database
  • Files (Internal Storage, External Storage)
  • Content Providers

General Encryption Use Cases:

  • Network Communication (HTTPS, custom protocols)
  • API Keys
  • User Credentials (Passwords, Tokens)
  • Personally Identifiable Information (PII)
  • Financial Data
  • Medical Data
  • Game Data
  • Offline Data
  • Configuration Files
  • Secure Key Storage
  • End-to-End Encryption
  • Data at Rest Encryption
  • Data in Transit Encryption
  • Two-Factor Authentication (2FA)
  • Digital Signatures
  • Message Authentication Codes (MACs)
  • Password Hashing
Crafted In
Repository

https://github.com/thesarangal/CipherWrapperUtil

Let’s Improve Together

Your reactions guide development. Please share your thoughts on new features or recommendations. Unitedly we can improve to help all.