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

This generates two prime numbers that are large enough to create keys that are in the order of 10s 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

Copyright 2015 Open Merchant Account Ltd.