DEV Community

PrashikNikumbe
PrashikNikumbe

Posted on

Cryptography using python.

What is cryptography ?

It is the process of securing the message/information for communication between sender and receiver.

Common terms:

  • Plain text : It is a message/information in the readable form.
  • Cipher text : It is formed by applying cryptographic algorithms on plain text. It is not in a readable form and cannot be easily understood.
  • Key : It is a variable value used in encryption and decryption process.

Types of cryptography

  • Symmetric key cryptography: same key is used for encryption and decryption.
  • Asymmetric key cryptography: different key is used for encryption and decryption. pair of keys is used known as public and private key.

What is hashing ?

It is a cryptography technique which converts the plain text into unique hash value. Commonly the hash string is of fixed length independent of length of plain text. hash value cannot be reversed engineered in to plain text.

Knowing our python libraries

  • cryptography: It includes both high level recipes and low-level interfaces to common cryptographic algorithms such as symmetric ciphers, message digests, and key derivation functions. We will be using it for demonstration of symmetric key cryptography.

  • rsa: It include pure implementation of rsa algorithm. We will be using it for demonstration of asymmetric key cryptography.

  • haslib: It provides interface for hashing messages easily. It contains numerous methods which will handle hashing any raw message in an encrypted format. We will be using it for demonstration of hashing.

Installing libraries....

pip install cryptography
Enter fullscreen mode Exit fullscreen mode
pip install rsa
Enter fullscreen mode Exit fullscreen mode

In case of any error try to run

python -m pip install --upgrade pip
Enter fullscreen mode Exit fullscreen mode

Symmetric key cryptography

Fernet : It is a module inside cryptography package. It guarantees that a message encrypted using it cannot be manipulated or read without the key.

Code

from cryptography.fernet import Fernet

plain_text = "Hello World"


# generating random key 
key = Fernet.generate_key()

# creating object of Fernet class
fernet = Fernet(key)

# encryption takes place using encrypt method
cipher = fernet.encrypt(plain_text.encode())

print("original string: ", plain_text)
print("encrypted string: ", cipher)

# decryption takes place using decrypt method
newMessage = fernet.decrypt(cipher).decode()

print("decrypted string: ", newMessage)

Enter fullscreen mode Exit fullscreen mode

Output

original string:  Hello World
encrypted string:  b'gAAAAABhVfD58m85tEJe3U4AQRbhIXFULXdfFGZnzS7IHS6aH8VGC4il3HSTF2tMjQ7_WJJdUAcAHuNV27ravfvOFOPv1hsQYg=='
decrypted string:  Hello World
Enter fullscreen mode Exit fullscreen mode

Asymmetric key cryptography

RSA algorithm is asymmetric cryptography algorithm. It uses different key for encryption and decryption i.e public and private.

Code

import rsa

#newkeys method will generate both the keys
public_key, private_key = rsa.newkeys(512)

plain_text = "Hello World"

#encryption takes place using encrypt method using public key
#normal string should be encoded into byte string
#using encode method
cipher = rsa.encrypt(plain_text.encode(),public_key)

print("original string: ", plain_text)
print("encrypted string: ", cipher)

#decryption takes place using decrypt method using private key
#after that string is converted from byte stream to string
#using decode method
newMessage = rsa.decrypt(cipher, private_key).decode()
print("decrypted string: ", newMessage)

Enter fullscreen mode Exit fullscreen mode

Output

original string:  Hello World
encrypted string:  b')e`\x00\xd7\xdb\xce\xae)\x93 \x06\x8b\x9a\x08\x90\xca`\xbd\x0e\xcc>72$\x08_\x0b\x9a6\x93\xf8\xc4\x1f\x8cv\xf7\xd1\x8e\x84\xb4\xd0\xb1\nPj\xee\xc5\x14\x88B\xd4{\x89[%\xab}s\xdcY\x05\x93\xba'
decrypted string:  Hello World
Enter fullscreen mode Exit fullscreen mode

Hashing

Message Digest Algorithm 5 (MD5) is a cryptographic hash algorithm that can be used to create a 128-bit string value from an arbitrary length string.

Secure Hash Algorithm(SHA), used for hashing data and certificate files. It is more secure than MD5. It has 6 types namely SHA-0, SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512.

Code

import hashlib

plain_text = "Hello World"

#MD5 Hashing takes place
md5Hash  = hashlib.md5(plain_text.encode())
#Hexidecimal string is generated
hexValue1 = md5Hash.hexdigest()

print("MD5 Hash Value:",hexValue1)

#SHA Hashing takes place
shaHash = hashlib.sha1(plain_text.encode())
#Hexidecimal string is generated
hexValue2 = shaHash.hexdigest()

print("SHA Hash Value:",hexValue2)

Enter fullscreen mode Exit fullscreen mode

Output

MD5 Hash Value: b10a8db164e0754105b7a99be72e3fe5
SHA Hash Value: 0a4d55a8d778e5022fab701977c5d840bbc486d0
Enter fullscreen mode Exit fullscreen mode

Top comments (0)