AES — Encryption & Decryption using Kotlin
What is the Advanced Encryption Standard or AES?
The Advanced Encryption Standard, or AES, is a symmetric block cipher chosen by the U.S. government to protect classified information and is implemented in software and hardware throughout the world to encrypt sensitive data.
How does AES encryption work?
AES comprises three block ciphers: AES-128, AES-192, and AES-256. Each cipher encrypts and decrypts data in blocks of 128 bits using cryptographic keys of 128-, 192- and 256-bits, respectively.
Getting started
I have prepared this tutorial using Intellij IDEA and Kotlin programming language. To start with, you can create a new project using Kotlin or you can just create a Kotlin class in your current project.
Library Dependency
I have used BouncyCastle dependency for this project. You can find maven/gradle dependency e.g:
Maven: bouncycastle:bcprov-jdk16:136
Encryptor
fun encrypt(strToEncrypt: String, secret_key: String): String? {
Security.addProvider(BouncyCastleProvider())
var keyBytes: ByteArray
try {
keyBytes = secret_key.toByteArray(charset("UTF8"))
val skey = SecretKeySpec(keyBytes, "AES")
val input = strToEncrypt.toByteArray(charset("UTF8"))
synchronized(Cipher::class.java) {
val cipher = Cipher.getInstance("AES/ECB/PKCS7Padding")
cipher.init(Cipher.ENCRYPT_MODE, skey)
val cipherText = ByteArray(cipher.getOutputSize(input.size))
var ctLength = cipher.update(
input, 0, input.size,
cipherText, 0
)
ctLength += cipher.doFinal(cipherText, ctLength)
return String(
Base64.encode(cipherText)
)
}
} catch (uee: UnsupportedEncodingException) {
uee.printStackTrace()
} catch (ibse: IllegalBlockSizeException) {
ibse.printStackTrace()
} catch (bpe: BadPaddingException) {
bpe.printStackTrace()
} catch (ike: InvalidKeyException) {
ike.printStackTrace()
} catch (nspe: NoSuchPaddingException) {
nspe.printStackTrace()
} catch (nsae: NoSuchAlgorithmException) {
nsae.printStackTrace()
} catch (e: ShortBufferException) {
e.printStackTrace()
}
return null
}
Decryptor
fun decryptWithAES(key: String, strToDecrypt: String?): String? {
Security.addProvider(BouncyCastleProvider())
var keyBytes: ByteArray
try {
keyBytes = key.toByteArray(charset("UTF8"))
val skey = SecretKeySpec(keyBytes, "AES")
val input = org.bouncycastle.util.encoders.Base64
.decode(strToDecrypt?.trim { it <= ' ' }?.toByteArray(charset("UTF8")))
synchronized(Cipher::class.java) {
val cipher = Cipher.getInstance("AES/ECB/PKCS7Padding")
cipher.init(Cipher.DECRYPT_MODE, skey)
val plainText = ByteArray(cipher.getOutputSize(input.size))
var ptLength = cipher.update(input, 0, input.size, plainText, 0)
ptLength += cipher.doFinal(plainText, ptLength)
val decryptedString = String(plainText)
return decryptedString.trim { it <= ' ' }
}
} catch (uee: UnsupportedEncodingException) {
uee.printStackTrace()
} catch (ibse: IllegalBlockSizeException) {
ibse.printStackTrace()
} catch (bpe: BadPaddingException) {
bpe.printStackTrace()
} catch (ike: InvalidKeyException) {
ike.printStackTrace()
} catch (nspe: NoSuchPaddingException) {
nspe.printStackTrace()
} catch (nsae: NoSuchAlgorithmException) {
nsae.printStackTrace()
} catch (e: ShortBufferException) {
e.printStackTrace()
}
return null
}
Secret Key example
val secretKey: String = “662ede816988e58fb6d057d9d85605e0”
Enjoy.. and below is my Github project sample.