Android Question odd number of characters [solved]

JDS

Active Member
Licensed User
Longtime User
Hi,

I've got the code below. At an certain point StringEncrypt gets called and the last line should execute. However at that point I get the error:

java.lang.Exception: hex string has odd number of characters
at anywheresoftware.b4a.agraham.byteconverter.ByteConverter.HexToBytes(ByteConverter.java:254)


The value of b64str at that moment is: 1204JDS$oftadMin#@2696c99692308015

The strange thing is that it only happens in an released APK. When I run and debug with Debug (rapid) everything goes well.
It also happens on an customers older device (Samsung Xcover). My own Sony Xperia hasn't this problem.

Am I missing something?

B4X:
Sub GetDeviceId As String
    Dim r As Reflector
    Dim p As Phone
    Dim id As String
    Dim Api As Int
    Api = r.GetStaticField("android.os.Build$VERSION", "SDK_INT")
    If Api < 9 Then
        'Old device
        id = p.GetSettings("android_id")
        Return id
    Else
        'New device
        id = r.GetStaticField("android.os.Build", "SERIAL")
        If id.ToLowerCase = "unknown" Then
            id = p.GetSettings("android_id")
        End If
        Return id
    End If
end sub

Sub StringEncrypt As String
    Dim text, b64str As String
    Dim bConv As ByteConverter
    Dim key(0) As Byte
    Dim data(0) As Byte
    Dim sigdata(0) As Byte
 
      b64str = (1204&"JDS$oftadMin#@"&GetDeviceId)
 
    key = bConv.HexToBytes( b64str )
 

udg

Expert
Licensed User
Longtime User
Hi,

shouldn't it be Dim Key() as byte?
I'm not sure, but key(0) means an array made up of a single element, so an odd number.

udg
 
Upvote 0

JDS

Active Member
Licensed User
Longtime User
problem solved :D
The problem was the line
B4X:
key = bConv.HexToBytes( b64str )

b64str isnt Hex but string.

When I changed is to

B4X:
key = bConv.StringToBytes( b64str,"UNICODE" )

all is good. Damn took a while but I'm happy now. I've read the line a 100 times and never saw it.
 
Upvote 0
Top