<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Population</title>
</head>

<body>



<p align="left"><font size="6" color="#FF0000"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
RSA&nbsp; Encryption&amp;Decryption</b></font></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A.<span lang="en-ca">First</span> Edition</font></b></pre>
</div>
<div align="left">
  <pre><b>This is the <span lang="en-ca"> first </span>edition of RSA encryption and decryption program. and most code are from text of </b></pre>
</div>
<div align="left">
  <pre><b>discrete mathematics.</b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Idea of program</b></font></span></pre>
</div>
<div align="left">
  <pre><b>1¡£ Basic idea: </b></pre>
</div>
<div align="left">
  <pre><b>Encryption is actually using modular theory and the theory is very profound though the algorithm is comparatively</b></pre>
</div>
<div align="left">
  <pre><b>simple. The only function by me is &quot;inverse&quot; which derives from Euclidean Algorithms. </b></pre>
</div>
<div align="left">
  <pre><b>2¡£ Program design: </b></pre>
</div>
<div align="left">
  <pre><b>There are some basic algorithm related with modular, such as inverse, GCD, exponent modular etc.</b><span lang="en-ca"><b> </b></span></pre>
</div>
<div align="left">
  <blockquote>
    <blockquote>
  <pre><b> </b></pre>
    </blockquote>
  </blockquote>
  <pre><b>4¡£ Further improvement£º</b></pre>
  <pre><b>I plan to find two very big prime number to use as &quot;Public Key&quot;, so I write a simple program to search for </b></pre>
  <pre><b>such big primes. <a href="#prime">see here</a>:</b></pre>
  <pre><span lang="en-ca"><b>	</b></span></pre>
</div>
<pre>#include &lt;iostream&gt;

using namespace std;

typedef unsigned long ulong;

const ulong Prime1 = 43;
const ulong Prime2 = 59;
const ulong DefaultExp = 13;


enum DivName
{Quotient, Remainder};

class RSA
{
private:
	int counter;
	int exp;
	ulong enModular;
	ulong deModular;
	ulong deExp;
	ulong temp[100][2];
	bool testExp(ulong e);
	void initialize(ulong e= DefaultExp);
public:
	RSA();
	RSA(ulong e);
	void setExp(ulong e);
	ulong inverse(ulong number, ulong mod);
	ulong GCD(ulong number, ulong mod);
	ulong modularExp(ulong base, ulong exp, ulong mod);
	ulong encryption(ulong input);
	ulong decryption(ulong input);
};

int main()
{
	int a, i=0;
	RSA R;
	while (i&lt;5)
	{
		cout&lt;&lt;endl;
		cin&gt;&gt;a;
		//cout&lt;&lt;modularExp(2, a, b);
		cout&lt;&lt;R.decryption(a)&lt;&lt;endl;
		i++;
	}
	return 0;
}

void RSA::setExp(ulong e)
{
	if (!testExp(e))
	{
		cout&lt;&lt;&quot;Input exponent is not relative prime to modular!\n&quot;;
		cout&lt;&lt;&quot;Default exponent is used!\n&quot;;
		exp = DefaultExp;
	}
	else
	{
		exp = e;	
	}
	deExp = inverse(exp, deModular);
}

ulong RSA::encryption(ulong input)
{
	return modularExp(input, exp, enModular);
}

ulong RSA::decryption(ulong input)
{
	return modularExp(input, deExp, enModular);
}

void RSA::initialize(ulong e)
{
	counter =0;
	enModular = Prime1* Prime2;
	deModular = (Prime1 -1)*(Prime2 -1);
	setExp(e);	
}

RSA::RSA(ulong e)
{
	initialize(e);
}


bool RSA::testExp(ulong e)
{
	return GCD(e, deModular)==1;
}

RSA::RSA()
{
	initialize();
}


ulong RSA::modularExp(ulong base, ulong exp, ulong mod)
{
	ulong shifts = exp; //in order to shift to test each bit
	ulong test;   //mask and also temp result to see bit value
	ulong power; //the base modular 
	ulong result =1;  //final result
	power = base % mod;

	for (int i=0; i&lt; 32; i++)
	{
		test =1;
		test &amp;= shifts;  //bit-and assignment
		if (test)  //if it is 1, then result times power
		{
			result *= power;
			result %= mod;
		}
		power *= power;
		power %= mod;
		shifts&gt;&gt;= 1;
		if (shifts==0)
		{
			break;
		}
	}
	return result;
}



ulong RSA::GCD(ulong number, ulong mod)
{
	div_t result;
	result = div(number, mod);
	if (result.rem != 0)
	{
		temp[counter][Quotient] = result.quot;
		temp[counter][Remainder] = result.rem;
		counter++;
		return GCD(mod, result.rem);
	}
	else
	{
		return temp[counter-1][Remainder];
	}
}

ulong RSA::inverse(ulong number, ulong mod)
{
	counter =0; //initialize 
	if (GCD(number, mod)!=1)
	{
		cout&lt;&lt;&quot;The two number are not relatively prime!&quot;;
	}
	else
	{
		//don't call GCD as it is already done in &quot;if&quot;, this is really a nasty way!!!
		counter--;
		ulong scale =0;
		ulong divident, divisor;//these are quotient for 
		divident = 1;
		divisor = temp[counter][Quotient];
		
		while (counter&gt;0)
		{
			scale = -1 * divisor;
			divisor = temp[counter-1][Quotient]* scale - divident;
			divident = scale;
			counter--;
		}
		return divident;
	}
	return -1;
}
</pre>
<pre>
</pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>

<p>¡¡          


</p>

<p>¡¡</p>

<p><font color="#FF0000"><a name="prime"></a>The following is a small silly 
program to search for all prime numbers. It use the simplest algorithms, so</font></p>

<p><font color="#FF0000">it runs hours to finish. I simply save every prime 
number I find in an array and future number just test </font></p>

<p><font color="#FF0000">if it is divisable by the prime in array. And as the 
factor of a composite is less or equal to its square </font></p>

<p><font color="#FF0000">root, so it can save some time.</font></p>

<pre>#include &lt;iostream&gt;
#include &lt;cmath&gt;

using namespace std;

typedef unsigned long ulong;

const ulong MaxNum = 0xFFFFFFFE;

ulong array[59999999];

ulong counter =0;

bool test(ulong number)
{
	ulong temp;
	temp = sqrt(number);
	for (int i =0; i&lt;counter; i++)
	{		
		if (array[i]&gt;temp+1&amp;&amp;number%array[i]==0)
		{
			return false;
		}
	}
	array[counter] = number;
	counter++;
	return true;
}

void addPrime(ulong prime)
{
	array[counter] = prime;
	counter++;
}

void initialize()
{
	addPrime(2);
	//addPrime(3);
}

void display()
{
	for (int i=0; i&lt;counter; i++)
	{
		cout&lt;&lt;&quot;Prime no.&quot;&lt;&lt;i&lt;&lt;&quot; is &quot;&lt;&lt;array[i]&lt;&lt;endl;
	}
}
	


int main()
{
	ulong i =2;
	initialize();
	
	while (i&lt;MaxNum)
	{
		test(i);
	
		i++;
	}
	display();

	return 0;
}
			



</pre>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                   
&nbsp;&nbsp;&nbsp; <a href="WhoAmI.htm">                  







                       <img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<a href="index.htm"><img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a>       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                         
<img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">          


</p>

</body>

</html>