CS - Lab Assignment-3
CS - Lab Assignment-3
Batch: A
CS LAB ASSIGNMENT 3
Aim:
To understand the working of RSA algorithm and to execute the algorithm
to understand its importance, advantages and disadvantages.
Thoery:
● Public key
● Private key
The Public key is used for encryption, and the Private Key is used for
decryption. Decryption cannot be done using a public key. The two keys are
linked, but the private key cannot be derived from the public key. The public
key is well known, but the private key is secret and it is known only to the
user who owns the key. It means that everybody can send a message to
the user using user's public key. But only the user can decrypt the message
using his private key.
The Public key algorithm operates in the following manner:
● The data to be sent is encrypted by sender A using the public key of the
intended receiver
● B decrypts the received ciphertext using its private key, which is known
only to B. B replies to A encrypting its message using A's public key.
● A decrypts the received ciphertext using its private key, which is known
only to him.
RSA is the most common public-key algorithm, named after its inventors Rivest,
Shamir, and Adelman (RSA).
RSA algorithm uses the following procedure to generate public and private keys:
● To determine the private key, we use the following formula to calculate the
d such that:
De mod {(p - 1) x (q - 1)} = 1
Or
De mod φ (n) = 1
● The private key is <d, n>. A ciphertext message c is decrypted using private
key <d, n>. To calculate plain text m from the ciphertext c following formula
is used to get plain text m.
m = cd mod n
Advantages of RSA
● RSA can’t be used for public data encryption like election voting.
CODE
import java.math.BigInteger;
import java.util.*;
class rsa
int p,q,n,e=1,j;
int d=1,i1;
int t1,t2;
p=ip.nextInt();
q=ip.nextInt();
i=ip.next();
i1=i.length();
n=p*q;
t1=p-1;
t2=q-1;
System.out.println("Sender Side:");
while((t1*t2)%e==0)
e++;
pt[j]=(i.charAt(j))-96;
// System.out.println("Plain Text=
"+pt[j]);
ct[j]=((int)Math.pow(pt[j],e))%n;
System.out.println("\nTransmitted Message:");
for(j=0;j<i1;j++)
temp[j]=ct[j]+96;
System.out.print((char)temp[j]);
System.out.println("\n\n----------------------------------------- ");
System.out.println("Receiver Side:");
while((d*e)%(t1*t2)!=1)
d++;
for(j=0;j<i1;j++)
//System.out.println("cipher Text=
"+ct[j]);
BigInteger very_big_no =
BigInteger.valueOf(ct[j]);
very_big_no = very_big_no.pow(d);
very_big_no =
very_big_no.mod(BigInteger.valueOf(n));
rt[j] = very_big_no.intValue();
}
System.out.println("\n ---------------------------------------- ");
System.out.println("Decrypted Message:");
for(j=0;j<i1;j++)
rt[j]=rt[j]+96;
System.out.print((char)rt[j]);
}
10
11
EXPERIMENT RESULTS
CONCLUSION
In this experiment, I have successfully learned how the RSA algorithm works,
executed the algorithm and what are the advantages and disadvantages of it.