# Help: Whats wrong with this Program of of RSA encryption

## Irfan Azher — 2002-04-06 00:25:14

    Hi,

        Plz. tell me that why the value of  charcter

    obtained by the program is not always equal to

    character  which the user enter

    SOURCE CODE:

    ~~~~~~~~~~~

    // Implementation to test RSA Algorithm

    #include<stdio.h>

    #include<math.h>

    #include<stdlib.h>

    #include<conio.h>

    main()

    {

    	unsigned long int  m,n,p,q,e,n2,d,c;

    	int loop=0,loop1,an,bn;

    	char binary[150],cchar;

    	unsigned long int result[100],mx;

    	clrscr();

    	printf("Enter any Character: ");

    	scanf("%c",&cchar);

    	m=cchar;

    	printf("\n%ld\n",m);

    	printf("Enter the First Prime No. p: ");

    	scanf("%ld",&p);

    	printf("Enter the 2nd Prime No. q: ");

    	scanf("%ld",&q);

    	n=p*q;

    	n2=(p-1)*(q-1);

    	printf("\nThe Value of Func(n) is %ld\nEnter The

    Value (Having GCD 1 with Func(n) of E: ",n2);

    	scanf("%ld",&e);

    	mx=e;

    // Evaluating Binary Equivalent of e

    	do

    	{

    		an=mx%2;

    		if(an==1)

    			binary[loop++]='1';

    		else

    			binary[loop++]='0';

    	}

    	while((mx=mx/2)>1);

    	if(mx==1)

    		binary[loop]='1';

    // Evaluating M^e%n

    	bn=0;

    	for(mx=m;loop>=0;loop--)

    	{

    		mx=mx%n;

    		if(binary[loop]=='1' )

    			result[bn++]=mx;

    		mx=mx*mx;

    	}

    	c=result[--bn];

    	if(bn!=0)

    	{

    		do

    		{

    			c=(result[--bn]*c)%n;

    		}

    		while(bn>0);

    	}

    	printf("\nencrypted code is %ld ",c);

    	getch();

    // Finding Value of D

    	d=1;

    	loop1=0;

    	while(d<n)

    	{

    		if(d*e%n2==1)

    		{

    			printf("\n The Value of d is %ld",d);

    			getch();

    			loop1=1;

    			break;

    		}

    		d++;

    	}

    	if(loop1==0)

    	{

    		printf("\n The Value of d is Undefined");

    		getch();

    	}

    	mx=d;

    //Evaluating Binary Equivalent of d

    	loop=0;

    	do

    	{

    		an=mx%2;

    		if(an==1)

    			binary[loop++]='1';

    		else

    			binary[loop++]='0';

    	}

    	while((mx=mx/2)>1);

    	if(mx==1)

    		binary[loop]='1';

    // Evaluating c^d%n

    	bn=0;

    	for(mx=c;loop>=0;loop--)

    	{

    //		printf("\n%ld(mx) mod %ld(n)=",mx,n);

    		mx=mx%n;

    //		printf("%ld",mx);

    		if(binary[loop]=='1' )

    		{

    			result[bn++]=mx;

    //			printf("\t\t\tresult[%d]=%ld",bn-1,result[bn-1]);

    		}

    		mx=mx*mx;

    	}

    	m=result[--bn];

    	if(bn!=0)

    	{

    		do

    		{

    			m=(result[--bn]*m)%n;

    		}

    		while(bn>0);

    	}

    	printf("\nYour Text is %ld,%c ",m,m);

    	getch();

     }

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~

    plz. solve my problem

    irfan azher

    __________________________________________________

    Do You Yahoo!?

    Yahoo! Tax Center - online filing with TurboTax

    http://taxes.yahoo.com/

## e1_t — 2002-04-06 02:54:06

    --- In concatenative@y..., Irfan Azher <irfan_azher@y...> wrote:

     > Hi,

    >     Plz. tell me that why the value of  charcter

    > obtained by the program is not always equal to

    > character  which the user enter

    >

    > SOURCE CODE:

    > ~~~~~~~~~~~

    > // Implementation to test RSA Algorithm

    >

    >

     [snip]

     >

    > plz. solve my problem

    >

     What exactly made you post that onto a message board on concatenative

    programming languages?

    Ivan