Professional Documents
Culture Documents
//
// 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
//
//
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 /
//
/
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<<"
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
//
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
//
----------//
-//
////////////////////////////////////
////
//
///////////
///
/////////////////////////////