Your Ad Here

Wednesday, January 23, 2008

RSA Algorithm

/* C program for the Implementation Of RSA Algorithm */

#include< stdio.h>
#include< conio.h>

int phi,M,n,e,d,C,FLAG;

int check()
{
int i;
for(i=3;e%i==0 && phi%i==0;i+2)
{
FLAG = 1;
return;
}
FLAG = 0;
}

void encrypt()
{
int i;
C = 1;
for(i=0;i< e;i++)
C=C*M%n;
C = C%n;
printf("\n\tEncrypted keyword : %d",C);
}

void decrypt()
{
int i;
M = 1;
for(i=0;i< d;i++)
M=M*C%n;
M = M%n;
printf("\n\tDecrypted keyword : %d",M);
}

void main()
{
int p,q,s;
clrscr();
printf("Enter Two Relatively Prime Numbers\t: ");
scanf("%d%d",&p,&q);
n = p*q;
phi=(p-1)*(q-1);
printf("\n\tF(n)\t= %d",phi);
do
{
printf("\n\nEnter e\t: ");
scanf("%d",&e);
check();
}while(FLAG==1);
d = 1;
do
{
s = (d*e)%phi;
d++;
}while(s!=1);
d = d-1;
printf("\n\tPublic Key\t: {%d,%d}",e,n);
printf("\n\tPrivate Key\t: {%d,%d}",d,n);
printf("\n\nEnter The Plain Text\t: ");
scanf("%d",&M);
encrypt();
printf("\n\nEnter the Cipher text\t: ");
scanf("%d",&C);
decrypt();
getch();
}

/*************** OUTPUT *****************

Enter Two Relatively Prime Numbers : 7 17

F(n) = 96

Enter e : 5

Public Key : {5,119}
Private Key : {77,119}

Enter The Plain Text : 19

Encrypted keyword : 66

Enter the Cipher text : 66

Decrypted keyword : 19 */

54 comments:

Sukesh said...

HI this is good work..


Sukesh

kumar said...

Hello try to write code to generate e also

Anonymous said...

Very good code, But plz try to generate e value also

Mayur said...

Hey its a very gud code ,
but i failed to understand it.
But very good attempt ,keep it up dude
next time this will not do.

abhijeet apsunde said...

Compact and efficient code !!
i was not able to handle the large exponents !! gr8 work man !!

kumar, there is code which checks for valid e [ means e should me co-prime to PHI ] in the program itself

you can simply add a loop for a range of numbers and select a particular number as e if it passes CHECK routine

Anonymous said...

This is great work, dude. I love coding too. I can see the efficiency of coding is of high class. it is simple and efficient, the way i love it.

s.sudha said...

Hi! this is good job.
keep it up.
it is very useful to me.
i can understand this program.
thank you dude.
thanks a lot.

pankaj said...

nice work dude...

pankaj said...

nice work dude...

Anonymous said...

really good code
it works n well understood

Anonymous said...

g8t help..cool code

Anonymous said...

could be much better.

Anonymous said...

Nice 1 re....I made it 2 long...But yours is short n sweet!

bhuvana said...

hai..
this code is short n sweet 2 explain the rsa algorithm concept.

Parth Shah said...

I am going to post another code just as r reference.

*************************************
/* C program to implement RSA[Rivest-Shamir-Adelman ] algorithm for encryption. */





#include

#include

#include



void main()

{

int I, j, k, n, t0, t1, p=11, q=3, phi, e, d=1, m, c, h;

clrscr();



n=p*q;

phi=(p-1)*(q-1);



printf(“phi= %d and n= %d ”, phi, n);

printf(“Enter value for encrypted key:- ”);

scanf(“%d”,&e);



do

{

t0=(e*d)-1;

t1=t0/phi;

d++;

}



while(t1!=1)

printf(“\nPublic key(n,e): %d %d”,n,e);

printf(“\nPrivate key(n,d): %d %d”, n, --d);



printf(“\n\nEnter the message to be encrypted:- ”)

scanf(“%d”, &m);



j=pow(m,e);

c=j%n;



printf(“\n Ciphertext is:- %d”, c);



k=pow(m,e);

c=k%n;



printf(“\n\n Decrypted text is:- %d”,h);



getch();

}





OUTPUT



phi: 20 and n:33

Enter value for encrypted key:- 3

Public key(n,e): 33 3

Private key(n,d): 33 7



Enter the message to be encrypted:- 7

Ciphertext is:- 13

Decrypted text is: 7


******************************

Anonymous said...

THANK YOU SOOO MUCHHHHHHH

Anonymous said...

please check it 4 primes 101,113.
e=3533
plaitext=9726
the output should hav come 5761 but its not..
i couldnt find the mistake plz do it n let me knw..

