You are on page 1of 10

//**************************************

//
// Name: RSA Encryption
// Description:To encrypt and decrypt da
//
ta using RSA encryption
// By: David J. Tabacco
//
//This code is copyrighted and has// limited warranties.Please see http://
//
www.Planet-Source-Code.com/vb/scripts/Sh
//
owCode.asp?txtCodeId=8714&lngWId=3//for details.//***********************
***************
//
#ifndef RSA
#define RSA
#endif
////////////////////////////////////////
//
////////////////////////////////////////
///
//----------------------------------Taba
//
cco9.cpp-------------------------------//
-//
//
//
//This program will implement RSA encryp
//
tion.
////
// Created by: David Tabacco //
// Created on: 12.06.04 //
////
//-------------------------------------//
Notes----------------------------------//
-//
////
// Input and output are strictly stdin a
//
nd stdout. File I/O is not available//
// nor necessary, due to the nature of t
//
his program.
//
//
////////////////////////////////////////
//
////////////////////////////////////////
///
#include<iostream>
#include<string>
#include<iomanip>
#include<ctime>
#include<cstdlib>
#include<cmath>
using namespace std;
/** Function Declarations **********/
int menu();
int prime(int);
__int64 GCD(__int64, __int64);
__int64 ExtEuclid(__int64,__int64);
void FastEXP(__int64,__int64,__int64);
void IntBin(__int64);
/* Global Arrays */
const __int64 maxsize = 50000;
__int64 r[maxsize]; //holds remainders

//

//

__int64 q[maxsize]; //holds quotients


const __int64 bytesize = 1024;
__int64 bin[bytesize];
__int64 top = 0;
__int64 bottom = 0;
//Start Main
int main()
{
__int64 method(0),max(0),gcd(0),counter(1),test(0);
__int64 b(0),c(0),d(0),e(0),f(0),i(0),j(0),p(0),q(0),t(0),a(0),n(0),m(0),the
ta(0);
while (method != 5)
{
method = menu();
switch (method)
{
case 1:
cout<<"Enter the max value between 1000 and 9999
"<<endl;
cin >> max;
if (max > 9999 || max < 1000){exit(0);}
system("CLS");
cout<<"Generating Keys";
while (p < 100){
p = prime(max);//get p
}
while (q <= p && q <= max){
q = prime(max);//get q
counter++;
test = counter %50; //track attempts
if (test == 0 ){cout<<".";}
}
cout<<endl;
n = p * q; //compute n
theta = (p-1)*(q-1); //compute theta
while (e == q || e == 0){
e = prime(max);}// e < theta
gcd = GCD(e,theta);
cout<<"Gcd is "<<gcd<<endl;
d = ExtEuclid(e,theta);
if (d < 0){
d = theta - abs(d);
}
cout<<"p is "<<p<<endl;
cout<<"q is "<<q<<endl;

cout<<"n is "<<n<<endl;
cout<<"theta is "<<theta<<endl;
cout<<"e is "<<e<<endl;
cout<<"d is "<<d<<endl;
p = 0;
q = 0; //reset p and q
break;
case 2: //Encrypt
cout<<"Enter Plaintext in decima
l format"<<endl;
cin>> t;
FastEXP(t,e,n);
break;
case 3: //Decrypt
cout<<"Enter Ciphertext in decim
al format"<<endl;
cin>> c;
FastEXP(c,d,n);
break;
case 4:
exit (0);
break;
default:
break;
}
}
return 0;
}
int menu()
{
int method;
cout<<"RSA Encryption"<<endl;
cout<<"Please make a selection?"<<endl;
cout<<"1. Generate Keys"<<endl;
cout<<"2. Encrypt "<<endl;
cout<<"3. Decrypt"<<endl;
cout<<"4. Exit"<<endl;
cin >> method;
system("CLS");
return method;
}
////////////////////////////////////////
//
////////////////////////////////////////
///
//-------------------------------------P
//
rime()---------------------------------//
-//
////
// This function will take a parameter s
//
ent in to determine the max prime//
// number that needs to be generated. A
//
list of all primes will be stored in//
// the static matrix array. after using
//
a Random Number Generator to pick from /
//
/

// the array, the final prime is returne


//
d.
//
//
//
//------------------------------------Va
//
riables--------------------------------//
-//
////
// max - Max number to find primes until
//
reached//
//
//
int prime(int max)//////////////////////////////////////
///////////////////////////
{
int prime(0);
int matrix[333][8]; //[row][column]
//all fixed top column numbers
matrix[0][0] = 1;
matrix[0][1] = 7;
matrix[0][2] = 11;
matrix[0][3] = 13;
matrix[0][4] = 17;
matrix[0][5] = 19;
matrix[0][6] = 23;
matrix[0][7] = 29;
int i(0),j(0),k(0),sr(0),modnum(0),row(0),col(0);
max = max / 30; //determine array-row size needed fo
r storage
for (i = 0; i < max; i++)//rows
{
for (j = 0; j < 8; j++) //columns, fixed
{
matrix[i][j] = (30 * i) + matrix[0][j];/
/i = row, j = columnn
}
}
//filtering the Matrix for Primes
for (k = 0; k < max; k++)//total rows
{
for (j = 0; j < 8; j++) //8 columns
{
sr = sqrt(matrix[k][j]);
for ( i = 2; i < sr + 2; i++) //check up
to the sq root
{

modnum = matrix[k][j] % i;
if (modnum == 0)
//not prime
{
i = sr +
3; //exit loop after Square root
matrix[k
][j] = NULL; //delete non-primes
}
if (modnum != 0 && i ==
sr + 1) //criteria for prime
{
//

cout<<"

"<<matrix[k][j]<<endl;//print out prime number


}
}
}
}
srand((unsigned)time(0)); //seed ran
dom number generator with clock
while (prime == 0){//until suitable
primes are picked
row = rand()%max;
col = rand()%8;
prime = matrix[row][col]; //assign p
rime to value in matrix
}
return prime;
} //end function
////////////////////////////////////////
//
/////////////////////////////////////
///
///
//-------------------------------------//
-GCD--()------------------------------//
--//
////
// This function will take 2 parameters
//
and solve the GCD from Euclid's Algor
ith
//

