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

 Dim p, q, PHI As Double

 Dim Key(3) As Double

C#

 double p,q,PHI;

 double[] Key = new double[3];

Click on cmdKeyGen and enter the following code

VB.NET

Private Sub cmdKeyGen_Click(ByVal eventSender As System.Object, ByVal _ eventArgs As System.EventArgs) Handles cmdKeyGen.Click

        Dim D, E1, N As Double

        Const PQ_UP As Short = 9999 'set upper limit of random number

        Const PQ_LW As Short = 3170 'set lower limit of random number

        Const KEY_LOWER_LIMIT As Integer = 10000000 'set for 64bit minimum

        p = 0 : q = 0

        Randomize()

        Do Until D > KEY_LOWER_LIMIT 'makes sure keys are 64bit minimum

            Do Until IsPrime(p) And IsPrime(q)

                p = Int((PQ_UP - PQ_LW + 1) * Rnd() + PQ_LW)

                q = Int((PQ_UP - PQ_LW + 1) * Rnd() + PQ_LW)

            Loop

            N = p * q

            PHI = (p - 1) * (q - 1)

            E1 = GCD(PHI)

            D = Euler(E1, PHI)

        Loop

        Key(1) = E1

        Key(2) = D

        Key(3) = N

        txtP.Text = CStr(p) 'P

        txtQ.Text = CStr(q) 'Q

        txtPhi.Text = CStr(PHI) 'PHI

        txtE.Text = CStr(Key(1)) 'E

        txtD.Text = CStr(Key(2)) 'D

        txtN.Text = CStr(Key(3)) 'N

End Sub

C#

private void cmdKeyGen_Click(object sender, System.EventArgs e)

{

    double D = 0,E1 = 0,N = 0;

    const short PQ_UP  = 9999; //set upper limit of random number

    const short PQ_LW  = 3170; //set lower limit of random number

    const int KEY_LOWER_LIMIT = 10000000; //set for 64bit minimum

    p = 0; q = 0;

    while (!(D > KEY_LOWER_LIMIT)) //makes sure keys are 64bit minimum

    {

            while (!(IsPrime(p) && IsPrime(q)))

            {

                    p = Convert.ToInt64((PQ_UP - PQ_LW + 1) * _

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

                    q = Convert.ToInt64((PQ_UP - PQ_LW + 1) * _

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

            }

            N = p * q;

            PHI = (p - 1) * (q - 1);

            E1 = GCD(PHI);

            D = Euler(E1, PHI);

    }

    Key[1] = E1;

    Key[2] = D;

    Key[3] = N;

    txtP.Text = p.ToString();

    txtQ.Text = q.ToString();

    txtPhi.Text = PHI.ToString();

    txtE.Text = Key[1].ToString();

    txtD.Text = Key[2].ToString();

    txtN.Text = Key[3].ToString();

}

Page 2   Page 4



Google

Copyright 2017 Infinite Loop Ltd.