RSA Algorithm in Cryptography
RSA Algorithm in Cryptography
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.
The idea! The idea of RSA is based on the fact that it is difficult to factorize a large integer. The public
key consists of two numbers where one number is multiplication of two large prime numbers. And private
key is also derived from the same two prime numbers. So if somebody can factorize the large number,
the private key is compromised. Therefore encryption strength totally lies on the key size and if we double
or triple the key size, the strength of encryption increases exponentially. RSA keys can be typically 1024
or 2048 bits long, but experts believe that 1024 bit keys could be broken in the near future. But till now it
seems to be an infeasible task.
Let us learn the mechanism behind RSA algorithm :
// Message to be encrypted
double msg = 20;
// Encryption c = (msg ^ e) % n
double c = pow(msg, e);
c = fmod(c, n);
printf("\nEncrypted data = %lf", c);
// Decryption m = (c ^ d) % n
double m = pow(c, d);
m = fmod(m, n);
printf("\nOriginal Message Sent = %lf", m);
return 0;
}
// This code is contributed by Akash Sharan.
Run on IDE
Output :