Hi all.
My app sends requests to the server and in this request some data is encrypted (for example device id, username, etc...)
It works fine since March 2020 and today one of my customer can't register with the server because string encrypted in my app on his LG X screen phone with Android 6.0.1 can't be decrypted on the server. It has never happened before. I tried all my phones with Android versions from 4 to 11 and all of them work fine with the same encrypt / decrypt procedures.
When I try to decrypt this string on the server (this is .NET 3.5) I'm getting the error Bad Data. I attached the error log from the server
and this is Decryption code on the server (VB.NET 3.5)
and this is a code in my B4A app
If I try to decrypt the same string with my B4A Decrypt code
I got the error
Thanks for your help.
My app sends requests to the server and in this request some data is encrypted (for example device id, username, etc...)
It works fine since March 2020 and today one of my customer can't register with the server because string encrypted in my app on his LG X screen phone with Android 6.0.1 can't be decrypted on the server. It has never happened before. I tried all my phones with Android versions from 4 to 11 and all of them work fine with the same encrypt / decrypt procedures.
When I try to decrypt this string on the server (this is .NET 3.5) I'm getting the error Bad Data. I attached the error log from the server
and this is Decryption code on the server (VB.NET 3.5)
B4X:
Public Function Decrypt(encryptedData As String) As String
Try
Dim result As String = ""
If encryptedData = "" Then
Return "-1"
End If
If IsIOS.ToLower = "yes" Then
result = IOSDecrypt(encryptedData)
Return result
End If
Dim buffer As Byte() = Convert.FromBase64String(encryptedData)
Dim des As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
des.IV = New Byte() {211, 5, 233, 24, 55, 166, 7, 88}
des.Key = ASCIIEncoding.UTF8.GetBytes("1234567890123456")
'This line where the error happens
result = Encoding.ASCII.GetString(des.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length))
des.Clear()
Return result
Catch ex As Exception
Functions.SaveError(ex, "encryptedData=" & encryptedData)
Return "-1"
End Try
End Function
and this is a code in my B4A app
B4X:
Sub Encrypt(dataToEncrypt As String ) As String
Try
Dim strPWD As String="1234567890123456"
If dataToEncrypt.Trim.Length=0 Then
Return dataToEncrypt
End If
Dim kg As KeyGenerator
Dim c As Cipher
Dim B64 As Base64
Dim bconv As ByteConverter
Dim data(0) As Byte
Dim iv(0) As Byte
iv = Array As Byte(211, 5, 233, 24, 55, 166, 7, 88) ' 16 bytes for AES
c.Initialize("DESEDE/CBC/PKCS5Padding")
c.InitialisationVector = iv
kg.Initialize("DESEDE")
kg.KeyFromBytes(bconv.StringToBytes(strPWD,"ASCII"))
data = bconv.StringToBytes(dataToEncrypt, "ASCII")
data = c.Encrypt(data, kg.Key, True)
Return B64.EncodeBtoS(data, 0, data.Length)
Catch
Log("Encrypt " & LastException)
Return "Error - Encryption failed."
End Try
End Sub
If I try to decrypt the same string with my B4A Decrypt code
B4X:
Sub Decrypt(encryptedData As String ) As String
Try
Dim strPWD As String="1234567890123456"
If encryptedData.Trim.Length=0 Then
Return encryptedData
End If
Dim kg As KeyGenerator
Dim c As Cipher
Dim B64 As Base64
Dim bconv As ByteConverter
Dim data(0) As Byte
Dim iv(0) As Byte
iv = Array As Byte(211, 5, 233, 24, 55, 166, 7, 88) ' 16 bytes for AES
c.Initialize("DESEDE/CBC/PKCS5Padding")
c.InitialisationVector = iv
kg.Initialize("DESEDE")
kg.KeyFromBytes(bconv.StringToBytes(strPWD,"ASCII"))
data = B64.DecodeStoB(encryptedData)
data = c.Decrypt(data, kg.Key, True)
Return bconv.StringFromBytes(data, "ASCII")
Catch
Log("Decrypt " & LastException)
Return "Error - Decrption failed."
End Try
End Sub
I got the error
B4X:
Error occurred on line: 81 (modCrypt)
javax.crypto.BadPaddingException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT
at com.android.org.conscrypt.NativeCrypto.EVP_CipherFinal_ex(Native Method)
at com.android.org.conscrypt.OpenSSLCipher$EVP_CIPHER.doFinalInternal(OpenSSLCipher.java:602)
at com.android.org.conscrypt.OpenSSLCipher.engineDoFinal(OpenSSLCipher.java:365)
at javax.crypto.Cipher.doFinal(Cipher.java:2055)
at anywheresoftware.b4a.agraham.encryption.CipherWrapper.doFinal(CipherWrapper.java:140)
at anywheresoftware.b4a.agraham.encryption.CipherWrapper.Decrypt(CipherWrapper.java:150)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:197)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:7288)
at android.view.View.performClickInternal(View.java:7258)
at android.view.View.access$4000(View.java:808)
at android.view.View$PerformClick.run(View.java:28019)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7615)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
Decrypt (Exception) java.lang.Exception: javax.crypto.BadPaddingException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT
Thanks for your help.