ankit said...

plz try to generate for e value also and also for the prime numbers

ankit said...

plz try to generate for e value also and also for the prime numbers

Anonymous said...

i also tried to do the same program in different way but it shows some error like overflow and al. . I can send u the codings and verify it and do it. .

siddhartha said...

i found the program here check out this.. friends..

http://csestrikers.com/source-code/c-programs/item/16-rsa-cryptography-c-program.html

Anonymous said...

Hey Parth,
pow() function takes a double as argument and returns a double.I tried explicit casting but no luck.

Nithin Devang said...

Hey dis s wrong.. try the same for other values.. like for plain text 128 with the same value of p,q,e.. it gives sum oder value after decrypt

Anonymous said...

for(e=2;e<=phi;e++)
if(((e*d)%phi)==1)
break;


this code can be used to calculate e value.

Anonymous said...

What is the purpose of i+2 in the

for(i=3;e%i==0&&phi%i==0;i+2)

loop?

Does this mean i++ instead?

adam said...

understandable!

really good references...=)

kajal said...

thank you. its a very good and understandable code but it does not work properly for many inputs. eg: p=11, q=7, e=5. pls reply..

kajal dhawral.

kajal said...

really nice easy code, but does not work for all values. eg: p=11, q=7, e=5. pls reply..

Anonymous said...

This program does not work for many values.

Stefan said...

The code here is wrong

for(i=0;i< e;i++)
C=C*M%n;

It should do modulo only after finishing the loop (did M^e)

Otherwise, good job

don said...

Hello everyone,
When p=11 and q=7 in that case value of phi will be (p-1)*(q-1)=10*6=60.
Since value of phi is 60, you can not select value of e as 5 because 5 and 60 are not relatively prime.

Wahiba said...

hi;

can some one help to resolve my issue, i'm unable to excecute this RSA Algorithm with 512, 1024,2048, 4096 bit keys ???

please could you help me Lionel

waiting for yr quick reply

regards

Anonymous said...

nice coding thanks for making it public to help all..

Anonymous said...

thank you for the interesting code, i think i should add an RSA algorythm based on this codes to my upcoming simple benchmarktest app.

greetings: Geri

Anonymous said...

I have a problem with te return;
I doing in TC (turbo c)

Himanshu said...

as stefen has said above in his comment, mode is done after calculating power in both cases i.e encryption and decryption... except that ur program is godd . secondaly , overflow errors are due to limitation of int value i.e 32567

Anonymous said...

if the plain text is string then how to implement rsa to decrypt the message

Anonymous said...

wowwwwwwww wat a code.. i became very dedicated to coding after looking at ur code.. thank u for opening my eyes to the beauty of coding

Anonymous said...

ur code works.. don listen to others

harsh said...

wat abt p=11 q=3 e=7

harsh said...

p=11 q=3 e=7

harsh said...

its nt wrkng fr p=11 q=7 e=3 and m=43

Дмитрий Лазоренко said...

My RSA program supports infinite digits. See my blog for details.

Anonymous said...

you are not validating e properly

Anonymous said...

thank you

Florian said...

Some mistakes in the code:
-int check should return a value, you dont need global variables for this code.
-only main should print something on the screen, e.g. add parameters to the encrypt function and return the value you want to print. decrypt the same.
-Error handling is missing.
-clrscr() is not ANSI.
-also you have to take much larger prime numbers for the algo to be (a little bit) safer, so you should randomly generate them and dont ask the user.
-Do I have to say something about the do/while loops?
-If you take larger numbers this code will fail.
-Also try to make more comments so everyone can understand it better.
-Like many said, if you dont generate e the code is useless, you can then just use the paper/pencil method.

The encrypt and decrypt method is aside from formal mistakes not so bad.

Anonymous said...

thnx alot
it really helped me

Anonymous said...

thnx

Anonymous said...

Thank you for the gоod writeup. It in truth used to
be a amuѕеmеnt аccount іt.
Glаnce сomplex to far delivеreԁ agrеeablе
from yοu! By thе wаy, how could ωe cоmmuniсаte?



my blog pοst: disque ssd

Anonymous said...

I do not even know how I ended up here, but I thought this post was great.

I do not know who you are but definitely you are going to a
famous blogger if you are not already ;) Cheers!


Here is my blog - voyance gratuite

Anonymous said...

I was able to find good advice from your blog articles.


Here is my weblog; voyance gratuite

Anonymous said...

could you please provide with C/C++ code for E0(stream cipher used in bluetooth) encryption algorithm as well?

Anonymous said...

thanks a lot...
God bless..

Anonymous said...

can anyone give me write code of rsa algorithm......above code dolesnot give any output.















Your Ad Here