Skip to content

Instantly share code, notes, and snippets.

@Vishwas1
Last active April 11, 2020 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vishwas1/eab4f89aa1f31eaeee1c77a25f23a47f to your computer and use it in GitHub Desktop.
Save Vishwas1/eab4f89aa1f31eaeee1c77a25f23a47f to your computer and use it in GitHub Desktop.

Using android Keystore

Importing keystore

//TODO: import keystore in the project

Create Instance of keystore

//TODO: Implement getInstance  method

Encrypt string using keystore instance

//TODO: implement encrypt method to encrypt the message

DeCrypt string using keystore instance

//TODO: implement decrypt method to decrypt the message

Reference

@vikramIde
Copy link

package com.example.hypersignwalletcorekotlin

import android.os.Build
import android.os.Bundle
import androidx.annotation.RequiresApi
import android.security.keystore.KeyGenParameterSpec
import android.security.keystore.KeyProperties
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import java.nio.charset.Charset
import java.security.Key
import java.security.KeyStore
import javax.crypto.Cipher
import javax.crypto.KeyGenerator
import javax.crypto.SecretKey
import javax.crypto.spec.IvParameterSpec

class MainActivity : AppCompatActivity() {

    @RequiresApi(Build.VERSION_CODES.M)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val KeyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES,"AndroidKeyStore")

        val KeyGenParameterSpec = KeyGenParameterSpec.Builder("HypersignKeyAllias",
                KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT)
                .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
                .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
                .build()

        KeyGenerator.init(KeyGenParameterSpec)
        KeyGenerator.generateKey()

        val pair = encryptData("Test this encryption")

        val decryptedData = decryptData(pair.first, pair.second)

        val encrypted = pair.second.toString(Charsets.UTF_8)
        
        showLog("Encrypted data: \n$encrypted")
        showLog("Decrypted data: \n$decryptedData")
    }

    fun getKey(): SecretKey {
        val keystore = KeyStore.getInstance("AndroidKeyStore")
        keystore.load(null)

        val secretKeyEntry = keystore.getEntry("HypersignKeyAllias", null) as KeyStore.SecretKeyEntry
        return secretKeyEntry.secretKey
    }

    fun encryptData(data: String): Pair<ByteArray, ByteArray> {
        val cipher = Cipher.getInstance("AES/CBC/NoPadding")

        var temp = data
        while (temp.toByteArray().size % 16 != 0)
            temp += "\u0020"

        cipher.init(Cipher.ENCRYPT_MODE, getKey())

        val ivBytes = cipher.iv
        val encryptedBytes = cipher.doFinal(temp.toByteArray(Charsets.UTF_8))

        return Pair(ivBytes, encryptedBytes)
    }

    fun decryptData(ivBytes: ByteArray, data: ByteArray): String{
        val cipher = Cipher.getInstance("AES/CBC/NoPadding")
        val spec = IvParameterSpec(ivBytes)

        cipher.init(Cipher.DECRYPT_MODE, getKey(), spec)
        return cipher.doFinal(data).toString(Charsets.UTF_8).trim()
    }

    private fun showLog(log: String) {
        println(log)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment