-
-
Save mrhungnq95/0947497ec3a628a73b7a28f8bfa0cd1e to your computer and use it in GitHub Desktop.
Kotlin AES Encryption and Decryption
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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