0% found this document useful (0 votes)
32 views13 pages

CS - Lab Assignment-3

Uploaded by

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

CS - Lab Assignment-3

Uploaded by

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

Name: Alester Dsouza

Reg. Id: 201080004

Course: Cyber Security

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:

RSA Encryption Algorithm

RSA encryption algorithm is a type of public-key encryption algorithm. To


better understand RSA, lets first understand what is public-key encryption
algorithm.

Public key encryption algorithm:

The Public Key encryption algorithm is also called the Asymmetric


algorithm. Asymmetric algorithms are those algorithms in which sender
and receiver use different keys for encryption and decryption. Each sender
is assigned a pair of keys:

● 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 encryption algorithm:

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:

● Select two large prime numbers, p and q.

● Multiply these numbers to find n = p x q, where n is called the modulus for


encryption and decryption.

● Choose a number e less than n, such that n is relatively prime to (p - 1) x (q


-1). It means that e and (p - 1) x (q - 1) have no common factor except 1.
Choose "e" such that 1<e < φ (n), e is prime to φ (n), gcd (e,d(n)) =1

● If n = p x q, then the public key is <e, n>. A plaintext message m is encrypted


using public key <e, n>. To find ciphertext from the plain text following
formula is used to get ciphertext C.
C = me mod n
Here, m must be less than n. A larger message (>n) is treated as a
concatenation of messages, each of which is encrypted separately.

● 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

● It is very easy to implement RSA algorithm.

● RSA algorithm is safe and secure for transmitting confidential data.

● Cracking RSA algorithm is very difficult as it involves complex mathematics.

● Sharing public key to users is easy.


Disadvantages of RSA

● It may fail sometimes because for complete encryption both symmetric


and asymmetric encryption is required and RSA uses asymmetric
encryption only.

● It has slow data transfer rate due to large numbers involved.

● It requires third party to verify the reliability of public keys sometimes.

● High processing is required at receiver’s end for decryption.

● RSA can’t be used for public data encryption like election voting.
CODE
import java.math.BigInteger;

import java.util.*;

class rsa

public static void main(String args[])

Scanner ip=new Scanner(System.in);

int p,q,n,e=1,j;

int d=1,i1;

int t1,t2;

int pt[]= new int[10];

int ct[]= new int[10];

int rt[]= new int[10];

int temp[]= new int[10];

String i=new String();

System.out.println("Enter the two prime


numbers:");

p=ip.nextInt();
q=ip.nextInt();

System.out.println("Enter the message to be


sent");

i=ip.next();

i1=i.length();

n=p*q;

t1=p-1;

t2=q-1;

//RSA algorithm to encrypt and decrypt the


data.

System.out.println("\n ---------------------------------------- ");

System.out.println("Sender Side:");

while((t1*t2)%e==0)

e++;

System.out.println("Public Key(e)= "+e);

System.out.println(" ---------------------------------------- ");


for(j=0;j<i1;j++)

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("Cipher Text= "+ct[j]);

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++;

//RSA algorithm to encrypt and decrypt the


data.

System.out.println("Private Key(d)= "+d);

System.out.println(" ---------------------------------------- ");

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("Plain Text= "+rt[j]);

}
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]);

System.out.println("\n ---------------------------------------- ");


ip.close();

}
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.

You might also like