B4J Question Diffie-Hellman

schimanski

Well-Known Member
Licensed User
Longtime User
I try to make a deffie-hellman-sub to exchange secret keys between two devices. Does anybody know, if the syntax for the following calculation is correct? It builds the foundation for the calculations..

upload_2016-6-29_12-23-47.png



B4X:
y=Power(g,x) mod p


Thanks for help..
 

schimanski

Well-Known Member
Licensed User
Longtime User
I have tried something and the following code is able to create two same keys. Bit it doesn't work for all prime numbers:

B4X:
Sub Process_Globals
    Dim p As Int = 11    'Primzahl'
    Dim g As Int = 7    'Ganze Zahl < Primzahl'
   
    Dim A As Long
    Dim B As Long
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    Dim xAlice As Int = Rnd(1, p-1)      'Zufallszahl Alice < Primzahl
    Dim xBop As Int = Rnd(1,p-1)        'Zufallszahl Bop < Primzahl'
    Log("xAlice:" & xAlice)
    Log("xBop:" & xBop)
   
    A=Power(g,xAlice) mod p
    Log("A:" & A)
    B=Power(g,xBop) mod p
    Log("B:" & B)
   
    Dim sA As Long
    sA=Power(B,xAlice) mod p            'Gemeinsamer Schlüssel'
    Log("Key Alice:" & sA)
   
    Dim sB As Long
    sB=Power(A,xBop) mod p
    Log("Key Bop: " & sB)            'Gemeinsamer Schlüssel'
End Sub

Perhaps there is someone who is expert in cryptography.
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
@Erel: I use a server-solution to connect the devices for an instant messenger-system. The algorithm seems to be simple, but they are only save, if the prime-numbers and numbers are big enough. Don't know, how to do it, without implementing it by myself...
 
Upvote 0
Top