0% found this document useful (0 votes)
2 views2 pages

CSSEXP2B

This document outlines the implementation of the RSA digital signature algorithm, including functions for modular exponentiation, calculating modular inverses, and encrypting messages. It provides a step-by-step process for signing and verifying messages using RSA encryption. The program takes user input for prime numbers, public exponent, and plaintext, then outputs the ciphertext, signature, and verification results.

Uploaded by

saakshi nalawade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

CSSEXP2B

This document outlines the implementation of the RSA digital signature algorithm, including functions for modular exponentiation, calculating modular inverses, and encrypting messages. It provides a step-by-step process for signing and verifying messages using RSA encryption. The program takes user input for prime numbers, public exponent, and plaintext, then outputs the ciphertext, signature, and verification results.

Uploaded by

saakshi nalawade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

EXPERIMENT NO.

2B

RSA DIGITAL SIGNATURE ALGORITHM:


INPUT :
# Function to calculate (a^b) % mod using fast exponentiation
def mod_exp(a, b, mod):
result = 1
a = a % mod
while b > 0:
if b % 2 == 1:
result = (result * a) % mod
a = (a * a) % mod
b //= 2
return result

# Function to calculate the modular inverse of a under modulo m using Extended Euclidean Algorithm
def mod_inverse(a, m):
m0, x0, x1 = m, 0, 1
while a > 1:
q = a // m
m, a = a % m, m
x0, x1 = x1 - q * x0, x0
if x1 < 0:
x1 += m0
return x1

# RSA Encryption function


def rsa_encrypt(p, q, e, plaintext):
# Step 1: Calculate modulus n
n=p*q
# Step 2: Calculate Euler's Totient function φ(n) = (p-1)*(q-1)
phi_n = (p - 1) * (q - 1)
# Step 3: Calculate the private exponent d (modular inverse of e mod φ(n))
d = mod_inverse(e, phi_n)
# Step 4: Encrypt the plaintext
ciphertext = mod_exp(plaintext, e, n)
return ciphertext, n, d

# RSA Digital Signature: Sign the message


def sign_message(plaintext, d, n):
# Sign the message using the private key (signature = plaintext^d % n)
signature = mod_exp(plaintext, d, n)
return signature

# RSA Digital Signature: Verify the signature


def verify_signature(signature, e, n):
# Verify the signature by decrypting it using the public key (m' = signature^e % n)
decrypted_message = mod_exp(signature, e, n)
return decrypted_message
# Taking input from the user
p = int(input("Enter prime number p: "))
q = int(input("Enter prime number q: "))
e = int(input("Enter public exponent e: "))
plaintext = int(input("Enter the plaintext (integer): "))

# Encrypting the plaintext


ciphertext, n, d = rsa_encrypt(p, q, e, plaintext)

# Calculate the RSA digital signature


signature = sign_message(plaintext, d, n)

# Verify the signature


decrypted_message = verify_signature(signature, e, n)

# Display the results


print(f"p = {p}")
print(f"q = {q}")
print(f"e = {e}")
print(f"Private exponent d = {d}")
print(f"Ciphertext = {ciphertext}")
print(f"Signature s = {signature}")
print(f"Decrypted message m' = {decrypted_message}")

# Check if the decrypted message matches the original plaintext


if decrypted_message == plaintext:
print("Signature is valid and the message matches.")
else:
print("Signature is invalid.")

OUTPUT :

You might also like