RSA Cipher Decryption



This chapter is a continuation of the previous chapter where we followed step wise implementation of encryption using RSA algorithm and discusses in detail about it.

The function used to decrypt cipher text is as follows

def decrypt_message(encrypted_msg, privatekey):
   return privatekey.decrypt(encrypted_msg, padding.OAEP(
        mgf=padding.MGF1(algorithm=hashes.SHA256()),
        algorithm=hashes.SHA256(),
        label=None
    ))

For public key cryptography or asymmetric key cryptography, it is important to maintain two important features namely Authentication and Authorization.

Authorization

Authorization is the process to confirm that the sender is the only one who have transmitted the message. The following code explains this −

def sign(message, private_key):
   return private_key.sign(
    message,
    padding.PSS(
        mgf=padding.MGF1(hashes.SHA256()),
        salt_length=padding.PSS.MAX_LENGTH
    ),
    hashes.SHA256()
)

Authentication

Authentication is possible by verification method which is explained as below −

def verify(message, signature, public_key):
   return public_key.verify(
        signature,
        message,
        padding.PSS(
            mgf=padding.MGF1(hashes.SHA256()),
            salt_length=padding.PSS.MAX_LENGTH
        ),
        hashes.SHA256()
    )

The digital signature is verified along with the details of sender and recipient. This adds more weight age for security purposes.

Example - RSA Cipher Decryption

You can use the following code for RSA cipher decryption −

main.py

from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives import hashes

def generate_keys():
   privatekey = rsa.generate_private_key(public_exponent=65537, key_size=2048)
   publickey = privatekey.public_key()
   return privatekey, publickey

def encrypt_message(a_message , publickey):
   return publickey.encrypt(a_message,
      padding.OAEP(
         mgf=padding.MGF1(algorithm=hashes.SHA256()),
         algorithm=hashes.SHA256(),
         label=None
      ))

def decrypt_message(encrypted_msg, privatekey):
   return privatekey.decrypt(encrypted_msg, padding.OAEP(
        mgf=padding.MGF1(algorithm=hashes.SHA256()),
        algorithm=hashes.SHA256(),
        label=None
    ))

def sign(message, private_key):
   return private_key.sign(
    message,
    padding.PSS(
        mgf=padding.MGF1(hashes.SHA256()),
        salt_length=padding.PSS.MAX_LENGTH
    ),
    hashes.SHA256()
)

def verify(message, signature, public_key):
   return public_key.verify(
        signature,
        message,
        padding.PSS(
            mgf=padding.MGF1(hashes.SHA256()),
            salt_length=padding.PSS.MAX_LENGTH
        ),
        hashes.SHA256()
    )

message = b"Sample Message to be signed"
privatekey , publickey = generate_keys()

signature = sign(message, privatekey)

try:
   verify(message, signature, publickey)
   print("Signature validated")
except:
   print("Invalid Signature")   

Output

Run the code and verify the output −

Signature validated
Advertisements