Code:
/* C program for the Implementation Of RSA Algorithm */
#include <stdio.h>
#include <ncurses.h>
#include <stdlib.h>
#include <math.h>
#include <packet.h>
#include <agent.h>
#include <tclcl.h>
#include "rsa.h"
using namespace std;
int TCL_OK,phi,M,n,e,d,C,FLAG,i,p,q,s;
static class rsaClass:public TclClass{
public:
rsaClass(): TclClass("Agent/rsa"){
}
TclObject* create(int argc,const char*const argv)
{
return (new rsa());
}
}Class_rsa;
rsa::rsa(): Agent(PT_RSA)
/*binding the tcl variable - c++ vraiable*/
{
bind("phi_otcl",&phi);
bind("M_otcl",&M);
bind("C_otcl",&C);
bind("n_otcl",&n);
bind("e_otcl",&e);
bind("d_otcl",&d);
bind("FLAG_otcl",&FLAG);
bind("i_otcl",&i);
bind("p_otcl",&p);
bind("q_otcl",q);
bind("s_otcl",s);
};
//le code rsa en c++//
int
rsa::check()
{
int i;
for(i=3;e%i==0 && phi%i==0;i+2)
{
FLAG = 1;
return 0;
}
FLAG = 0;
}
void
rsa::encrypt()
{
int i;
C = 1;
for(i=0;i< e;i++)
C=C*M%n;
C = C%n;
printf("\n\tEncrypted keyword : %d",C);
}
void
rsa::decrypt()
{
int i;
M = 1;
for(i=0;i< d;i++)
M=M*C%n;
M = M%n;
printf("\n\tDecrypted keyword : %d",M);
}
int
rsa::main()
{
int p,q,s;
printf("Enter Two Relatively Prime Numbers\t: ");
scanf("%d%d",&p,&q);
n = p*q;
phi=(p-1)*(q-1);
printf("\n\tF(n)\t= %d",phi);
do
{
printf("\n\nEnter e\t: ");
scanf("%d",&e);
check();
}while(FLAG==1);
d = 1;
do
{
s = (d*e)%phi;
d++;
}while(s!=1);
d = d-1;
printf("\n\tPublic Key\t: {%d,%d}",e,n);
printf("\n\tPrivate Key\t: {%d,%d}",d,n);
printf("\n\nEnter The Plain Text\t: ");
scanf("%d",&M);
encrypt();
}
/*command*/
int
rsa::command(int argc,const char*const*argv)
{
if (argc==2){
if (strcmp(argv[1],"encrypt")==0){
encrypt();
return (TCL_OK);};
}
return (Agent::command(argc,argv));
}