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

CE-408: Cryptography & Network Security SSUET/QR/114

LAB#07
Public Key Cryptography – RSA Algorithm

OBJECTIVE

The purpose of this lab is to implement RSA Algorithm and use it for encryption and to
perform digital signature in MATLAB.

THEORY

Traditional private/secret/single key cryptography uses one key which is shared by both
sender and receiver. If this key is disclosed then communications are compromised. As a
result, another approach uses two keys – a public & a private key which is called
Asymmetric since parties use different keys and they are not equal. If one is encrypting
using a key, then the other cannot decrypt using the same key.

The public key cryptosystems can be classified into 3 categories:


Encryption/decryption (provide secrecy)
Digital signatures (provide authentication)
Key exchange (of session keys)
Some algorithms are suitable for all uses, others are specific to one

RSA:

Developed by Rivest, Shamir & Adleman of MIT in


1977 Best known & widely used public-key scheme
Based on exponentiation in a finite (Galois) field over integers modulo
a prime Uses large integers (e.g. 1024 bits)
Security due to cost of factoring large numbers

Lab#07: RSA Algorithm Page | 27


CE-408: Cryptography & Network Security SSUET/QR/114

Procedure:

Each user generates a public/private key pair


by: Selecting two large primes at random p,q
Computing their system modulus n = p.q and ø(n)=(p-1)(q-1)
Selecting at random the encryption key e where 1<e<ø(n), gcd(e,ø(n))=1
Solve following equation to find decryption key d such that
e.d = 1 mod ø(n) and 0 ≤ d ≤ n and gcd(e,ø(n)) = 1

d is nothing but the multiplicative inverse of e and vice versa in mod ø(n)
Publish their public encryption key: KU = {e,n}
Keep secret private decryption key: KR = {d,n}
To encrypt a message M the sender obtains public key of recipient KU={e,n}
e
Computes: C = M mod n, where 0≤M<n
To decrypt the ciphertext C the owner uses their private
d
key KR={d,n} Computes: M = C mod n
Note that the message M must be smaller than the modulus n

Matlab code:

The only primary operation here is modulo operation. Use mod command for
evaluating modulo of two numbers.
To generate random prime numbers, use the command primes(n).
To find primitive root of a number, use if statement and mod function.
Use gcd(a,b) command to verify whether e and d are relatively prime to ø(n)
To find the multiplicative inverse a number, use series of if statements and mod function.

Lab#07: RSA Algorithm Page | 28


CE-408: Cryptography & Network Security SSUET/QR/114

Lab Tasks:
Perform the following tasks

1. Perform the block shown below such that RSA algorithm is used. Use the
following parameters to create the whole scenario.
User A: generates two prime numbers q = 3 and p = 5 and e = 7. The message M to be sent
by this user to user B is M = [0 1 1 0].

User B: generates two prime numbers q = 7 and p = 3 and e = 11.

CODE:
clc
clear all
% User A
p = 5; q
= 3; n =
p*q; e =
7;
M=[0110];
Mdec = bin2dec(int2str(M));
C = mod(Mdec^e, n) % Encrypt
% User B
p = 3;
q = 7;
n = p*q;
e = 11;
D = mod(C^e, n) % Decrypt
OUTPUT:

Lab#07: RSA Algorithm Page | 29


80
2. Using an appropriate method, find (20 mod 95) in MATLAB.

CODE:
C = mod(20^80, 95)

OUTPUT:

Lab#07: RSA Algorithm Page | 30

You might also like