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

1.Windows API reference
2.HTML to WML Converter
3.Webcam streaming in VB.NET
4.Remoting with firewalls
5.RSA from first principles
6.Key & MouseLogger in .NET
7.Networking Resource Kit for .NET
8.Automatic Reboot with .NET
9.XAML Schema
10.Migrating VB6 Winsock to VB.NET
11.Migrating C++ sockets to C#
12.RFC Reference guide
13.Lingua - Localization webservice
14.COM Reference guide
15.WMI Reference guide
16.SQL stored procedures
17.TCP & UDP port reference
18..NET Framework reference
19.Ethernet Type codes
20.IP to country webservice
21.MAC address assignments
22.DLL entry point reference
23.WHOIS server list
24. Turing Numbers
25. Boost SQL performance
26. Progress Bar in ASP.NET
27. OleDb WebService
27. Internet Explorer

Contact us

This generates two prime numbers that are large enough to create keys that are in the order of 10’s of millions. It also checks that the numbers are relatively prime to (p-1) * (q – 1)

Note that the functions IsPrime, GCD and Euler have not yet been implemented

Firstly, create the IsPrime function.

VB.NET

Private Function IsPrime(ByRef lngNumber As Double) As Boolean

        On Error Resume Next

        Dim lngCount As Double

        Dim lngSqr As Double

        Dim x As Double

        lngSqr = Int(System.Math.Sqrt(lngNumber)) ' Get the int square root

        If lngNumber < 2 Then

            IsPrime = False

            Exit Function

        End If

        lngCount = 2

        IsPrime = True

        If lngNumber Mod lngCount = 0 Then

            IsPrime = False

            Exit Function

        End If

        lngCount = 3

        For x = lngCount To lngSqr Step 2

            If lngNumber Mod x = 0 Then

                IsPrime = False

                Exit Function

            End If

        Next

End Function

C#

private bool IsPrime(double lngNumber)

{

    double lngCount;

    double lngSqr;

    double x;

    lngSqr = Convert.ToInt64(System.Math.Sqrt(lngNumber));

    if (lngNumber < 2)

    {

            return false;

    }

    lngCount = 2;                  

    if (lngNumber % lngCount == 0)

    {

            return false;

    }

    lngCount = 3;

    for (x=lngCount;x<lngSqr;x+=2)

    {

            if (lngNumber % x == 0)

            {

                    return false;

            }

    }

    return true;

}

This function counts from 2 to 3 to the square root of lngNumber in steps of 2. If at any point, an even divisor is found the function returns false.

For instance to test the number 101, the function would divide 101 by 2,3,5,7 and 9.

Now, to implement Euclid's greatest common divisor algorithm (GCD) function

Page 3   Page 5



Google

© 2008 Free SMS| Sage Line 50 Component