I am trying to encrypt the password using AES algorithm in android.The backend is done using the CryptojS library and it can not be changed.I need to send the encrypted password to the backend.I have tried with the below code:
fun encryptNew(key: String, value: String): String? {
try {
val secretKey: SecretKey = SecretKeySpec(
Base64.decode(
key.toByteArray(),
Base64.NO_WRAP
), "AES"
)
val iv: AlgorithmParameterSpec = IvParameterSpec(
Base64.decode(
key.toByteArray(),
Base64.NO_WRAP
)
)
val cipher =
Cipher.getInstance("AES/CBC/PKCS5Padding")
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv)
return String(
Base64.encode(
cipher.doFinal(value.toByteArray(charset("UTF-8"))),
Base64.NO_WRAP
)
)
} catch (e: java.lang.Exception) {
Log.d("1111","2222")
e.printStackTrace()
}
return null
}
But it returns null and showing exception as java.security.invalidkeyexception: unsupported key size: 20 bytes. The code for backend is
const encrypt = CryptoJS.AES.encrypt(str, secret);
return encrypt.toString();
Can anyone help me on this? Note:secret key is to be 32 byte value is the password which is needed to be encypted