<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.2.5 Edition</font></b></pre>
</div>
<div align="left">
  <pre><b>This is the <span lang="en-ca">second </span>edition of RSA encryption and decryption program only with a slight difference.</b></pre>
</div>
<div align="left">
  <pre><b>I only add Chinese remainder method.</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>Chinese remainder method takes two int array plus its size as params. The algorithms is very simple and it is in text. First</pre>
</div>
<div align="left">
  <pre>you calculate the total product of all modulos, then for each mod, you simply exclude it and then calculate the inverse of that </pre>
</div>
<div align="left">
  <pre>modulos. I don't know how to express it. But it is simple when you see it.</pre>
</div>
<div align="left">
  <pre><b>2。 Program design: </b></pre>
</div>
<div align="left">
  <pre><b>I only want to make a demo how to use the functions.</b></pre>
</div>
<div align="left">
  <blockquote>
    <blockquote>
  <pre><b> </b></pre>
    </blockquote>
  </blockquote>
</div>
<pre>　</pre>
<pre>　</pre>
<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;
	long divident;
	long divisor;
	ulong enModular;
	ulong deModular;
	ulong deExp;
	ulong gcd;
	long temp[100][2];
	bool testExp(ulong e);
	void initialize(ulong e= DefaultExp);
	void internalCalculate(long number, long mod);
public:
	RSA();
	RSA(ulong e);
	void setExp(ulong e);
	void linearCombination(long number, long mod);
	long inverse(long number, long mod);
	ulong GCD(long number, long mod);
	ulong modularExp(ulong base, ulong exp, ulong mod);
	ulong encryption(ulong input);
	ulong decryption(ulong input);
	int chineseRemainder(int size, int* number, int* mod);
};

int main()
{
	
	int num1[3] ={2,3,4};
	int mod1[3] ={3,5,11};
	int num2[3]={5,6,7};
	int mod2[3]={45,49,52};
	
	RSA R;
	

	cout&lt;&lt;&quot;\nQ1 is:&quot;;
	R.linearCombination(3457, 4669);
	cout&lt;&lt;&quot;\nQ2 is &quot;&lt;&lt;R.inverse(144, 233);
	cout&lt;&lt;&quot;\nQ10a is&quot;&lt;&lt;R.chineseRemainder(3, num1, mod1);
	cout&lt;&lt;&quot;\nQ10b is&quot;&lt;&lt;R.chineseRemainder(3, num2, mod2);
	cout&lt;&lt;&quot;\nQ11 is&quot;&lt;&lt;R.modularExp(17, 23, 131);
	cout&lt;&lt;&quot;\nDecrpt\n&quot;;
	cout&lt;&lt;&quot;0667 = &quot;&lt;&lt;R.decryption(667);
	cout&lt;&lt;&quot;\t 1947 = &quot;&lt;&lt;R.decryption(1947);
	cout&lt;&lt;&quot;\t 0671 = &quot;&lt;&lt;R.decryption(671);</pre>
<pre>	
	return 0;
}

int RSA::chineseRemainder(int size, int * number, int* mod)
{
	int M =1;
	int result =0;
	for (int i=0; i&lt; size; i++)
	{
		M *= mod[i]; //M is the product of all mod
	}
	for (i=0; i&lt; size; i++)
	{
		result += number[i] * M / mod[i] * inverse(M/mod[i], mod[i]);
	}
	return result;
}





void RSA::linearCombination(long number, long mod)
{

	internalCalculate(number, mod);
	cout&lt;&lt;&quot;\nThe linear combination is: &quot;&lt;&lt;number&lt;&lt;&quot; x &quot;&lt;&lt;divident&lt;&lt;&quot; - &quot;;
	cout&lt;&lt;&quot; &quot;&lt;&lt;divisor&lt;&lt;&quot; x &quot;&lt;&lt;mod&lt;&lt;&quot; = &quot;&lt;&lt;gcd&lt;&lt;endl; 
}

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(long number, long mod)
{
	div_t result;
	long tempDivident, tempDivisor;
	tempDivident = number;
	tempDivisor = mod;
	counter = 0;

	result = div(tempDivident, tempDivisor);
	
	//if they are not relative prime
	if (result.rem == 0)
	{
		temp[counter][Quotient] = result.quot;
		temp[counter][Remainder] = result.rem;
		counter++;
		gcd = mod;
		return gcd;
	}
	
	while (result.rem != 0)
	{			
		temp[counter][Quotient] = result.quot;
		temp[counter][Remainder] = result.rem;
		counter++;
		tempDivident = tempDivisor;
		tempDivisor = result.rem;	
		result = div(tempDivident, tempDivisor);
	}
	gcd = temp[counter-1][Remainder];
	return gcd;

}

void RSA::internalCalculate(long number, long mod)
{
	long scale =0;
	GCD(number, mod);
	//these are quotient for 
	counter--;
	if (counter ==0&amp;&amp;temp[counter][Remainder] ==0)//this is mod|number!!!
	{
		divident = 1;
		divisor = divisor =temp[counter][Quotient] -1;
		return;
	}
	divident = 1;
	divisor = temp[counter][Quotient];
	
	while (counter&gt;0)
	{
		scale = -1 * divisor;
		divisor = temp[counter-1][Quotient]* scale - divident;
		divident = scale;
		counter--;
	}
}


long RSA::inverse(long number, long mod)
{
	//counter =0; //initialize 

	internalCalculate(number, mod);
	if (gcd == 1)
	{
		

		return divident;
	}
	else
	{
		cout&lt;&lt;&quot;\nThese two number are not relative prime!&quot;&lt;&lt;endl;
		return 0;
	}
}


			
</pre>
<pre><font color="#FF0000"><a name="Result"></a>The following is result of program:</font></pre>
<pre><font color="#FF0000"> <b><font size="3">(I found out the reason why Decryption outcome is different from solution:  the &quot;0&quot; in 0667</font></b></font></pre>
<pre><b><font color="#FF0000" size="3">means number 667 in base of &quot;8&quot;!!! and it is by chance that the two number 0667 and 0671 are all</font></b></pre>
<pre><b><font color="#FF0000" size="3">digits under 8!!! otherwise I will find out this mistake earlier!)		</font></b></pre>
<pre>Q1 is:
The linear combination is: 3457 x 1252 - 927 x 4669 = 1

Q2 is 89
Q10a is488
Q10b is31415
Q11 is122
Decrpt
0667 = 1808 1947 = 1121 0671 = 417		</pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>

<p>　          


</p>

<p>　</p>

<pre></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>