0% found this document useful (0 votes)
4 views

CSS_EXP 2_66

The document outlines an experiment on the implementation and analysis of the RSA cryptosystem, focusing on public key generation, encryption, and decryption processes. It details the steps for generating RSA key pairs, including selecting prime numbers and calculating public and private keys, as well as providing a sample Java code for encryption and decryption. The conclusion emphasizes RSA's strength as a secure encryption algorithm based on the difficulty of factoring large numbers.

Uploaded by

amishav2004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

CSS_EXP 2_66

The document outlines an experiment on the implementation and analysis of the RSA cryptosystem, focusing on public key generation, encryption, and decryption processes. It details the steps for generating RSA key pairs, including selecting prime numbers and calculating public and private keys, as well as providing a sample Java code for encryption and decryption. The conclusion emphasizes RSA's strength as a secure encryption algorithm based on the difficulty of factoring large numbers.

Uploaded by

amishav2004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment No.

2
Name: Amisha Verma
Roll No. 66
Year: 2024-25 Batch- C
Subject : Cryptography and System Security

Aim: Implementation and analysis of RSA cryptosystem

Objectives:
• To understand the concept of public key cryptosystem.
• To understand the RSA cryptosystem.
• To understand RSA Digital signature scheme.
• To understand the possible attacks on RSA cryptosystem.

Outcomes: The learner will be able to Apply the cryptosystem to ensure confidentiality and integrity of
information.

Hardware / Software Required: C/C++/JAVA.


Theory:

RSA was invented by Ron Rivest, Adi Shamir, and Len Adleman and hence, it is termed as RSA
cryptosystem. We will see two aspects of the RSA cryptosystem, firstly generation of key pair and
secondly encryption-decryption algorithms.

Generation of RSA Key Pair


Each person or a party who desires to participate in communication using encryption needs to
generate a pair of keys, namely public key and private key. The process followed in the generation
of keys is described below −
● Generate the RSA modulus (n)
○ Select two large primes, p and q.
○ Calculate n=p*q. For strong unbreakable encryption, let n be a large number,
typically a minimum of 512 bits.
● Find Derived Number (e)
○ Number e must be greater than 1 and less than (p − 1)(q − 1).
○ There must be no common factor for e and (p − 1)(q − 1) except for 1. In other
words two numbers e and (p – 1)(q – 1) are coprime.
● Form the public key
The pair of numbers (n, e) form the RSA public key and is made public.
○ Interestingly, though n is part of the public key, difficulty in factorizing a large prime
number ensures that attacker cannot find in finite time the two primes (p & q) used to
obtain n. This is strength of RSA.
● Generate the private key
○ Private Key d is calculated from p, q, and e. For given n and e, there is unique
number d.Number d is the inverse of e modulo (p - 1)(q – 1). This means that
d is the number less than (p - 1)(q - 1) such that when multiplied by e, it is
equal to 1 modulo (p - 1) (q-1)

• This relationship is written mathematically as follows − ed = 1 mod (p − 1)(q − 1)


The Extended Euclidean Algorithm takes p, q, and e as input and gives d as output.

Example
An example of generating RSA Key pair is given below. (For ease of understanding, the primes
p & q taken here are small values. Practically, these values are very high).
● Let two primes be p = 7 and q = 13. Thus, modulus n = pq = 7 x 13 = 91.
● Select e = 5, which is a valid choice since there is no number that is common factor of 5 and
(p − 1)(q − 1) = 6 × 12 = 72, except for 1.
● The pair of numbers (n, e) = (91, 5) forms the public key and can be made available to
anyone whom we wish to be able to send us encrypted messages.
● Input p = 7, q = 13, and e = 5 to the Extended Euclidean Algorithm. The output will be d =
29.
● Check that the d calculated is correct by
computing − de = 29 × 5 = 145 = 1 mod 72
● Hence, public key is (91, 5) and private keys is (91, 29).

Encryption and Decryption


Once the key pair has been generated, the process of encryption and decryption are relatively
straightforward and computationally easy.
RSA Encryption
● Suppose the sender wish to send some text message to someone whose public key is (n, e).
● The sender then represents the plaintext as a series of numbers less than n.
● To encrypt the first plaintext P, which is a number modulo n. The encryption process is simple
mathematical step as −
C = Pe mod n
● In other words, the ciphertext C is equal to the plaintext P multiplied by itself e times and
then reduced modulo n. This means that C is also a number less than n.
● Returning to our Key Generation example with plaintext P = 10, we get
ciphertext C − C = 105 mod 91
RSA Decryption
● The decryption process for RSA is also very straightforward. Suppose that the receiver of
public-key pair (n, e) has received a ciphertext C.
● Receiver raises C to the power of his private key d. The result modulo n will be the plaintext
P.
Plaintext = Cd mod n
● Returning again to our numerical example, the ciphertext C = 82 would get decrypted to
number 10 using private key 29 −
Plaintext = 8229 mod 91 = 10

Source Code:
import java.util.*;
class Exp1
{

public static void main(String args[])


{
Scanner sc=new Scanner(System.in);
int d=0;
System.out.println("Enter two prime numbers");
int p=sc.nextInt();

int q=sc.nextInt();
int n=p*q;
System.out.println("n="+n);
int e=0;

int pn=(p-1)*(q-1);
search:

{
for(int i=2;i<=pn;i++)
{
int r;
int j=i;
int k=pn;
while(k != j)

{
if(k > j)
k = k-j;
else
j = j-k;

}
if(k==1)
{
e=i;
break search;
}
}
}
System.out.println("e="+e);
go:{
for(int i=1;i<=pn;i++)

{
int x=(e*i)%pn;
if(x==1)
{
System.out.println("d="+i);
System.out.println("The private key is (d) "+i);
d=i;
break go;
}
}
}
System.out.println("The public key is (n,e) "+n+", "+e);
String t;
int c;

System.out.println("Enter plaintext");
t=sc.next();
int m = 0;

for (int i = 0; i < t.length(); i++){


m += (int)t.charAt(i);
}
c=((m)^e)%n;
System.out.println("The Encryted message is "+m);
m=(c^d)%n;
System.out.println("The decrypted message is "+t);

}
}

Output:

Conclusion:
RSA is a strong encryption algorithm. RSA implements a public-key cryptosystem
that allows secure communications and digital signatures, and its security rests
in part on the difficulty of factoring large numbers.

You might also like