Experiment-8: To Implement RSA Encryption-Decryption

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Experiment-8

Aim :
To implement RSA encryption-decryption.

Description :
RSA algorithm is asymmetric cryptography algorithm. Asymmetric actually means that it
works on two different keys i.e. Public Key and Private Key. As the name describes that the
Public Key is given to everyone and Private key is kept private.
An example of asymmetric cryptography :
1) A client (for example browser) sends its public key to the server and requests for
some data.
2) The server encrypts the data using client’s public key and sends the encrypted data.
3) Client receives this data and decrypts it.
Since this is asymmetric, nobody else except browser can decrypt the data even if a third
party has public key of browser.

Algorithm :
● Generating Public Key :
1. Select two prime no's. Suppose P = 53 and Q = 59. 2.
Now First part of the Public key : n = P*Q = 3127 3.
We also need a small exponent say e :
4. But e Must be
○ An integer.
○ Not be a factor of n.
5. 1 < e < Φ(n) [Φ(n) is discussed below],
6. Our Public Key is made of n and e

● Generating Private Key :


1. We need to calculate Φ(n) :
Such that Φ(n) = (P-1)(Q-1)
so, Φ(n) = 3016
2. Now calculate Private Key, d :
d = (k*Φ(n) + 1) / e for some integer k

Code:
#include<iostream>
#include<math.h>
#include<string.h>
#include<stdlib.h>

using namespace std;

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();
int prime(long int pr) {
int i;
j = sqrt(pr);
for (i = 2; i <= j; i++) {
if (pr % i == 0)
return 0;
}
return 1;
}

int main() {
cout << " Enter first prime number : ";
cin >> p;
flag = prime(p);
if (flag == 0) {
cout << "\n WRONG INPUT \n";
exit(1);
}
cout << " Enter second prime number : ";
cin >> q;
flag = prime(q);
if (flag == 0 || p == q) {
cout << "\n WRONG INPUT \n";
exit(1);
}
cout << " Enter plain text : ";
fflush(stdin);
cin >> msg;
for (i = 0; msg[i] != '\0'; i++)
m[i] = msg[i];
n = p * q;
t = (p - 1) * (q - 1);
ce();
cout << "\n Possible values of 'e' and 'd' are \n";
for (i = 0; i < j - 1; i++)
cout << " " << e[i] << "\t" << d[i] << "\n";
encrypt();
decrypt();
cout<<"\n";
return 0;
}

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;
cout << "\n Cipher text : ";
for (i = 0; en[i] != -1; i++)
cout<<char(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;
cout << "\n Decrypted text : ";
for (i = 0; m[i] != -1; i++)
cout<<char(m[i]);
}

Output :
Discussion :

RSA derives its security from the difficulty of factoring large integers that are the product of
two large prime numbers. Multiplying these two numbers is easy, but determining the
original prime numbers from the total -- or factoring -- is considered infeasible due to the
time it would take using even today's supercomputers. The public and private key
generation algorithm is the most complex part of RSA cryptography. Two large prime
numbers, p and q, are generated using the Rabin-Miller primality test algorithm. A modulus,
n, is calculated by multiplying p and q. This number is used by both the public and private
keys and provides the link between them. Its length, usually expressed in bits, is called the
key length.

Finding and Learning :

RSA security relies on the computational difficulty of factoring large integers. As computing
power increases and more efficient factoring algorithms are discovered, the ability to factor
larger and larger numbers also increases. Encryption strength is directly tied to key size, and
doubling key length can deliver an exponential increase in strength, although it does impair
performance. RSA keys are typically 1024- or 2048-bits long, but experts believe that 1024-
bit keys are no longer fully secure against all attacks. This is why the government and some
industries are moving to a minimum key length of 2048-bits. Barring an unforeseen
breakthrough in quantum computing, it will be many years before longer keys are required.

You might also like