Css Exp 3
Css Exp 3
Experiment No 3
Theory:
Introduction
Since this is asymmetric, nobody else except the browser can decrypt the data even if a third
party has the public key of the browser.
The idea! The idea of RSA is based on the fact that it is difficult to factorize a large integer. The
public key consists of two numbers where one number is a multiplication of two large prime
numbers. And private keys are also derived from the same two prime numbers. So if somebody
can factorize the large number, the private key is compromised. Therefore encryption strength
totally lies on the key size and if we double or triple the key size, the strength of encryption
increases exponentially. RSA keys can be typically 1024 or 2048 bits long, but experts believe
that 1024-bit keys could be broken in the near future. But till now it seems to be an infeasible
task.
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
the user's public key. But only the user can decrypt the message using his private key.
● The data to be sent is encrypted by sender A using the public key of the intended receiver
Vidya Vikas Education Trust’s
Universal College of Engineering, Kaman Road, Vasai-401208
Accredited by B+ Grade by NAAC
● 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 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:
● 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
Vidya Vikas Education Trust’s
Universal College of Engineering, Kaman Road, Vasai-401208
Accredited by B+ Grade by NAAC
● 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
Program
#required for the sqrt() function, if you want to avoid doing **0.5
import random
if b == 0:
return a
else:
return gcd(b, a % b)
if (a * x) % m == 1:
return x
return -1
def isprime(n):
if n < 2:
return False
elif n == 2:
return True
Vidya Vikas Education Trust’s
Universal College of Engineering, Kaman Road, Vasai-401208
Accredited by B+ Grade by NAAC
else:
if n % i == 0:
return False
return True
p = rand(1, 1000)
q = rand(1, 1000)
primes = [2]
# we choose two prime numbers in range(start, stop) so that the difference of bit lengths is at
most 2.
return []
for p in primes:
if i % p == 0:
break
else:
primes.append(i)
del primes[0]
while primes:
p = random.choice(primes)
primes.remove(p)
if q_values:
q = random.choice(q_values)
break
print(p, q)
n=p*q
phi = (p - 1) * (q - 1)
e = random.randrange(1, phi)
g = gcd(e, phi)
Vidya Vikas Education Trust’s
Universal College of Engineering, Kaman Road, Vasai-401208
Accredited by B+ Grade by NAAC
while True:
e = random.randrange(1, phi)
g = gcd(e, phi)
d = mod_inverse(e, phi)
if g == 1 and e != d:
break
e, n = package
return msg_ciphertext
d, n = package
return (''.join(msg_plaintext))
#-------------------------------------------------------------
#driver program
if __name__ == "__main__":
print("Running RSA...")
print(decrypt(encrypted_msg, private))
Vidya Vikas Education Trust’s
Universal College of Engineering, Kaman Road, Vasai-401208
Accredited by B+ Grade by NAAC
Output
Conclusion:
It is concluded that, while establishing RSA key pairs, usage keys and general-purpose keys are
integrated. In RSA keys, two key pairs are used for encryption and signatures. In the
General-purpose key, one single pair is used for both encryption and signature.