Android Question Result encrypt changes according to device !!!

vecino

Well-Known Member
Licensed User
Longtime User
Hello, I have encountered a very strange problem.
I use some functions to encrypt texts that have always worked well.
The functions are copied from a message from these forums. They have been working correctly for years. But not now.
In some new devices (tablets) the results are different.
These are the functions:

B4X:
Sub Encrypt(dataToEncrypt As String ) As String
  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("1234567890123456","ASCII"))
  data = bconv.StringToBytes(dataToEncrypt, "ASCII")
  data = c.Encrypt(data, kg.Key, True)             
    '
  Return B64.EncodeBtoS(data, 0, data.Length)
End Sub

Sub Decrypt(encryptedData As String ) As String   
  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("1234567890123456","ASCII"))
  data = B64.DecodeStoB(encryptedData)
  data = c.Decrypt(data, kg.Key, True) 
   '
   Return bconv.StringFromBytes(data, "ASCII")
End Sub

Sub getEncoded( cCadena As String ) As String
    Dim data() As Byte
    Dim su As StringUtils
    Dim encoded As String
    '
    data = cCadena.GetBytes("ASCII")
    encoded = su.EncodeBase64(data)
    '
    Return (encoded)
End Sub

Sub getDecoded( cCadena As String ) As String 
    Dim data() As Byte
    Dim su As StringUtils
    Dim decoded As String     
    '
    data = su.DecodeBase64(cCadena)
    decoded = BytesToString(data, 0, data.Length, "ASCII")
    '
    Return (decoded) 
End Sub

This is the code I am using to test.

B4X:
    cHoInt = "Hi this Is a test."
    cUsInt = "This Is the second line."
    cPaInt = "And here the text ends."
    Log("cHoInt: "&cHoInt)
    Log("cUsInt: "&cUsInt)
    Log("cPaInt: "&cPaInt)

    cHoInt = uf.Encrypt(cHoInt)
    cUsInt = uf.Encrypt(cUsInt)
    cPaInt = uf.Encrypt(cPaInt)
    Log("cHoInt: "&cHoInt)
    Log("cUsInt: "&cUsInt)
    Log("cPaInt: "&cPaInt)
  
    cHoInt = uf.getEncoded(cHoInt)
    cUsInt = uf.getEncoded(cUsInt)
    cPaInt = uf.getEncoded(cPaInt)   
    Log("cHoInt: "&cHoInt)
    Log("cUsInt: "&cUsInt)
    Log("cPaInt: "&cPaInt)
  
    Log("<<<")

    cHoInt = uf.getDecoded(cHoInt)
    cUsInt = uf.getdecoded(cUsInt)
    cPaInt = uf.getdecoded(cPaInt)
    Log("cHoInt: "&cHoInt)
    Log("cUsInt: "&cUsInt)
    Log("cPaInt: "&cPaInt)

    cHoInt = uf.Decrypt(cHoInt)
    cUsInt = uf.dEcrypt(cUsInt)
    cPaInt = uf.dEcrypt(cPaInt)
    Log("cHoInt: "&cHoInt)
    Log("cUsInt: "&cUsInt)
    Log("cPaInt: "&cPaInt)

And this is the result with one tablet and another.

cHoInt: Hi this Is a test.
cUsInt: This Is the second line.
cPaInt: And here the text ends.
cHoInt: 4H1xC4N9yngRoynRep9L2/OMePLBzbfQ
cUsInt: 4hDzX80C+KrJ4BTCPmp6NQzN0VKqnhjfakBF6kTM38I=
cPaInt: lDsPRRRH3CbRqfKd/qVFH/P2WPVRgHda
cHoInt: NEgxeEM0Tjl5bmdSb3luUmVwOUwyL09NZVBMQnpiZlE=
cUsInt: NGhEelg4MEMrS3JKNEJUQ1BtcDZOUXpOMFZLcW5oamZha0JGNmtUTTM4ST0=
cPaInt: bERzUFJSUkgzQ2JScWZLZC9xVkZIL1AyV1BWUmdIZGE=
<<<
cHoInt: 4H1xC4N9yngRoynRep9L2/OMePLBzbfQ
cUsInt: 4hDzX80C+KrJ4BTCPmp6NQzN0VKqnhjfakBF6kTM38I=
cPaInt: lDsPRRRH3CbRqfKd/qVFH/P2WPVRgHda
cHoInt: Hi this Is a test.
cUsInt: This Is the second line.
cPaInt: And here the text ends.

cHoInt: Hi this Is a test.
cUsInt: This Is the second line.
cPaInt: And here the text ends.
cHoInt: 8LqUm4ka+bZV97Ht47vPUMxFi+IOCOom
cUsInt: equI+tGF8Ekf4J3UuLsPRGmCny4+iOTxU+jz+mz4Rnk=
cPaInt: yS9U2DuvH5pLDMJ6Rj6zSYF/0EwYkxsM
cHoInt: OExxVW00a2ErYlpWOTdIdDQ3dlBVTXhGaStJT0NPb20=
cUsInt: ZXF1SSt0R0Y4RWtmNEozVXVMc1BSR21Dbnk0K2lPVHhVK2p6K216NFJuaz0=
cPaInt: eVM5VTJEdXZINXBMRE1KNlJqNnpTWUYvMEV3WWt4c00=
<<<
cHoInt: 8LqUm4ka+bZV97Ht47vPUMxFi+IOCOom
cUsInt: equI+tGF8Ekf4J3UuLsPRGmCny4+iOTxU+jz+mz4Rnk=
cPaInt: yS9U2DuvH5pLDMJ6Rj6zSYF/0EwYkxsM
cHoInt: Hi this Is a test.
cUsInt: This Is the second line.
cPaInt: And here the text ends.
Please, someone who enlightens my mind.
 

