Generating Prime Numbers
Many public key algorithms depend on prime number, which are difficult to
factorize when multiplied together. This program creates the ones from 1 to
:
The following is the code:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Security.Cryptography;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Net;
using System.Threading;
public partial class _Default5 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button3_Click(object sender, EventArgs e)
{
findprimes = true;
goPrimes();
}
string primeList = "";
static ulong RecursiveModulo(ulong uBase, ulong uPower, ulong uDivisor)
{
ulong uAux;
if (uPower == 0) return 1;
//a^2c mod n = (ac mod n)2 mod n
else if (uPower % 2 == 0)
{
uAux = RecursiveModulo(uBase, uPower / 2, uDivisor);
return (uAux * uAux) % uDivisor;
}
//ac+1 mod n = (ac mod n) ยท a mod n
else return (RecursiveModulo(uBase, uPower - 1, uDivisor) * uBase) % uDivisor;
}
public static bool TestPrime(ulong uNumber, uint uIterations)
{
Random Rnd = new Random();
ulong uAux;
for (ulong i = 0; i < uIterations; ++i)
{
uAux = (ulong)(Rnd.Next()) % uNumber;
if (uAux == 0) uAux = uNumber - 1;
if (RecursiveModulo(uAux, uNumber - 1, uNumber) != 1)
{
return false;
}
}
return true;
}
bool findprimes = false;
public void goPrimes()
{
int j = 0;
// for (uint i = 2; i < Math.Pow(2, 32); i++)
for (uint i = 2; i < 5000; i++)
{
if (i == 2) tbMessage.Text= "\r\nPrime numbers:\r\n";
if (findprimes == false) { return; }
if (TestPrime(i, 2) == true && findprimes == true)
{
j++;
if (j % 10 == 0)
{
tbMessage.Text+= primeList + "\r\n";
primeList = "";
}
else primeList += Convert.ToString(i) + "\t";
}
System.Threading.Thread.Sleep(2);
}
}
}
|