C program to implement RSA algorithm. The given program will Encrypt and Decrypt a message using RSA Algorithm.

#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<math.h> #include<string.h> long int p,q,n,t,flag,e[100],d[100],temp[100],j,m[100],en[100],i; char msg[100]; int prime(long int); void ce(); long int cd(long int); void encrypt(); void decrypt(); void main() { clrscr(); printf("\nENTER FIRST PRIME NUMBER\n"); scanf("%d",&p); flag=prime(p); if(flag==0) { printf("\nWRONG INPUT\n"); getch(); exit(1); } printf("\nENTER ANOTHER PRIME NUMBER\n"); scanf("%d",&q); flag=prime(q); if(flag==0||p==q) { printf("\nWRONG INPUT\n"); getch(); exit(1); } printf("\nENTER MESSAGE\n"); fflush(stdin); scanf("%s",msg); for(i=0;msg[i]!=NULL;i++) m[i]=msg[i]; n=p*q; t=(p-1)*(q-1); ce(); printf("\nPOSSIBLE VALUES OF e AND d ARE\n"); for(i=0;i<j-1;i++) printf("\n%ld\t%ld",e[i],d[i]); encrypt(); decrypt(); getch(); } int prime(long int pr) { int i; j=sqrt(pr); for(i=2;i<=j;i++) { if(pr%i==0) return 0; } return 1; } void ce() { int k; k=0; for(i=2;i<t;i++) { if(t%i==0) continue; flag=prime(i); if(flag==1&&i!=p&&i!=q) { e[k]=i; flag=cd(e[k]); if(flag>0) { d[k]=flag; k++; } if(k==99) break; } } } long int cd(long int x) { long int k=1; while(1) { k=k+t; if(k%x==0) return(k/x); } } void encrypt() { long int pt,ct,key=e[0],k,len; i=0; len=strlen(msg); while(i!=len) { pt=m[i]; pt=pt-96; k=1; for(j=0;j<key;j++) { k=k*pt; k=k%n; } temp[i]=k; ct=k+96; en[i]=ct; i++; } en[i]=-1; printf("\nTHE ENCRYPTED MESSAGE IS\n"); for(i=0;en[i]!=-1;i++) printf("%c",en[i]); } void decrypt() { long int pt,ct,key=d[0],k; i=0; while(en[i]!=-1) { ct=temp[i]; k=1; for(j=0;j<key;j++) { k=k*ct; k=k%n; } pt=k+96; m[i]=pt; i++; } m[i]=-1; printf("\nTHE DECRYPTED MESSAGE IS\n"); for(i=0;m[i]!=-1;i++) printf("%c",m[i]); }

**Output ff the above program:-**

RSA Inputs |

RSA Result |

**Related Programs:-**

★ Encrypt and Decrypt a message using Substitution Cipher

★ Encrypt and Decrypt a message using Vernan Cipher

★ Encrypt and Decrypt a message using Transposition Cipher

★ Encrypt and Decrypt a message using PlayFair Cipher

★ Calculate compression ratio

I want the same program in java.please help me.....plz respond to me as soon as possible

ReplyDeletecheck network label above to access all network programs or check it: http://www.coders-hub.com/2013/04/implementation-of-rsa-using-java.html

DeleteCan RSA algorithm can be implemented on NS2 by creating the nodes, Can u write from where i get it?

ReplyDeletegreat code, thanks so much....

ReplyDeletecode is not running it is getting error in 38th line can u please help me

Deletethank tou so much!!!!

ReplyDeletethank you so much!!!!!

ReplyDeletevery long code !!!

ReplyDeletebut it a great code

thanks... :)

how can i calculate run time for encryption &decryption......plz help

ReplyDeletecan you pls explain what 'ce' is?

ReplyDeletevoid ce()

its a function to compute encryption key e

DeleteThis comment has been removed by the author.

ReplyDeletecan you send me the code for encrypting and decrypting a image using RSA

ReplyDeletethanks

Deletecan you send me the code for encrypting and decrypting a image using RSA to kaliraj401@gmail.com

Deletecan you send me the code for encrypting and decrypting a image using RSA to sean123456p@gmail.com

Deletethanks

thanks

ReplyDeletecan i have this program in c#

ReplyDeletecode not run on Linux correctly if enter prime 5 ,3 not encrypt correctly

ReplyDeletehi ,

ReplyDeletecould you pls let me know how the same code works using fixed block size ? example 4?

This comment has been removed by the author.

ReplyDeleteIn the encrypt function, why is 96 added to pt, and subsequently added back?

ReplyDeleteI too want to know the same thing.

DeleteThis is based on ascii code. Ascii code of 'A' is 65 and 'a' is 97.

DeleteIs the code also working without adding/subtracting the 96? Thanks!

DeleteAlso, is there a limit on how large the prime numbers can be for this method?

ReplyDeletecould you tell me what variable "flag" means?

ReplyDeleteHere we are checking input number is a prime or not and setting 0 or 1 value to flag based on prime number and than doing rest of the things.

Deletecan you send me the code for encrypting and decrypting a image using RSA to kaliraj401@gmail.com

ReplyDeletein encrypt() function, what does this part do?

ReplyDeletefor(j=0;j<key;j++)

{

k=k*pt;

k=k%n;

}

Can you please tell me that in the function void ce(), what does the following do:

ReplyDeleteif(k==99)

break;

Hi im having a problem when i run your code, i get this error:

ReplyDeletein function 'main':

38 warning: comparison between pointer and integer[enabled by default]

undefined reference to 'clrscr'

Hi im having a problem when i run your code, i get this error:

ReplyDeletein function 'main':

38 warning: comparison between pointer and integer[enabled by default]

undefined reference to 'clrscr'

why pt=pt-96 in this program.plz ..

ReplyDeleteI need this code for swift, please help me

ReplyDeletei need source code encryption description in gost algorithm, anyone can help me ?

ReplyDeletecan i have a code for secure user authentication system using rsa key generation..?

ReplyDeleteis it possible to make it work using "char temp[100]" instead of "int temp[100]"

ReplyDeleteis there anyway to lock a folder using RSA encryption?

ReplyDeleteif we give prime numbers as 251 & 263 then its not working.. anybody know why...?

ReplyDeletecode is not running it is getting some error in 38th line

ReplyDeletecan you send me the modified code for my mail

Deletejeevanreddy307@gmail.com