vecino

Well-Known Member
Licensed User
Longtime User
Hello, it can not be done, that's the problem. It does not generate the same results. Shows invalid data errors. The program crashes.
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Hello, here is the test project.

This is the result with a tablet:

B4X:
Logger connected to:  samsung SM-T805
--------- beginning of main
--------- beginning of system
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
>>> Encrypt...
cText: Hi this Is a test.
cText: 8LqUm4ka+bZV97Ht47vPUMxFi+IOCOom
cText: OExxVW00a2ErYlpWOTdIdDQ3dlBVTXhGaStJT0NPb20=
<<< Decrypt...
cText: 8LqUm4ka+bZV97Ht47vPUMxFi+IOCOom
cText: Hi this Is a test.
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **

This is the result with another tablet:

B4X:
Logger connected to:  unknown miTab LIVE
--------- beginning of /dev/log/main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
>>> Encrypt...
cText: Hi this Is a test.
cText: 4H1xC4N9yngRoynRep9L2/OMePLBzbfQ
cText: NEgxeEM0Tjl5bmdSb3luUmVwOUwyL09NZVBMQnpiZlE=
<<< Decrypt...
cText: 4H1xC4N9yngRoynRep9L2/OMePLBzbfQ
cText: Hi this Is a test.
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
 

Attachments

  • testcrypt.zip
    9 KB · Views: 200
Upvote 0

KMatle

Expert
Licensed User
Longtime User
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Hello, thanks for the information.
Can you tell me how is that?
I only copied and pasted these functions. I do not know the possible parameters.
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
I have made these changes, I do not know if it is right to make them that way.

B4X:
iv = Array As Byte(211, 5, 233, 24, 55, 166, 7, 88,  211, 5, 233, 24, 55, 166, 7, 88) ' 16 bytes for AES
c.Initialize("AES/CBC/PKCS5Padding")     
kg.Initialize("AES")

The result is also not valid, different data are obtained in different devices.

>>> Encrypt...
cText: Hi this Is a test.
cText: 8LqUm4ka+bZV97Ht47vPUMxFi+IOCOom
cText: OExxVW00a2ErYlpWOTdIdDQ3dlBVTXhGaStJT0NPb20=
<<< Decrypt...
cText: 8LqUm4ka+bZV97Ht47vPUMxFi+IOCOom
cText: Hi this Is a test.
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **



--------- beginning of system
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
>>> Encrypt...
cText: Hi this Is a test.
cText: Br7vnGS+yj6VLksyuQu3qQgvKoS3mHOeurSpit1Yyl8=
cText: QnI3dm5HUyt5ajZWTGtzeXVRdTNxUWd2S29TM21IT2V1clNwaXQxWXlsOD0=
<<< Decrypt...
cText: Br7vnGS+yj6VLksyuQu3qQgvKoS3mHOeurSpit1Yyl8=
cText: Hi this Is a test.
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
The two getDecoded and getEncoded subs do not do anything useful.
Hello, it's just to make the key more complicated.
What is the tablet version?
Samsung galaxy tab S SM-T805 (Android 6.0.1)
Try the project you've uploaded with a 24 byte key. Do you still get different results? Can you post them?
Now the result is the same, why? :eek:

I have changed this code:
kg.KeyFromBytes(bconv.StringToBytes("1234567890123456","ASCII"))
For this other code:
kg.KeyFromBytes(bconv.StringToBytes("123456789012345678901234","ASCII"))

What explanation does this have?

B4X:
Logger connected to:  samsung SM-T805
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
>>> Encrypt...
cText: Hi this Is a test.
cText: n51bXuh/fpUQy4S+dFr6EUOM6NQqQ50g
cText: bjUxYlh1aC9mcFVReTRTK2RGcjZFVU9NNk5RcVE1MGc=
<<< Decrypt...
cText: n51bXuh/fpUQy4S+dFr6EUOM6NQqQ50g
cText: Hi this Is a test.
** Activity (main) Resume **

B4X:
Logger connected to:  unknown miTab LIVE
--------- beginning of /dev/log/main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
>>> Encrypt...
cText: Hi this Is a test.
cText: n51bXuh/fpUQy4S+dFr6EUOM6NQqQ50g
cText: bjUxYlh1aC9mcFVReTRTK2RGcjZFVU9NNk5RcVE1MGc=
<<< Decrypt...
cText: n51bXuh/fpUQy4S+dFr6EUOM6NQqQ50g
cText: Hi this Is a test.
** Activity (main) Resume **
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Hello, it's just to make the key more complicated.
It doesn't add any real complexity. Just makes the code a bit more complicated.

Now the result is the same, why?
The triple DES key size should be 16 or 24 bytes. Seems like 24 bytes is more common.
I guess that there is an issue with older versions of Android and 16 bytes keys with triple DES.
 
Upvote 0
Top