
Kotlin library implements AES encryption/decryption, supporting 128-bit, 192-bit, and 256-bit keys with modes like ECB, CBC, CFB, OFB, CTR. Offers easy-to-use API for secure data handling.
Enjoy secure encryption with AES in Kotlin! 🚀
This repository provides a Kotlin implementation of the Advanced Encryption Standard (AES), ported from the original aes-js library. It supports AES encryption and decryption with various modes of operation, including ECB, CBC, CFB, OFB, and CTR.
Add the following dependency to your build.gradle.kts file:
implementation("io.github.niyajali:aes-kotlin:1.0.1")val key = "2b7e151628aed2a6abf7158809cf4f3c".hexToBytes() // 128-bit key
val aes = AESEncryption(key)val plaintext = "Hello, AES!".toByteArray()
val ciphertext = aes.encrypt(plaintext)
val decryptedText = aes.decrypt(ciphertext)
println("Ciphertext: ${ciphertext.toHexString()}")
println("Decrypted Text: ${String(decryptedText)}")val ecb = ModeOfOperationECB(key)
val encrypted = ecb.encrypt(plaintext)
val decrypted = ecb.decrypt(encrypted)val iv = "000102030405060708090a0b0c0d0e0f".hexToBytes() // Initialization Vector
val cbc = ModeOfOperationCBC(key, iv)
val encrypted = cbc.encrypt(plaintext)
val decrypted = cbc.decrypt(encrypted)val cfb = ModeOfOperationCFB(key, iv, segmentSize = 8) // 8-bit segment size
val encrypted = cfb.encrypt(plaintext)
val decrypted = cfb.decrypt(encrypted)val ofb = ModeOfOperationOFB(key, iv)
val encrypted = ofb.encrypt(plaintext)
val decrypted = ofb.decrypt(encrypted)val counter = Counter("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff".hexToBytes())
val ctr = ModeOfOperationCTR(key, counter)
val encrypted = ctr.encrypt(plaintext)
val decrypted = ctr.decrypt(encrypted)val paddedData = pkcs7Pad(plaintext, 16) // Pad to 16-byte blocksval strippedData = pkcs7Strip(paddedData)val hexString = bytesToHex(ciphertext)
println("Hex: $hexString")val bytes = "2b7e151628aed2a6abf7158809cf4f3c".hexToBytes()Here’s a complete example of encrypting and decrypting data using AES in CBC mode:
fun main() {
val key = "2b7e151628aed2a6abf7158809cf4f3c".hexToBytes()
val iv = "000102030405060708090a0b0c0d0e0f".hexToBytes()
val plaintext = "Hello, AES!".toByteArray()
// Encrypt
val cbc = ModeOfOperationCBC(key, iv)
val ciphertext = cbc.encrypt(plaintext)
println("Encrypted: ${bytesToHex(ciphertext)}")
// Decrypt
val decrypted = cbc.decrypt(ciphertext)
println("Decrypted: ${String(decrypted)}")
}Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
Enjoy secure encryption with AES in Kotlin! 🚀
This repository provides a Kotlin implementation of the Advanced Encryption Standard (AES), ported from the original aes-js library. It supports AES encryption and decryption with various modes of operation, including ECB, CBC, CFB, OFB, and CTR.
Add the following dependency to your build.gradle.kts file:
implementation("io.github.niyajali:aes-kotlin:1.0.1")val key = "2b7e151628aed2a6abf7158809cf4f3c".hexToBytes() // 128-bit key
val aes = AESEncryption(key)val plaintext = "Hello, AES!".toByteArray()
val ciphertext = aes.encrypt(plaintext)
val decryptedText = aes.decrypt(ciphertext)
println("Ciphertext: ${ciphertext.toHexString()}")
println("Decrypted Text: ${String(decryptedText)}")val ecb = ModeOfOperationECB(key)
val encrypted = ecb.encrypt(plaintext)
val decrypted = ecb.decrypt(encrypted)val iv = "000102030405060708090a0b0c0d0e0f".hexToBytes() // Initialization Vector
val cbc = ModeOfOperationCBC(key, iv)
val encrypted = cbc.encrypt(plaintext)
val decrypted = cbc.decrypt(encrypted)val cfb = ModeOfOperationCFB(key, iv, segmentSize = 8) // 8-bit segment size
val encrypted = cfb.encrypt(plaintext)
val decrypted = cfb.decrypt(encrypted)val ofb = ModeOfOperationOFB(key, iv)
val encrypted = ofb.encrypt(plaintext)
val decrypted = ofb.decrypt(encrypted)val counter = Counter("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff".hexToBytes())
val ctr = ModeOfOperationCTR(key, counter)
val encrypted = ctr.encrypt(plaintext)
val decrypted = ctr.decrypt(encrypted)val paddedData = pkcs7Pad(plaintext, 16) // Pad to 16-byte blocksval strippedData = pkcs7Strip(paddedData)val hexString = bytesToHex(ciphertext)
println("Hex: $hexString")val bytes = "2b7e151628aed2a6abf7158809cf4f3c".hexToBytes()Here’s a complete example of encrypting and decrypting data using AES in CBC mode:
fun main() {
val key = "2b7e151628aed2a6abf7158809cf4f3c".hexToBytes()
val iv = "000102030405060708090a0b0c0d0e0f".hexToBytes()
val plaintext = "Hello, AES!".toByteArray()
// Encrypt
val cbc = ModeOfOperationCBC(key, iv)
val ciphertext = cbc.encrypt(plaintext)
println("Encrypted: ${bytesToHex(ciphertext)}")
// Decrypt
val decrypted = cbc.decrypt(ciphertext)
println("Decrypted: ${String(decrypted)}")
}Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.