1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import java.nio.charset.StandardCharsets import java.util.Base64 import javax.crypto.Cipher import javax.crypto.SecretKey import javax.crypto.spec.SecretKeySpec fun main() { val claveTextoNormal = "1234567891234567" // Codificar la clave a Base64 val encodedKey: String = Base64.getEncoder().encodeToString(claveTextoNormal.toByteArray(StandardCharsets.UTF_8)) val decodedKey: ByteArray = Base64.getDecoder().decode(encodedKey) val originalKey: SecretKey = SecretKeySpec(decodedKey, 0, decodedKey.size, "AES") println("originalKey: $claveTextoNormal") val cipher = Cipher.getInstance("AES") val clearText = "hola" val clearTextBytes = clearText.toByteArray(StandardCharsets.UTF_8) println("Texto en claro: $clearText") cipher.init(Cipher.ENCRYPT_MODE, originalKey) val cipherBytes = cipher.doFinal(clearTextBytes) val cipherText = String(cipherBytes, StandardCharsets.UTF_8) println("Texto cifrado: $cipherText") cipher.init(Cipher.DECRYPT_MODE, originalKey) val decryptedBytes = cipher.doFinal(cipherBytes) val decryptedText = String(decryptedBytes, StandardCharsets.UTF_8) println("Texto descifrado: $decryptedText") } |