Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

java - Kotlin AES BAD_DECRYPT

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to reply this article.

OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.7k users

...