0% found this document useful (0 votes)
234 views

RSA

RSA algorithm is an asymmetric cryptographic algorithm that uses two keys, a public key and a private key, to encrypt and decrypt messages. It works by first generating the two keys using prime numbers and mathematical operations. The public key is used to encrypt messages, while the private key is needed to decrypt encrypted messages. The document then provides code for an RSA encryption/decryption program in C++ to demonstrate how it works in practice.

Uploaded by

Prathibha Bh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
234 views

RSA

RSA algorithm is an asymmetric cryptographic algorithm that uses two keys, a public key and a private key, to encrypt and decrypt messages. It works by first generating the two keys using prime numbers and mathematical operations. The public key is used to encrypt messages, while the private key is needed to decrypt encrypted messages. The document then provides code for an RSA encryption/decryption program in C++ to demonstrate how it works in practice.

Uploaded by

Prathibha Bh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

RSA algorithm to encrypt and decrypt

RSA Algorithm is used to encrypt and decrypt data in modern computer systems and
other electronic devices. RSA algorithm is an asymmetric cryptographic algorithm as it creates 2
different keys for the purpose of encryption and decryption. It is public key cryptography as one
of the keys involved is made public. RSA stands for Ron Rivest, Adi Shamir and Leonard
Adleman who first publicly described it in 1978.

RSA makes use of prime numbers (arbitrary large numbers) to function. The public key
is made available publicly (means to everyone) and only the person having the private key with
them can decrypt the original message.

Working of RSA Algorithm

RSA involves use of public and private key for its operation. The keys are generated using the
following steps:-

1. Two prime numbers are selected as p and q


2. n = pq which is the modulus of both the keys.
3. Calculate totient = (p-1)(q-1)
4. Choose e such that e > 1 and coprime to totient which means gcd (e, totient) must be
equal to 1, e is the public key
5. Choose d such that it satisfies the equation de = 1 + k (totient), d is the private key not
known to everyone.
6. Cipher text is calculated using the equation c = m^e mod n where m is the message.
7. With the help of c and d we decrypt message using equation m = c^d mod n where d is the
private key.

Page 1
RSA algorithm to encrypt and decrypt

Program

#include<stdio.h>

#include<math.h>

double min(double x, double y)

return(x<y?x:y);

double max(double x,double y)

return(x>y?x:y);

double gcd(double x,double y)

if(x==y) return(x); else return(gcd(min(x,y),max(x,y)-min(x,y)));

long double modexp(long double a,long double x,long double n)

long double r=1;

while(x>0)

if ((int)(fmodl(x,2))==1)

Page 2
RSA algorithm to encrypt and decrypt

r=fmodl((r*a),n);

a=fmodl((a*a),n);

x/=2;

return(r); }

int main()

long double p,q,phi,n,e,d,cp,cq,dp,dq,mp,mq,sp,sq,rp,rq,qInv,h; long double ms,es,ds;

do{ printf("\n Enter prime numbers p and q:");

scanf(" %Lf %Lf",&p,&q);

} while(p==q);

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

do{

printf("\n Enter prime value of e:"); scanf(" %Lf",&e);

} while((gcd(e,phi)!=1)&&e>phi); /*for e being relatively prime to phi */

for(d=1;d<phi;++d)

if(fmod((e*d),phi)==1)

break;

Page 3
RSA algorithm to encrypt and decrypt

printf("\n D within main = %Lf",d); /* public key is {n,e} private key is d */

printf("\n Enter the message:");

scanf(" %Lf",&ms);

es=modexp(ms,e,n);

ds=modexp(es,d,n);

printf("\n Original Message : %Lf",ms);

printf("\n Encrypted Message : %Lf",es);

printf("\n Decrypted Message : %Lf\n",ds);

return(0);

Page 4

You might also like