Symmetric and Asymmetric Cryptography



In this chapter, let us discuss in detail about symmetric and asymmetric cryptography.

Symmetric Cryptography

In this type, the encryption and decryption process uses the same key. It is also called as secret key cryptography. The main features of symmetric cryptography are as follows −

  • It is simpler and faster.
  • The two parties exchange the key in a secure way.

Drawback

The major drawback of symmetric cryptography is that if the key is leaked to the intruder, the message can be easily changed and this is considered as a risk factor.

Data Encryption Standard (DES)

The most popular symmetric key algorithm is Data Encryption Standard (DES) and Python includes a package which includes the logic behind DES algorithm.

Installation

The command for installation of DES package pyDES in Python is −

(myenv) D:\Projects\python\myenv>pip3 install pyDES
Collecting pyDES
  Downloading pyDes-2.0.1.tar.gz (9.9 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: pyDES
  Building wheel for pyDES (pyproject.toml) ... done
  Created wheel for pyDES: filename=pydes-2.0.1-py2.py3-none-any.whl size=9737 sha256=cc4ac26565690404f31aa3aef42334eac97679e21fe8fb333a5e75089c4bf40e
  Stored in directory: c:\users\mahes\appdata\local\pip\cache\wheels\fa\d5\cb\f9970a46d7c9ddeb958e8dd9483e5c368923fccf23ceb0c716
Successfully built pyDES
Installing collected packages: pyDES
Successfully installed pyDES-2.0.1

Example - Usage of DES Algorithm

Simple program implementation of DES algorithm is as follows −

import pyDes

data = "DES Algorithm Implementation"
k = pyDes.des("DESCRYPT", pyDes.CBC, "\0\0\0\0\0\0\0\0", pad=None, padmode=pyDes.PAD_PKCS5)
d = k.encrypt(data)

print("Encrypted: %r" % d)
print("Decrypted: %r" % k.decrypt(d))
assert(k.decrypt(d) == data)

It calls for the variable padmode which fetches all the packages as per DES algorithm implementation and follows encryption and decryption in a specified manner.

Output

You can see the following output as a result of the code given above −

Encrypted: b'\xd6V\xf2\xffW\x16\xda\xa8r\x12\x9bi\xce\xect\x93\xef\t4\xf4!\xc2\x91\x8dA\xf3\x0b\x10\xfc\x97\xcf\xb2'
Decrypted: b'DES Algorithm Implementation'

Asymmetric Cryptography

It is also called as public key cryptography. It works in the reverse way of symmetric cryptography. This implies that it requires two keys: one for encryption and other for decryption. The public key is used for encrypting and the private key is used for decrypting.

Drawback

  • Due to its key length, it contributes lower encryption speed.
  • Key management is crucial.

Example - Usage of Asymmetric Cryptography using RSA Algorithm

The following program code in Python illustrates the working of asymmetric cryptography using RSA algorithm and its implementation −

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
    ))

a_message = b"This is the illustration of RSA algorithm of asymmetric cryptography"
privatekey , publickey = generate_keys()
encrypted_msg = encrypt_message(a_message , publickey)
decrypted_msg = decrypt_message(encrypted_msg, privatekey)

print(" Original content: %s - (%d)" % (a_message, len(a_message)))
print("Encrypted message: %s - (%d)" % (encrypted_msg, len(encrypted_msg)))
print("Decrypted message: %s - (%d)" % (decrypted_msg, len(decrypted_msg)))

Output

You can find the following output when you execute the code given above −

Original content: b'This is the illustration of RSA algorithm of asymmetric cryptography' - (68)
Encrypted message: b"o\\\x851\x18\x01?S\xf0/\xe6\xb0s\xf9(BMA\x7f#Fw\xa6\x16s\xf6\x83\x00I\xc5$\xd0H@\x92\xdb~\xbc:\xe3\x914\x0c\x1b\x9c;\x98\xde|\xe5\xea\xb9Yj\xc0y\x9a5^\xe8r\xe9e\x94r\xf4\xe7\xf8\x1bP\xc7\x8e\xf6\x9e&S\xa1\xbb\x0f\xd4ArW\xfalh4^`\x96\xaa\xfe\\X\xbbO \xd8_^\xb8\xdd\x8c+\xcf\xd5\xd5viE\x85u\xeeG4\x07oD\x86\x0e5)\xc5\x1b}\x06:W^\x00\xb8\xd0\x88j\xc2\xb6D\x981\x06\xb7\xb6\x9e\xebj\xe5uE\xdb\xd8~\x03\x0c\xb0<\xbc7@\xcbq\xb8\xeac\xa8v\xd5D\xf2\xff)\xfea\x12\xff\xbc\xf4\xe8\xfa\xa3\x82Z\x13\x18\x8d'&\xdf\x17\xd1\xf1\x88\xd9C\xe8g\xf0\xee$\xc8\xe6\xe4\xa8d\\\xdbO\xcb\\\x1ek\x90d\x91b\x08f[IK\x9c7t\xb2\t\x19\x8b\xe6\xeb\xdb=\xba\x18[\xbf\xcf\x99\x16\xd7\xeb\x8d\xed\x97\xa0\x99\x866c\x08Qfy\xbb\xe2\xc9\x15\xa7" - (256)
Decrypted message: b'This is the illustration of RSA algorithm of asymmetric cryptography' - (68)
Advertisements