I am trying to encrypt and decrypt strings/files within Kotlin.
I am using the following tutorial which is in Java https://mkyong.com/java/java-aes-encryption-and-decryption/ to make this happen.
When I tried to run it, it throws an error of "... Cipher functions:OPENSSL_internal:BAD_DECRYPT ..." It goes wrong when the doFinal is executed within the Decrypt function.
I'am trying to fix this for hours now, but no luck.
This is the code.
private const val ENCRYPT_ALGO = "AES/GCM/NoPadding"
private const val TAG_LENGTH_BIT = 128
private const val IV_LENGTH_BYTE = 12
private const val SALT_LENGTH_BYTE = 16
fun getRandomNonce(): ByteArray {
val nonce = ByteArray(16)
SecureRandom().nextBytes(nonce)
return nonce
}
fun getAESKeyFromPassword(password: CharArray?, salt: ByteArray?): SecretKey {
val factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256")
val spec: KeySpec = PBEKeySpec(password, salt, 65536, 256)
return SecretKeySpec(factory.generateSecret(spec).encoded, "AES")
}
fun encryptFile(pText: ByteArray, password: String): String {
val salt: ByteArray = getRandomNonce()
val iv: ByteArray = getRandomNonce()
val aesKeyFromPassword: SecretKey = getAESKeyFromPassword(password.toCharArray(), salt)
val cipher = Cipher.getInstance(ENCRYPT_ALGO)
cipher.init(Cipher.ENCRYPT_MODE, aesKeyFromPassword, GCMParameterSpec(TAG_LENGTH_BIT, iv))
val cipherText = cipher.doFinal(pText)
val cipherTextWithIvSalt: ByteArray =
ByteBuffer.allocate(iv.size + salt.size + cipherText.size)
.put(iv)
.put(salt)
.put(cipherText)
.array()
return Base64.getEncoder().encodeToString(cipherTextWithIvSalt)
}
fun decryptFile(cText: String, password: String): String {
val decode: ByteArray = Base64.getDecoder().decode(cText.toByteArray())
val bb: ByteBuffer = ByteBuffer.wrap(decode)
val iv = ByteArray(IV_LENGTH_BYTE)
bb.get(iv)
val salt = ByteArray(SALT_LENGTH_BYTE)
bb.get(salt)
val cipherText = ByteArray(bb.remaining())
bb.get(cipherText)
val aesKeyFromPassword: SecretKey = getAESKeyFromPassword(password.toCharArray(), salt)
val cipher = Cipher.getInstance(ENCRYPT_ALGO)
cipher.init(Cipher.DECRYPT_MODE, aesKeyFromPassword, GCMParameterSpec(TAG_LENGTH_BIT, iv))
val plainText = cipher.doFinal(cipherText)
return String(plainText, StandardCharsets.UTF_8)
}
Where did I go wrong?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…