Cryptography in Kotlin: Encryption and Hashing

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Cryptography in Kotlin: Encryption and Hashing



Modern development in Kotlin (especially for Android and backend) often requires implementing cryptographic functions. In this article, we will look at the main approaches to encrypting and hashing data using Kotlin and the standard Java Cryptography Extension (JCE) and Bouncy Castle libraries.



Basic Concepts

Cryptography in Kotlin relies on JVM capabilities. You can use:

  • Symmetric encryption (AES, DES) — one key for encryption and decryption.
  • Asymmetric encryption (RSA) — a pair of keys: public and private.
  • Hashing (SHA-256, MD5) — irreversible data transformation.
  • HMAC — hashing with a secret key for integrity verification.


Project Setup

For most examples, the standard Java library is sufficient. For advanced algorithms (e.g., AES with GCM), add the dependency to build.gradle.kts:

dependencies {    implementation("org.bouncycastle:bcprov-jdk15on:1.70")}


1. Symmetric Encryption (AES)

AES is the most popular algorithm. Example of encrypting and decrypting a string:

import javax.crypto.Cipherimport javax.crypto.KeyGeneratorimport javax.crypto.SecretKeyimport javax.crypto.spec.IvParameterSpecimport java.util.Base64

fun aesEncrypt(plainText: String, secretKey: SecretKey): String { val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding") val iv = ByteArray(16) // Generate IV java.security.SecureRandom().nextBytes(iv) cipher.init(Cipher.ENCRYPT_MODE, secretKey, IvParameterSpec(iv)) val encrypted = cipher.doFinal(plainText.toByteArray()) // Combine IV and ciphertext val combined = iv + encrypted return Base64.getEncoder().encodeToString(combined)}

fun aesDecrypt(encryptedBase64: String, secretKey: SecretKey): String { val combined = Base64.getDecoder().decode(encryptedBase64) val iv = combined.copyOfRange(0, 16) val encrypted = combined.copyOfRange(16, combined.size) val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding") cipher.init(Cipher.DECRYPT_MODE, secretKey, IvParameterSpec(iv)) return String(cipher.doFinal(encrypted))}

// Usagefun main() { val keyGen = KeyGenerator.getInstance("AES") keyGen.init(256) val secretKey = keyGen.generateKey()

val original = "Hello, Kotlin Crypto!" val encrypted = aesEncrypt(original, secretKey) println("Encrypted: $encrypted")

val decrypted = aesDecrypt(encrypted, secretKey) println("Decrypted: $decrypted")}


2. Asymmetric Encryption (RSA)

RSA is used for secure data transmission. Example:

import java.security.KeyPairGeneratorimport java.security.PrivateKeyimport java.security.PublicKeyimport javax.crypto.Cipherimport java.util.Base64

fun generateRSAKeyPair(): Pair<PublicKey, PrivateKey> { val generator = KeyPairGenerator.getInstance("RSA") generator.initialize(2048) val keyPair = generator.generateKeyPair() return Pair(keyPair.public, keyPair.private)}

fun rsaEncrypt(plainText: String, publicKey: PublicKey): String { val cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding") cipher.init(Cipher.ENCRYPT_MODE, publicKey) return Base64.getEncoder().encodeToString(cipher.doFinal(plainText.toByteArray()))}

fun rsaDecrypt(encryptedBase64: String, privateKey: PrivateKey): String { val cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding") cipher.init(Cipher.DECRYPT_MODE, privateKey) return String(cipher.doFinal(Base64.getDecoder().decode(encryptedBase64)))}

fun main() { val (publicKey, privateKey) = generateRSAKeyPair() val message = "Secret message" val encryp

Recommendations