Search Tutorials

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

47 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
    Replies
    1. code is not running it is getting error in 38th line can u please help me

      Delete
  4. thank tou so much!!!!

    ReplyDelete
  5. thank you so much!!!!!

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

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

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

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

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

    ReplyDelete
  10. 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
    2. can you send me the code for encrypting and decrypting a image using RSA to sean123456p@gmail.com
      thanks

      Delete
  11. can i have this program in c#

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

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

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

    ReplyDelete
  15. 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
    3. Is the code also working without adding/subtracting the 96? Thanks!

      Delete
    4. hi, I want to ask. I have problem when modified your code in c. I have same question, why pt=pt-96 and pt=k+96? i have seen ascii table but the result is false. When i try to calculate, the result between manual and program is different. May you give your email? because i have so many problem and want talk about RSA.

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

      Delete
    6. hi, I want to ask. I have problem when modified your code in c. I have same question, why pt=pt-96 and pt=k+96? i have seen ascii table but the result is false. When i try to calculate, the result between manual and program is different. May you give your email? because i have so many problem and want talk about RSA.

      maybe you have modified your code, you can send your code on email oktasafira98@gmail.com, thank you so much and please respond my question
      i need your help

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

    ReplyDelete
  17. 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
  18. can you send me the code for encrypting and decrypting a image using RSA to kaliraj401@gmail.com

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

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

    ReplyDelete
  21. Anonymous5:50 pm

    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. Anonymous7:22 pm

    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
  23. why pt=pt-96 in this program.plz ..

    ReplyDelete
  24. I need this code for swift, please help me

    ReplyDelete
  25. i need source code encryption description in gost algorithm, anyone can help me ?

    ReplyDelete
  26. can i have a code for secure user authentication system using rsa key generation..?

    ReplyDelete
  27. is it possible to make it work using "char temp[100]" instead of "int temp[100]"

    ReplyDelete
  28. is there anyway to lock a folder using RSA encryption?

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

    ReplyDelete
  30. code is not running it is getting some error in 38th line

    ReplyDelete
    Replies
    1. can you send me the modified code for my mail
      jeevanreddy307@gmail.com

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

    ReplyDelete
  32. how large primes nos it can handle...
    If we want the primes nos to be very large then how to implement it?

    ReplyDelete
  33. what is the prime number for?

    ReplyDelete
  34. Why do we subtract 96 in encryption and decryption

    ReplyDelete

Back to Top