Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

EXPERIMENT NO.

-04

Aim of the experiment:-To derelof o progom huz Pmplemnttn9 enhyon


Chypen vStng RSA kchntque .
Course Outcome- To be able to apply the knowledgeof symmetricand asymmetric
cryptography to Implementsimple ciphers

Date of Conduction: 02/02ho?3 Date of Submission:_

Timely Total
Understanding
Submission Pyochialrtty (15)

Practical
e223
Incharge
EXPERIMENT NO-04

Aim: Developa program to implementRSA algorithm for encryption and decryption. This
is one the initial system. It remains most employed cryptosystem
cryptosystem
even today. The system was invented by three scholars Ron Rivest, Adi Shamir,
and Len Adleman and hence, it is termed as RSA cryptosystem. The two aspects
of the RSA cryptosystem, firstly generation of key pair and secondly encryption-
decryption algorithms

Theory:

Generation ofRSA Key Pair

Each person or a party who desires to participate in communication using

encryption needs to generate a pair of keys, namelypublic 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, letn be a large

number, typically a minimum of 512 bits.

Find Derived Number (e)

Numberemust be greater than l andless than (p


1)q 1).
There must be no common factor for e and (p-
1g - 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.

though n is part of
the public key, difficulty in
Interestingly,

number ensures that attacker cannot find in


factorizing a large prime
finite time the two primes (p & 9) used to obtain n. This is strength

ofRSA.

Generate the private key


e. For given n and there
Private Key d is calculated from p, q, and e,

is unique number d.
Number d is the inverse of emodulo (p - 1)(q- 1). This means that
disthenumber less than (p- 1q- 1) such that when multiplied by
e, it is equal to I modulo (p -
1q
-
1).

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

The Extended Euclidean Algorithm takes p, q, and e as input


and gives d as

output.

ALGORITHM:
1. Key Generation

i. Choose two distinct prime numbersp and q.


i. Find n such that n= pq, n will be used as themodulus

for both the public and private keys.

ii. Find the totient of n, d(n) (n)F(p-10q-1)

iv. Choose an e such that < e < o(n), and such that
1 eand
other than (e and p(n) are
o(n) share no divisors
1

relatively prime). e is kept as the public

key exponent
the
v. Determine d (using modular arithmetic) which satisfies

relation
congruence
de 1 (mod r(n)).

The public key has modulusn and the public (or

encryption) exponent
e. The private key has modulusn and the private (or

which is kept secret.


decryption) exponent d,

2. Encryption
c m°(mod n).

3. Decryption:
m = c° (mod n).

4. Stop.

PROGRAM:

import java.io.*;
import java.math.*;
import java.util.*
public class
RSA
double h)
public static double gcd(double a,

double temp;
while (true){
temp a %h;
=
if(temp=0)
return h;
a=h;
h= temp;

static void main(String[] args)


public

Console c =System.console);
try
the number P: ");
double p =Double.parseDouble(c.readLine("Enter the number Q: ")):
double q =Double.parseDouble(c.readLine("Enter
double n
double
p *9%
e= *
doublephi (p- 1) (q- 1); =
while (e< phi) {
if(gcd(e, phi)== 1)
break;
else
et+

int
k =2;
double d=(1 +(k phi))/e;
= * the Message: ");
double msg Double.parseDouble(c.readLine("Enter

"
** ****** ******* ********** ");
System.out.println("..
data =
**********************
+ msg);
System.out.println("Message
double ca =
Math.pow(msg, e);
ca ca % n; = ="+
data ca);
=
System.out.println("Encrypted
double m
m =m % n
Math.pow(ca, d)
="+ m);
Sent
System.out.println("Original Message
}catch(NumberFormatException e){
Numbers Allowed");;
System.out.println("Only

OUTPUT:
UPTER
PROBLEMS 10 OUTPUT DEBUC CONSOLETERMNAL
RuntRailFe
Windaws PowerShell
All rights reserved.
Rur RSA

Copyright (C)icrosoft Corporátian.

PoerShell https://aka.ns/pscores
Try the new cross-platfor

iisInëxCepticniessagescp C:sers
PS C:Project \CSS> & 'C:Progran Files Javal jdk-18.8.2\binijava.ere-0K:+Shoutodebeta
ysiCSs_ bbabbc15\bin
Vinayak v Utekar\AppDatatRoaming\Code\Usertuorkspacesturagel15%c2e5edado62971fdëb6ebe189a72e\redhat.java\jot
FSA
Enter the nunber P
:7
Enter the number Q:
Enter tne iessa *****************°****

12.08
essage data
Encrypted data 3.8
=
12.0
Original Message Sent

PS C:\Project\cSs>

In 46.Col2A1RA
VTnayak V. Ukkor OATE
Ta-BLormps41
Pan

QWhat s RSA3
AThe RSA Algtt n aymnet tPtngnphy algarthm thsg.

mians that tvies pbtc Ky parte Kiy Shatd puby,


h
Lrle aprtvate Kay 1 Secet k must net bhe shotd

Onyon&

SongShong prmeS nelassony n RA2


A has ofen
use so
SHonq
laled
pmesoR
sugaested
Stong pHmes p&q
hoSe th
o
geneate
Caytath
e
hat tn:choosenq a kay par,ona Shoua
modles &
pepertres hot maka
He pdut & havd t actor b Speuefe oetoHng rmotheds
* has poS ble hat n peaerng aloarthm may be deleped

fn the R wihonte gatn etpmes wh cstnin_


pmpetres f So chootno tng pmmes moy Onte ogan
help to înfak Seluy

3 Cen RA not of deshnt prmes'

Th The
ofHom The
ae enevgh numberS hat_ReA users
pne Number beytm Sntes
n4er
he nmbe
l
at
o_ptmesss bhan o egal on
is a asynp tvbaly n.
Thes hat the nmbey
eanf a prm nmbea ef ergth 512
bRSoX ssS
hon he nmbe
lo
So

of atoms tn
h s
h
o nmber 9eat
9Pen hiese

hat o2 he alematie & RSA2

AJWe Lon use RSA for-both Stgnatek encyphronbt netd


drent algotams f bat.y2 KCA ESTOAE P an egpton
algaetm
ha
ue
eSom ASASS-£ss aslgnabse
algof thm for both
bot t
algarkm:TextbsK
S not ece.
RA
ow fast RSA3
A e RSAalgethm has hemeam
mpleneste
o pedakton a 293MHz, hr 64brt uhth es haue
fa
hom he ecrer epted enes RA 1
pbp Key a |
yptgmphy
what would Pt oke to bfak RSAZ
P Lka mast Cpogosyslem He Setrty o RIA on how Pt
fmplemintested&used
o ha
0ns
inpatont fatoY he 2e
1he lergey He nimbe ok br tn a
kay
ky
s
he me dsfhaAE_ft b cek hagh ttaklSuh
brt -Fx'ng k_fexerng
0 a
uw loge malus Kay fS Use Pn RSA
AJ x
1he
s208
lommended RSA meduls s12Rhy malt etig
Lit yol bitsThs he prme to be
9enyaed nad be o24 L?t 2048 L?t Long

Ho How legeShevd ha p be_


A:J 1h_p1m umber w be 9entztd naj tobe lo24 bt
t 20k b_ Long

8How_dajau_Vnow a numbey pEme>

A 1F a number has onky hus thas fotoy 1ktef


hen numer PS paim
bw sRSA Wed hr ouhonhtatton For Prla

RSA 1S Svoly Combine th a hash enttren byn


u hoS Sond qned mage
a

bobSh ppresa hash hnckton amesspge


bob
CPate o moSfage dtaest,th S nes
a4 tta
msogo
Pblr koy
pnphy
hat da
Cptgphy How
pu man by
y
Secet ky
ae
cry
drFePnt hrom op on tsy

A at Koy
Ser
Tt arned
Pblrc y- thae
as Ha
) AeHred as ha kdntue
hat vses
bo drtleent Key
atuseS Stngle a
shotd Koy

encpt docayt k er
enyptran kdocayptvon
messoe

mmetsc TtPs als Calboy Asynmatc


i)Tt
llalledalled
Kiy encypton-

Kehoiga
tbt mesoge
omont
v)Apot hom Snds etves |* 1 h to use

prote ay fs kpt
Secet k
not pblfc
DRTE

aLht do we Knowabouk RSAT

AJRSA
both
an amtsc
&pat key
algorfhm h£r
A doneS
ens vjes
tt ves
geiteA_pslkc
both pule) Pc

not R ched wíh aryon

You might also like