Skip to content

Instantly share code, notes, and snippets.

@sudipidus
Created September 20, 2015 17:36
Show Gist options
  • Save sudipidus/5033fa7ae8875cad9f5a to your computer and use it in GitHub Desktop.
Save sudipidus/5033fa7ae8875cad9f5a to your computer and use it in GitHub Desktop.
//rsa algorithm for encryption
#include<stdio.h>
int modulo(int,int,int);
int main()
{
int p,q,m;
printf("\nEnter p and q values (Prime Numbers)");
scanf("%d%d",&p,&q);
printf("\nEnter the message:");
scanf("%d",&m);
int n=p*q;
int phi=(p-1)*(q-1);
int e=1;
for (;e<phi;e++)
if (phi%e!=0) break;
int d=1;
for (;d<n;d++)
if ((d*e)%phi==1) break;
int c=modulo(m,e,n); //crypt text
printf("\nc=%d d=%d",c,d);
printf("\nEncrypted Message=%d",c);
printf("\nDecrypted Message=%d",modulo(c,d,n));
return 0;
}
int modulo(int a,int b,int c)
{
int rem=(a%c);
b--;
while (b--)
{
rem=(rem*(a%c))%c;
}
return rem;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment