javax.crypto.BadPaddingException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT during decryption

I am trying to make an android application in kotlin, after i save the user info (that i got from my API) in a file on the device i run the method to decrypt it, but it gives me “javax.crypto.BadPaddingException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT” exception. And I can’t figure out why, somebody help?

This is the code of the decryption function:

package com.example.uniapp
import android.annotation.SuppressLint
import android.os.Build
import androidx.annotation.RequiresApi
import java.nio.charset.StandardCharsets
import java.util.Base64
import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec

object EncryptionUtils {

    @SuppressLint("NewApi")
    fun decrypt(encryptedText: String, aesKey: String): String {
        // Decode the Base64 encoded string
        val fullCipher = Base64.getDecoder().decode(encryptedText)

        // Create an AES cipher instance
        val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
        val blockSize = cipher.blockSize

        // Extract the IV and the actual cipher text
        val iv = fullCipher.copyOfRange(0, blockSize)
        val cipherBytes = fullCipher.copyOfRange(blockSize, fullCipher.size)

        // Setup the key and IV
        val keySpec = SecretKeySpec(aesKey.toByteArray(StandardCharsets.UTF_8), "AES")
        val ivSpec = IvParameterSpec(iv)

        // Initialize the cipher for decryption
        cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec)

        // Perform decryption
        val decryptedBytes = cipher.doFinal(cipherBytes)

        // Convert decrypted bytes to string
        return String(decryptedBytes, StandardCharsets.UTF_8)
    }
}

And this is the login activity that calls decryption (it’s not finished)

package com.example.uniapp
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import com.example.uniapp.network.NetworkService
import com.example.uniapp.util.EncryptionUtils
import com.example.uniapp.util.FileStorageUtils
import com.example.uniapp.util.GlobalVariables

class LoginActivity : AppCompatActivity() {

    private val apiUrl = "${GlobalVariables.apiCommonUrl}users/userinfo"
    private val aesKey = "${GlobalVariables.AESKey}" // Replace with your actual key
    private lateinit var networkService: NetworkService

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.login_screen)

        networkService = NetworkService(apiUrl)

        val usernameEditText = findViewById<EditText>(R.id.usernameField)
        val passwordEditText = findViewById<EditText>(R.id.passwordField)
        val loginButton = findViewById<Button>(R.id.loginButton)

        loginButton.setOnClickListener {
            val username = usernameEditText.text.toString()
            val password = passwordEditText.text.toString()
            loginUser(username, password)
        }
    }

    @RequiresApi(Build.VERSION_CODES.O)
    private fun loginUser(username: String, password: String) {
        networkService.loginUser(username, password) { success, encryptedUserInfo ->
            if (success && encryptedUserInfo != null) {
                FileStorageUtils.saveToFile(this, "user_info.txt", encryptedUserInfo)
                val intent = Intent(this, HomepageProfessor::class.java)
                startActivity(intent)
                decryptUserInfo(encryptedUserInfo)
            } else {
                runOnUiThread {
                    Toast.makeText(this, "Login failed", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }

    @RequiresApi(Build.VERSION_CODES.O)
    private fun decryptUserInfo(encryptedUserInfo: String) {
        val decryptedUserInfo = EncryptionUtils.decrypt(encryptedUserInfo, aesKey)
        runOnUiThread {
            if (decryptedUserInfo != null) {
                Toast.makeText(this, "Decrypted User Info: $decryptedUserInfo", Toast.LENGTH_LONG).show()
            } else {
                Toast.makeText(this, "Decryption failed", Toast.LENGTH_SHORT).show()
            }
        }
    }
}

The AESKey is of 32 bytes so it is correct, and this code respects the encryption of the API.

New contributor

user26617702 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật