/* 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 */
Wednesday, January 23, 2008
Subscribe to:
Post Comments (Atom)
54 comments:
HI this is good work..
Sukesh
Hello try to write code to generate e also
Very good code, But plz try to generate e value also
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.
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
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.
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.
nice work dude...
nice work dude...
really good code
it works n well understood
g8t help..cool code
could be much better.
Nice 1 re....I made it 2 long...But yours is short n sweet!
hai..
this code is short n sweet 2 explain the rsa algorithm concept.
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
******************************
THANK YOU SOOO MUCHHHHHHH
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..
plz try to generate for e value also and also for the prime numbers
plz try to generate for e value also and also for the prime numbers
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. .
i found the program here check out this.. friends..
http://csestrikers.com/source-code/c-programs/item/16-rsa-cryptography-c-program.html
Hey Parth,
pow() function takes a double as argument and returns a double.I tried explicit casting but no luck.
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
for(e=2;e<=phi;e++)
if(((e*d)%phi)==1)
break;
this code can be used to calculate e value.
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?
understandable!
really good references...=)
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.
really nice easy code, but does not work for all values. eg: p=11, q=7, e=5. pls reply..
This program does not work for many values.
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
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.
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
nice coding thanks for making it public to help all..
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
I have a problem with te return;
I doing in TC (turbo c)
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
if the plain text is string then how to implement rsa to decrypt the message
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
ur code works.. don listen to others
wat abt p=11 q=3 e=7
p=11 q=3 e=7
its nt wrkng fr p=11 q=7 e=3 and m=43
My RSA program supports infinite digits. See my blog for details.
you are not validating e properly
thank you
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.
thnx alot
it really helped me
thnx
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
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
I was able to find good advice from your blog articles.
Here is my weblog; voyance gratuite
could you please provide with C/C++ code for E0(stream cipher used in bluetooth) encryption algorithm as well?
thanks a lot...
God bless..
can anyone give me write code of rsa algorithm......above code dolesnot give any output.
Post a Comment