Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
Available now!
Buy at Amazon US or
Buy at Amazon UK



Articles

» Windows API reference
» Webcam streaming in VB.NET
» Remoting with firewalls
» RSA from first principles
» Key & MouseLogger in .NET
» Networking Resource Kit for .NET
» Migrating VB6 Winsock to VB.NET
» Migrating C++ sockets to C#
» RFC Reference guide
» COM Reference guide
» WMI Reference guide
» SQL stored procedures
» TCP & UDP port reference
» NET Framework reference
» Ethernet Type codes
» MAC address assignments
» DLL entry point reference
» Boost SQL performance
» Free SMS UK
» Free SMS Ireland
» Free SMS South Africa
» Internet Explorer

Contact us

VB.NET

Private Function GCD(ByRef nPHI As Double) As Double

        Dim x As Double

        Dim nE, y As Double

        Const N_UP As Integer = 99999999 'set upper limit for E

        Const N_LW As Integer = 10000000 'set lower limit for E

        On Error Resume Next

        Randomize()

        nE = Int((N_UP - N_LW + 1) * Rnd() + N_LW)

        Do

            x = nPHI Mod nE

            y = x Mod nE

            If y <> 0 And IsPrime(nE) Then

                GCD = nE

                Exit Function

            Else

                nE = nE + 1

            End If

        Loop

End Function

C#

private double GCD(double nPHI)

{

     double x;

     double nE,y;

     const int N_UP  = 99999999; //set upper limit for E

     const int N_LW  = 10000000; //set lower limit for E

     nE = Convert.ToInt64((N_UP - N_LW + 1) * _

           (new Random()).NextDouble() + N_LW);

     while(true)

     {

          x = nPHI % nE;

          y = x % nE;

          if (y != 0 && IsPrime(nE))

          {

             return nE;

          }

          else

          {

             nE++;

          }

     }

}

GCD generates a number that is relatively prime to (p-1) * (q – 1). This number must be in the order of 10’s of millions, and be prime.

The last function required to generate the public and private keys is the Euler function

VB.NET

Private Function Euler(ByRef E3 As Double, ByRef PHI3 As Double) As Double

        On Error Resume Next

        Dim v3, v1, u2, u1, u3, v2, q As Double

        Dim vv, z, t2, t1, t3, uu, inverse As Double

        u1 = 1

        u2 = 0

        u3 = PHI3

        v1 = 0

        v2 = 1

        v3 = E3

        Do Until (v3 = 0)

            q = Int(u3 / v3)

            t1 = u1 - q * v1

            t2 = u2 - q * v2

            t3 = u3 - q * v3

            u1 = v1

            u2 = v2

            u3 = v3

            v1 = t1

            v2 = t2

            v3 = t3

            z = 1

        Loop

        uu = u1

        vv = u2

        If (vv < 0) Then

            inverse = vv + PHI3

        Else

            inverse = vv

        End If

        Euler = inverse

    End Function

Page 4   Page 6



Google

Copyright 2019 Infinite Loop Ltd.