Search Tutorials

Loading...

Tuesday, 23 April 2013

C code to implement RSA Algorithm(Encryption and Decryption)

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:-

C code to implement RSA (Encryption and Decryption)
RSA Inputs

C code to implement RSA (Encryption and Decryption)
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

31 comments:

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

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

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

    ReplyDelete
  3. great code, thanks so much....

    ReplyDelete
  4. thank you so much!!!!!

    ReplyDelete
  5. very long code !!!
    but it a great code
    thanks... :)

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

    ReplyDelete
  7. can you pls explain what 'ce' is?
    void ce()

    ReplyDelete
    Replies
    1. its a function to compute encryption key e

      Delete
  8. This comment has been removed by the author.

    ReplyDelete
  9. can you send me the code for encrypting and decrypting a image using RSA

    ReplyDelete
    Replies
    1. can you send me the code for encrypting and decrypting a image using RSA to kaliraj401@gmail.com

      Delete
  10. can i have this program in c#

    ReplyDelete
  11. code not run on Linux correctly if enter prime 5 ,3 not encrypt correctly

    ReplyDelete
  12. hi ,
    could you pls let me know how the same code works using fixed block size ? example 4?

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. In the encrypt function, why is 96 added to pt, and subsequently added back?

    ReplyDelete
    Replies
    1. I too want to know the same thing.

      Delete
    2. This is based on ascii code. Ascii code of 'A' is 65 and 'a' is 97.

      Delete
  15. Also, is there a limit on how large the prime numbers can be for this method?

    ReplyDelete
  16. could you tell me what variable "flag" means?

    ReplyDelete
    Replies
    1. Here 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.

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

    ReplyDelete
  18. in encrypt() function, what does this part do?
    for(j=0;j<key;j++)
    {
    k=k*pt;
    k=k%n;
    }

    ReplyDelete
  19. Can you please tell me that in the function void ce(), what does the following do:
    if(k==99)
    break;

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

    in function 'main':
    38 warning: comparison between pointer and integer[enabled by default]
    undefined reference to 'clrscr'

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

    in function 'main':
    38 warning: comparison between pointer and integer[enabled by default]
    undefined reference to 'clrscr'

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

    ReplyDelete

Back to Top