Css Exp 5
Css Exp 5
THEORY:
1. RSA encryption algorithm is a type of public-key encryption algorithm.
2. 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:
a. Public key
b. Private key
3. The Public key is used for encryption, and the Private Key is used for decryption.
4. RSA is the most common public-key algorithm, named after its inventors Rivest,
Shamir, and Adelman (RSA).
CODE:
import math
def gcd(a, h):
temp = 0
while(1):
temp = a % h
if (temp == 0):
return h
a=h
h = temp
p = int(input("Enter value of p: "))
q = int(input("Enter value of q: "))
e=2
n = p*q
phi = (p-1)*(q-1)
# d is multiplicative inverse of e
#d = (1 + (k*phi))/e
for d in range(1, phi):
if (((e % phi) * (d % phi)) % phi == 1):
break
# Encryption c = (msg ^ e) % n
a = pow(msg, e)
c=a%n
print("Encrypted data is ", c)
# Decryption m = (c ^ d) % n
b = pow(c, d)
m=b%n
print("Original Message is ", m)
OUTPUT:
Enter value of p: 3
Enter value of q: 7
Enter message to be encrypted: 12
Message data is 12
Encrypted data is 3
Original Message is 12