#include<stdio.h>
#include<string.h>
int w=0;
void gcd(int i,int z)
{
    if(z%i==0){ if(i==1) w=1;}
    else
    gcd(z%i,i);

}
main()
{
      int p1,q,n,z,d,e,i,j,l;
      int  t1=1;
      char p[20],c[20],p2[20];
      printf("enter any two prime numbers\n");
      scanf("%d %d",&p1,&q);
      n=p1*q;
      z=(p1-1)*(q-1);
      for(i=2;i<z;i++)
      {
     w=0;
     gcd(i,z);
     if(w==1) break;
      }
      if(i!=z) d=i;
      else printf("relative prime not found\n");
      for(i=1;i<z;i++)
      if((i*d)%z==1) break;
      printf("enter ur value\n");
      scanf("%d",&e);

      //else printf("rltve prm not found\n");
      printf("private key  is %d,%d\n",d,n);
      printf("public  key  is %d,%d\n",e,n);

      printf("enter the length of string ur enterng\n");
      scanf("%d",&l);
      printf("enter the plain text\n");
      scanf("%s",&p);
      getch();
      for(i=0;i<l;i++)
      {
      t1=1;
      for(j=0;j<e;j++)
      t1=(t1*p[i])%n;
      c[i]=(char)t1;

      }
      c[i]='\0';

      printf("\ncipher is %s",c);
      for(i=0;i<l;i++)
      {
      t1=1;
      for(j=0;j<d;j++)
      t1=(t1*c[i])%n;
      p2[i]=(char)t1;
      }
      p2[i]='\0';
      printf("\n decripted text is %s",p2);
      getch();

}


On Wed, Dec 9, 2015 at 2:20 PM, Incognito Avenger <
[email protected]> wrote:

> Hi everyone..I tried RSA algorithm by using C language and I got the
> correct output also. My query is *are there more number of methods to
> implement RSA algorithm in C language*??
>
>
> The logic of the code goes like this:
>
> 1) Pick two large prime numbers p and q, p != q;
> 2) Calculate n = p × q;
> 3) Calculate ø (n) = (p − 1)(q − 1);
> 4) Pick e, so that gcd(e, ø (n)) = 1, 1 < e <  ø (n);
> 5) Calculate d, so that d · e mod ø (n) = 1, i.e., d is the multiplicative
> inverse of e in mod  ø (n);
> 6) Get public key as KU = {e, n};
> 7) Get private key as KR = {d, n}.
> *Encryption*
> For plaintext block P < n, its ciphertext C = P^e (mod n).
> *Decryption*
> For ciphertext block C, its plaintext is P = C^d (mod n).
>
>
>
>
> My code goes like this:
>
> #include<stdio.h>
> #include<conio.h>
> int phi,d,flag,C,T,n,e;
> int eval(int e1);
> {
> printf("e value is:%d",e1);
> for(int k=3;e1%k==0 && phi%k==0;k++)
> {
> flag=1;
> return(1);
> }
> flag=0;
> return(1);
> }
> void encrypt()
> {
> int C=(T^e)%n;
> printf("\nencrypted data is:%d",C);
> }
> void decrypt()
> {
> int T=(C^e)%n;
> printf("\ndecrypted data is:%d",T);
> }
> int  main()
> {
> int p=17,q=29,s;
> //clrscr();
> //printf("\nenter first prime number here:");
> //scanf("%d",&p);
> //printf("\nenter second prime number here:");
> //scanf("%d",&q);
> n=p*q;
> printf("\nn value is:%d",n);
> phi=(p-1)*(q-1);
> printf("\nphi value is:%d",phi);
> do
> {
> e=31;
> printf("e value is:%d",e);
> eval(e);
> }while(flag==1);
> int d=1;
> do
> {//
> s=(e*d)%phi;
> d++;
> }while(s!=1);
> d=d-1;
>  printf("\npublic key is:{%d,%d}",e,n);
>  printf("\nprivate key is:{%d,%d}",d,n);
>  printf("\nenter you text here");
>  scanf("%d",&T);
>  encrypt();
>  printf("\nenter cipher text here");
>  scanf("%d",&C);
>  decrypt();
>  //getch();
>  return 0;
>  }
>
>
>
> Here at first I wrote p,q values manually but i got an error. Thats why I
> assigned values to p and q directly.
>
>
> Please tell me are there any more number of methods to implement RSA
> algorithm in C language.
>
>
> 7h4nk Y0u.... :-)
>
> --
> You received this message because you are subscribed to the Google Groups
> "NFORCEIT" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/nforceit.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
*Mujahid Pasha Syed,*
07070724727(Bihar),
09959190641( Hyd),
Asst Prof, CSE,
MANUU Polytechnic, Darbhanga.

-- 
You received this message because you are subscribed to the Google Groups 
"NFORCEIT" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send an email to [email protected].
Visit this group at https://groups.google.com/group/nforceit.
For more options, visit https://groups.google.com/d/optout.
#include<stdio.h>
#include<string.h>
int w=0;
void gcd(int i,int z)
{
    if(z%i==0){ if(i==1) w=1;}
    else
    gcd(z%i,i);

}
main()
{
      int p1,q,n,z,d,e,i,j,l;
      int  t1=1;
      char p[20],c[20],p2[20];
      printf("enter any two prime numbers\n");
      scanf("%d %d",&p1,&q);
      n=p1*q;
      z=(p1-1)*(q-1);
      for(i=2;i<z;i++)
      {
		      w=0;
		      gcd(i,z);
		      if(w==1) break;
      }
      if(i!=z) d=i;
      else printf("relative prime not found\n");
      for(i=1;i<z;i++)
      if((i*d)%z==1) break;
      printf("enter ur value\n");
      scanf("%d",&e);

      //else printf("rltve prm not found\n");
      printf("private key  is %d,%d\n",d,n);
      printf("public  key  is %d,%d\n",e,n);

      printf("enter the length of string ur enterng\n");
      scanf("%d",&l);
      printf("enter the plain text\n");
      scanf("%s",&p);
      getch();
      for(i=0;i<l;i++)
      {
			       t1=1;
			       for(j=0;j<e;j++)
			       t1=(t1*p[i])%n;
			       c[i]=(char)t1;

      }
      c[i]='\0';

      printf("\ncipher is %s",c);
      for(i=0;i<l;i++)
      {
			       t1=1;
			       for(j=0;j<d;j++)
			       t1=(t1*c[i])%n;
			       p2[i]=(char)t1;
      }
      p2[i]='\0';
      printf("\n decripted text is %s",p2);
      getch();

}


Reply via email to