RSA Algorithm
Asymmetric Key cryptography Algorithm
RSA Algorithm
• Rivest Shamir Adleman
Steps
1. Choose two large prime numbers P and Q
2. Calculate N= P * Q
3. Select the public key (i.e. Encryption Key) E such that it is not a
factor of (P-1) and (Q-1)
4. Select the private key (i.e. Decryption Key) D such that the following
equation is true
(D * E) mod (P-1) * (Q-1) =1
5. For Encryption, Calculate the cypher text CT from the plain text PT
CT=PTE mod N
6. For Decryption, Calculate the plain Text PT from the Cipher text CT
PT=CTD mod N
Step 1:
1.Choose two large prime numbers P and Q
Let P=7 , Q=17
Step 2:
2. Calculate Φ(N)= P * Q
P=7 , Q=17
N = P * Q
N = 7 * 17
N = 119
Step 3:
3. Select the public key (i.e. Encryption Key) E such that it is not a factor
of (P-1) and (Q-1) i.e. (relatively prime to Φ(N) and less than Φ(N)
• Let us find Φ(N) =(7-1) * (17-1) = 6*16= 96
• The factor of 96 are 2,2,2,2,2 and 3 (Because 2*2*2*2*2*3 )
• Choose E such that E, such that none of the factors of E is 2 & 3
• We cannot choose E as 4 (Because it has 2 as a factor)
• We cannot choose E as 15 (Because it has 3 as a factor)
• We cannot choose E as 6 (Because it has 2 and 3 both as a factor)
• Lets choose E as 5 (it could ne any other number that does not have its factor as
2 and 3)
Step 4:
4. Select the private key (i.e. Decryption Key) D such that the following
equation is true
(D * E) mod (P-1) * (Q-1) =1
• Let us substitute the value of E , P and Q in the equation.
• Wee have (D *5 mod (7-1) * (17-1) = 1
• i.e. (D *5 ) mod (6) * (16) = 1
• i.e. (D *5 ) mod (96) = 1
• After some calculations , Let us take D=77, Then the following is true
• (77*5) mod 96 =385 mod 96 =1 (which is what we wanted)
Step 5:
5. For Encryption, Calculate the cypher text CT from the plain text PT
CT=PTE mod N
• Let us assume that we want to encrypt plain text 10 then we have
CT=PTE mod N
CT=105 mod 119
CT=100000 mod 119
CT= 40
Step 6:
6.For Decryption, Calculate the plain Text PT from the Cipher text CT
PT=CTD mod N
• Let us assume that we want to encrypt plain text 10 then we have
PT=CTD mod N
PT =4077 mod 119
PT=10
Example 2
Step 1:
1.Choose two large prime numbers P and Q
Let P=17 , Q=11
Step 2:
2. Calculate Φ(N)= P * Q
P=17, Q=11
N = P * Q
N = 17 * 11
N = 187
Step 3:
3. Select the public key (i.e. Encryption Key) E such that it is not a factor
of (P-1) and (Q-1) i.e. (relatively prime to Φ(N) and less than Φ(N)
• Let us find Φ(N)= (17-1) * (11-1) = 16*10= 160
• The factor of 160 are 2,2,2,2,2 and 5 (Because 2*2*2*2*2*5 )
• Choose E such that E, such that none of the factors of E is 2 & 5
• Lets choose E as 7
Step 4:
4. Select the private key (i.e. Decryption Key) D such that the following
equation is true
(D * E) mod (P-1) * (Q-1) =1
• Let us substitute the value of E , P and Q in the equation.
• Wee have (D *7 mod (17-1) * (11-1) = 1
• i.e. (D *7 ) mod (16) * (10) = 1
• i.e. (D *7 ) mod (160) = 1
• After some calculations , Let us take D=23, Then the following is true
• (23*7) mod 160 =161 mod 160 =1 (which is what we wanted)
Step 5:
5. For Encryption, Calculate the cypher text CT from the plain text PT
CT=PTE mod N
• Let us assume that we want to encrypt plain text 10 then we have
CT=PTE mod N
CT=887 mod 187
88 mod 187=88 ;
882 mod 187=7744 mod 187 =77;
884 mod 187=59969 mod 187=132
(88 * 77 * 132) mod 187
CT= 11
Step 6:
6.For Decryption, Calculate the plain Text PT from the Cipher text CT
PT=CTD mod N
• Let us assume that we want to encrypt plain text 10 then we have
PT=CTD mod N
PT =1123 mod 187
11 mod 187=11 ;
112 mod 187=121;
114 mod 187=1461 mod 187=55
118 mod 187=214358,881 mod 187=33
(11 * 121 * 55 *33 *33) mod 187 =88
Example 3
Step 1:
1.Choose two large prime numbers P and Q
Let P=7 , Q=11
Step 2:
2. Calculate Φ(N)= P * Q
P=7, Q=11
N = P * Q
N = 7 * 11
N = 77
Step 3:
3. Select the public key (i.e. Encryption Key) E such that it is not a factor
of (P-1) * (Q-1) i.e. (relatively prime to Φ(N) and less than Φ(N)
• Two numbers are said to be relatively prime if the common factor
between the numbers is one.
• For example, 34 and 35 are relatively prime. Factors of 34 are 1, 2,
17, 34, and factors of 35 are 1, 5, 7, 35. the HCF-Highest Common
Factor is one.
Step 3:
3. Select the public key (i.e. Encryption Key) E such that it is not a factor
of (P-1) and (Q-1) i.e. (relatively prime to Φ(N) and less than Φ(N)
• Let us find Φ(N) =(7-1) * (11-1) = 6*10= 60
• The factor of 60 are 2,2,5 and 3 (Because 2*2*5*3 )
• Choose E such that 1 <E < Φ(N)
• Choose E such that E, such that none of the factors of E is 2 ,3 & 5 (E must not
appear in the table of 2,3,5)
• If we consider the remaining prime number 7,11,13,…..
• 7 & 11 are already considered we can go ahead with 13
• Since 13 is not divisible by 2,3,& 5
• Lets choose E as 13
Step 4:
4. Select the private key (i.e. Decryption Key) D such that the following
equation is true
(D * E) mod (P-1) * (Q-1) =1
• Let us substitute the value of E , P and Q in the equation.
• Wee have (D *13 mod (7-1) * (11-1) = 1
• i.e. (D *13 ) mod (6) * (10) = 1
• i.e. (D *13 ) mod (60) = 1
Bezouts Thm.
• We use the Euclidean distance to calculate D
• Using the equation aX +bY = gcd(a,b)
• a=Φ ;b=E
• a = 60 & b =13
Φ X +EY = gcd(Φ,E)
60 X +13 Y=gcd(60,13)
Y=D
Solve until d=1 (note d !=D, b=D)
i) calculate k2 iv) calculate d3=d1-(d2*k2)
a b d K
d1/d2= 60/13 =4
1 1 0 60 - d3=60-(13*4)
2 0 1 13 4 =60-(52)
ii) calculate a3=a1-(a2*k2)
3 1 -4 8 1 =1-(0* 4) =8
=1 v) calculate k3
iii) calculate b3=b1-(b2*k2) d2/d3= 13/8 =1
=0-(1* 4)
=-4
Solve until d=1 (note d !=D, b=D)
a b d K iii) calculate d4=d2-(d3*k3)
1 1 0 60 - i) calculate a4=a1-(a3*k3) d4=13-(18*1)
2 0 1 13 4 =0-(1* 1) =13-(8)
3 1 -4 8 1 =-1 =5
4 -1 5 5 1 ii) calculate b4=b2-(b3*k3) iv) calculate k4
=1-(-4* 1) d3/d4= 8/5 =1
5
=5
6
7
Solve until d=1 (note d !=D, b=D)
a b d K Φ X +EY = gcd(Φ,E)
1 1 0 60 - 60 X +13 Y=gcd(60,13)
2 0 1 13 4 X=a and Y=b
3 1 -4 8 1 (60*5)+(13 *-23) =gcd(60,13)
4 -1 5 5 1 =1
5 2 -9 3 1 So d = - 23
6 -3 14 2 1
7 5 -23 1
Solve until d=1
a b d K But if d> Φ
1 1 0 60 - Then d= d mod Φ
2 0 1 13 4 But if d is negative
3 1 -4 8 1 Then d = d + Φ
4 -1 5 5 1 = -23 +60
5 2 -9 3 1 = 37
6 -3 14 2 1
7 5 -23 1 CT=PTE mod N PT=CTD mod N
CT= PT13 mod 77 PT=CT37 mod 77
Diffie Hellman Key exchange
algorithm
This algorithm is only used for key
agreement and not for Encryption and
decryption
Simple explanation
Step 1
Alice & Bob Agree on two large
prime numbers n and g
These two integers n and g need not
be kept secret, Alice & Bob can use a
insecure channel to agree on them
Simple explanation
Step 2
Alice chooses another large random
number X a calculate A such that
A=gX mod n
Step 3:Alice sends A to Bob
Simple explanation
Step 4
Bob chooses another large random
number Y and calculate B such that
B=gY mod n
Step 5: Bob sends B to Alice
Simple explanation
Step 6
Alice now computes secret key K1
K1=BX mod n
Simple explanation
Step 6
Bob now computes secret key K2
K2=AY mod n
Simple explanation
Step 1
K1 = K2 = K = key
K becomes the secret key between
Alice & Bob
Example
• Alice & Bob Agree on two large prime numbers n and g
n = 11 and g= 7
Alice chooses another large random number X a
calculate A such that (Let X=3)
X
A=g mod n
3
A=7 mod 11
A=343 mod 11 A=2
Example
Bob chooses another large random number Y
and calculate B such that (Let Y=6)
Y
B=g mod n
6
B=7 mod 11
B=117649 mod 11 B=4
A=2 sent to Bob
B=4 sent to Alice
Alice now computes Bob now computes
secret key K1 secret key K2
K1=BX mod n K2=AY mod n
K1 =43 mod 11 K2=26 mod 11
K1= 9 K2=9
K1=K2= k= 9
Mathematical Theory behind the algorithm
In Step 6
Alice computes secret key K1
K1=BX mod n
Alice computes secret key K1
If we substitute the value of B in step 6
B=gY mod n
K1=(gy)Xmod n
Mathematical Theory behind the algorithm
In Step 7
Bob computes secret key K2
K2=AY mod n
Bob computes secret key K2
If we substitute the value of A in step 7
A=gX mod n
K2 =(gX)Ymod n
K1=(gy)Xmod n K2 =(gX)Ymod n
K1=K2= k= 9
• The final symmetric key between Alice & Bob is made up
of three parts g, X and Y
• The first part of the Key i.e. g is a public number known
to everyone
• The other two parts of the key i.e. X and Y must be made
available by Alice and Bob
Man in the Middle attack
Man in the Middle attack
• Alice wants to communicate with Bob securely and therefore she
wants to do a Diffie Hellman key exchange with him
• She sends the value of n ang g to bob
n=11 and g=7
Man in the Middle attack
• Alice has not realized that Tom is listening to their communication
between her and Bob.
• Tom simply picks up the value oh n and g and also forwards them to
Bob
Alice Tom Bob
n=11 and g=7 n=11 and g=7 n=11 and g=7
Man in the Middle attack
• Assuming that Alice ,Tom and Bob Select random numbers X and Y
• Tom simply picks up the value oh n and g and also forwards them to
Bob
Alice Tom Bob
X=3 X= 8 and Y= 6 Y=9
Man in the Middle attack
Alice Bob
Calculates A Calculates B
Tom
Calculates A & B
Man in the Middle attack
Tom
Alice Bob
Calculates A & B
Calculates A Calculates B
A=gX mod n
A=gX mod n B=gY mod n
A=78 mod 11
A=73 mod 11 B=79 mod 11
A=5764801mod 11
A=343 mod 11 B=40353607mod 11
A=9
A=2 B=8
B=gY mod n
B=76 mod 11
B=117649 mod 11
B=4
Man in the Middle attack- Expected values
Alice Bob
Expects Tom Generated Expects
A=9 & B=4
A=2 & B=8 B=8 & A=2
Interference of Tom
Alice Bob
Received A=2 & B=8 Received
A=2 & B=4 B=8 & A=9
Man in the Middle attack Calculate Key
Tom
Alice Bob
Calculates K1 & K2
Calculates K1 Calculates K2
K1=BX mod n K2 =AY mod n
K1=BX mod n
K1 =88 mod 11 K2 =99 mod 11
K1 =43 mod 11
K1 =16777216mod 11 K2 =387420489mod 11
K1 =64 mod 11
K1 =5 K2 =5
K1 =9
K2 =AY mod n
K2 =26 mod 11
K2 =64 mod 11
K2 =9