m //

//
//
//
//------------------------------------Va
//
riables-------------------------------//
--//
////
// a - smaller value

//
// b - larger value
//
//
//
//
__int64 GCD(__int64 a, __int64 b) //////////
////////////////////////////////////////
{
__int64 rem(0),gcd(0),i(0),k(0);
//Initialize Array
for (k=0;k<maxsize;k++){r[k]=(0);q[k]=(0
);}
r[i] = b;
i++;
while (b%a != 0){ //Does not divide
r[i] = a;
rem = b - a * (b/a);
q[i] = b/a;
b = a;
a = rem;
i++;
}
gcd = rem;
if (i == 1){gcd = a;}
r[i] = a;
bottom = i+1;//store length of array for Ext
Euclid loop
return gcd;
}
////////////////////////////////////////
//
////////////////////////////////////////
///
//-----------------------------------Ext
//
Euclid()-------------------------------//
--//
////
// This function will take 2 parameters
//
sent in and solve a two variable equatio
//
n //
// using the Extended Euclid Algorithm
//
//
//
//
//
//
//------------------------------------Va
//
riables--------------------------------//
--//
////
// a - smaller value
//
// b - larger value
//

//
//
//
__int64 ExtEuclid(__int64 a, __int64 b)/////////
///////////////////////////////////////////////
{
__int64 d(0),k(0),length(0),i(0);
const __int64 max = 5000;
__int64 x[max];
__int64 y[max];
for (k=0;k<max;k++){x[k]=(0);y[k]=(0);}
x[0] = 1;
x[1] = 0;
y[0] = 0;
y[1] = 1;
length = bottom;
for (i=2; i<length;i++){
x[i]=x[i-2]- (q[i-1]* x[i-1]);
y[i]=y[i-2]- (q[i-1]* y[i-1]);
}
d = y[i-1];
return d;
}
void FastEXP(__int64 a, __int64 m, __int64 n)
{
IntBin(m); //convert m to a binary integ
er array
__int64 k = top; //k is equal to the len
gth of the binary integer array
cout<<"k "<<k<<endl;
__int64 d = 1;
int c = 0;
//Fast Exponentiation Algorithm
for (int i = k; i > 0; i--)
{
c = 2*c;
d = (d*d) % n;
if (bin[i] == 1)
{
c = c+1;
d = (d * a) % n;
}
cout<<""<<c <<""<< d <<endl;
}
cout << "The Power "<<a<<" raise
d to the "<<m<<" (Mod "<<n<< ") is "<<d<<endl;

}
////////////////////////////////////////
//
/////////////////////////////////
///////
///
//-------------------------------------I
//
ntBin()-------------------------------//
-//
////
// This function will take a parameter s
//
ent in to determine the value tha
t//
// will be converted into a binary simul
//
ated integer array. Once the arra
y //
// is created, the function will print o
//
ut the binary number backwards//
//
//
//------------------------------------Va
//
riables-------------------------------//
-//
////
// m - Number to be converted into binar
//
y
//
//
//
void IntBin(__int64 m)//////////////////
///////////////////////////////////////////////
{
__int64 tmp(1),remain(1);
int c(0),i(0),j(0),test(0);
bool first(true);
for (i = 0; i < bytesize; i++){bin[i
] = 0;} //initialize array to 0
m = abs(m);
while (m != 0)
{
while (tmp <= m)
{
tmp = tmp * 2;
c++; //gets power
}
test = m%2; //determine if even
or odd
if (c == 0 && test==0)//special
case; ones place

{
bin[c]=0;//even number,
so assign one's place as 0
}
else
{bin[c]=1;}
for (j=1; j<c; j++)
{
remain = remain * 2; //g
et the actual va
//

lue of the highes

t power
}
if (first == true)//run only
once
{
top = c;//identify the h
ighest power
first = false; //prevent
condition from happening again
}
m = m - remain; //move to th
e next significant digit
c = 0; //reset counter
remain = 1; //reset remain
tmp = 1;//reset tmp
cout<<"m is "<<m<<endl;
}
for (i=1;i<top+1;i++){cout<<bin[
i];} //print binary
cout<<""<<endl;
}
////////////////////////////////////
////
//

/////////////////////////////

///////////
///
//----------------------------------Tab
//

acco8.cpp--------------------

----------//
-//
//----------------------------Copyri
ght
//

David Tabacco, 2004----------

----------//
-//
////////////////////////////////////
////

//
///////////
///

/////////////////////////////

You might also like