You are on page 1of 6

STES SINHGAD INSTITUTE OF TECHNOLOGY, LONAVALA

EXPERIMENT NO. : 03

Name of the student:-

Roll no Subject:- Information and Cyber Security

Date of practical performed: - Staff signature with date & Marks

Title:-
Develop the program to implement RSA algorithm for encryption and decryption. Assume
suitable Private and Public Keys.
Theory:-
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.
8. Note: If we take the two prime numbers very large it enhances security but requires
implementation of Exponentiation by squaring algorithm and square and multiply
algorithm for effective encryption and decryption. For simplicity the program is
designed with relatively small prime numbers.
9. Below is the implementation of this algorithm in C and C++.
Program for RSA Algorithm in C

1. /Program for RSA asymmetric cryptographic algorithm


2. //for demonstration values are relatively small compared to practical application
3.
4. #include<stdio.h>
5. #include<math.h>
6.
7. //to find gcd
8. int gcd(int a, int h)
9. {
10. int temp;
11. while(1)
12. {
13. temp = a%h;
14. if(temp==0)
15. return h;
16. a = h;
17. h = temp;
18. }
19. }
20.
21. int main()
22. {
23. //2 random prime numbers
24. double p = 3;
25. double q = 7;
26. double n=p*q;
27. double count;
28. double totient = (p-1)*(q-1);
29.
30. //public key
31. //e stands for encrypt
32. double e=2;
33.
34. //for checking co-prime which satisfies e>1
35. while(e<totient){
36. count = gcd(e,totient);
37. if(count==1)
38. break;
39. else
40. e++;
41. }
42.
43. //private key
44. //d stands for decrypt
45. double d;
46.
47. //k can be any arbitrary value
48. double k = 2;
49.
50. //choosing d such that it satisfies d*e = 1 + k * totient
51. d = (1 + (k*totient))/e;
52. double msg = 12;
53. double c = pow(msg,e);
54. double m = pow(c,d);
55. c=fmod(c,n);
56. m=fmod(m,n);
57.
58. printf("Message data = %lf",msg);
59. printf("\np = %lf",p);
60. printf("\nq = %lf",q);
61. printf("\nn = pq = %lf",n);
62. printf("\ntotient = %lf",totient);
63. printf("\ne = %lf",e);
64. printf("\nd = %lf",d);
65. printf("\nEncrypted data = %lf",c);
66. printf("\nOriginal Message Sent = %lf",m);
67.
68. return 0;
69. }

Program for RSA Algorithm in C++

/Program for RSA asymmetric cryptographic algorithm


//for demonstration values are relatively small compared to practical application

#include<iostream>
#include<math.h>

using namespace std;

//to find gcd


int gcd(int a, int h)
{
int temp;
while(1)
{
temp = a%h;
if(temp==0)
return h;
a = h;
h = temp;
}
}

int main()
{
//2 random prime numbers
double p = 3;
double q = 7;
double n=p*q;
double count;
double totient = (p-1)*(q-1);

//public key
//e stands for encrypt
double e=2;

//for checking co-prime which satisfies e>1


while(e<totient){
count = gcd(e,totient);
if(count==1)
break;
else
e++;
}

//private key
//d stands for decrypt
double d;

//k can be any arbitrary value


double k = 2;

//choosing d such that it satisfies d*e = 1 + k * totient


d = (1 + (k*totient))/e;
double msg = 12;
double c = pow(msg,e);
double m = pow(c,d);
c=fmod(c,n);
m=fmod(m,n);

cout<<"Message data = "<<msg;


cout<<"\n"<<"p = "<<p;
cout<<"\n"<<"q = "<<q;
cout<<"\n"<<"n = pq = "<<n;
cout<<"\n"<<"totient = "<<totient;
cout<<"\n"<<"e = "<<e;
cout<<"\n"<<"d = "<<d;
cout<<"\n"<<"Encrypted data = "<<c;
cout<<"\n"<<"Original Message sent = "<<m;

return 0;
}
Output:-

Conclusion:-

You might also like