Android Question Problem to calculate in Hex

Lello1964

Well-Known Member
Licensed User
Longtime User
I have to calculate in hex this value :

Generator = 0x1D
Host_Rnd = 0x83
Modulus = 0x35

HostInerKey = ( Generator ^ Host_Rnd ) mod Modulus
must be : 0x2B

i have tryed
B4X:
Dim Generator As Long  = 29    ' (0x1D)
Dim Host_Rnd As Long = 131  ' (0X83)
Dim Modulus  As Long   = 53   ' ( 0x35)

Dim TmpPower As Long =  Power(Generator ,Host_Rnd)
Dim HostInterKey As Int  =  TmpPower Mod Modulus

result is : 33 (0x21)

but correct result must be : 43 (0x2B)

where is my error ?
 
Last edited:

emexes

Expert
Licensed User
My first thought is that 29^131 is way larger than a 2^64 Long, and thus the Power function is probably superimposing a Mod 2^64 that messes things up.

(written using 2^64 instead of 2^63-1 to make it visually easier to see the problem)

But when I do the calculation "manually" using Big Number Calculator I get 29. Ditto for calculating the Power multiply-by-multiply and doing the Mod after each multiply. How reliable is the "must be : 43" ?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
I've just tried it too, and also got 29. I also tried the BigNumbers Library which contains a function to do that calculation, with the same result.
 
Upvote 0

Lello1964

Well-Known Member
Licensed User
Longtime User
This is manual information


ssp.jpg
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
It is switching the parameters around in the calculations, so the host is using the Slave generated Inter Key and Host_Rnd to validate and the Device is using the HostInterKey and Slave_RND, both of which give 0x2B or 43
 
Upvote 0

emexes

Expert
Licensed User
I think you'll still have to do the Power-Mod calculation step-by-step to avoid exceeding capacity of Long. On bright side, you only need an integer type big enough to hold the maximum product of two unsigned 8-bit integers and so an Int will do the job.
B4X:
Dim Generator As Int = 0x1D
Dim Host_Rnd As Int = 0x53
Dim Modulus As Int = 0x35

Dim HostInterKey As Int  =  1
For I = 1 To Host_Rnd
    HostInterKey = (HostInterKey * Generator) Mod Modulus
Next

Log(HostInterKey)
 
Last edited:
Upvote 0

Lello1964

Well-Known Member
Licensed User
Longtime User
I think you'll still have to do the Power-Mod calculation step-by-step to avoid exceeding capacity of Long. On bright side, you only need an integer type big enough to hold the maximum product of two unsigned 8-bit integers and so an Int will do the job.
B4X:
Dim Generator As Int = 0x1D
Dim Host_Rnd As Int = 0x53
Dim Modulus As Int = 0x35

Dim HostInterKey As Int  =  1
For I = 1 To Host_Rnd
    HostInterKey = (HostInterKey * Generator) Mod Modulus
Next
Log(HostInterKey)

Yes, this is the right solution.
 
Upvote 0
Top