Skip to content

Instantly share code, notes, and snippets.

@flymrc
Last active October 30, 2023 13:24
Show Gist options
  • Save flymrc/31f0efe6cf927ff8d8e79d4c9ca9eb5c to your computer and use it in GitHub Desktop.
Save flymrc/31f0efe6cf927ff8d8e79d4c9ca9eb5c to your computer and use it in GitHub Desktop.
Kotlin AES Encryption and Decryption
// ref: https://www.baeldung.com/java-aes-encryption-decryption
import java.util.*
import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec
fun decrypt(algorithm: String, cipherText: String, key: SecretKeySpec, iv: IvParameterSpec): String {
val cipher = Cipher.getInstance(algorithm)
cipher.init(Cipher.DECRYPT_MODE, key, iv)
val plainText = cipher.doFinal(Base64.getDecoder().decode(cipherText))
return String(plainText)
}
fun encrypt(algorithm: String, inputText: String, key: SecretKeySpec, iv: IvParameterSpec): String {
val cipher = Cipher.getInstance(algorithm)
cipher.init(Cipher.ENCRYPT_MODE, key, iv)
val cipherText = cipher.doFinal(inputText.toByteArray())
return Base64.getEncoder().encodeToString(cipherText)
}
val inputText = "abcdefghigklmnopqrstuvwxyz0123456789"
val algorithm = "AES/CBC/PKCS5Padding"
val key = SecretKeySpec("1234567890123456".toByteArray(), "AES")
val iv = IvParameterSpec(ByteArray(16))
val cipherText = encrypt(algorithm, inputText, key, iv)
val plainText = decrypt(algorithm, cipherText, key, iv)
assert(inputText == plainText)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment