I try to cipher and decipher a string in the simplest way but it does not work ...
After few hours of research I try to post my problem here. I have reduce the problem at its minimum but it still doesn't work and I don't understand the error.
Here is my code :
class MainActivity : AppCompatActivity()
{
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val password = "passwordpassword"
val plainText = "tototiti123"
val encryptText = encrypt(plainText, password)
text.text = decrypt(encryptText, password)
}
private fun encrypt(plainText: String, password: String): String
{
val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
val key = SecretKeySpec(password.toByteArray(charset("UTF-8")), "AES")
cipher.init(Cipher.ENCRYPT_MODE, key)
return String(cipher.doFinal(plainText.toByteArray(charset("UTF-8"))))
}
private fun decrypt(encrypted: String, password: String): String
{
val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
val key = SecretKeySpec(password.toByteArray(charset("UTF-8")), "AES")
val r = SecureRandom()
r.setSeed(r.generateSeed(16))
val byteIV = ByteArray(16)
r.nextBytes(byteIV)
cipher.init(Cipher.DECRYPT_MODE, key, IvParameterSpec(byteIV))
return String(cipher.doFinal(encrypted.toByteArray(charset("UTF-8"))))
}
}
and here is my error :
AndroidRuntime: FATAL EXCEPTION: main
Process: com.cryptapp, PID: 17710
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cryptapp/com.cryptapp.MainActivity}: javax.crypto.IllegalBlockSizeException: error:1e00007b:Cipher functions:OPENSSL_internal:WRONG_FINAL_BLOCK_LENGTH
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: javax.crypto.IllegalBlockSizeException: error:1e00007b:Cipher functions:OPENSSL_internal:WRONG_FINAL_BLOCK_LENGTH
at com.android.org.conscrypt.NativeCrypto.EVP_CipherFinal_ex(Native Method)
at com.android.org.conscrypt.OpenSSLCipher$EVP_CIPHER.doFinalInternal(OpenSSLCipher.java:570)
at com.android.org.conscrypt.OpenSSLCipher.engineDoFinal(OpenSSLCipher.java:351)
at javax.crypto.Cipher.doFinal(Cipher.java:1741)
at com.cryptapp.MainActivity.decrypt(MainActivity.kt:47)
at com.cryptapp.MainActivity.onCreate(MainActivity.kt:23)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
Does anyone know what is going wrong with my code ?
Thank you per advance.