Python 3 Pycryptodome Example Script to Encrypt or Encode Input Raw Text Using AES Encryption Cryptography Library Full Project For Beginners

You are currently viewing Python 3 Pycryptodome Example Script to Encrypt or Encode Input Raw Text Using AES Encryption Cryptography Library Full Project For Beginners

 

Welcome folks today in this post we will be encrypting raw text in python using pycryptodome library. All the full source code of the application is shown below.

 

Get Started

 

In order to get started you need to install the following library using the pip command as shown below

 

pip install pycryptodome

 

After installing this library you can make an app.py file and copy paste the following code

 

app.py

 

 

from Crypto.Cipher import AES
from Crypto import Random
from binascii import b2a_hex
import sys

# get the plaintext
plain_text = sys.argv[1]

# The key length must be 16 (AES-128), 24 (AES-192), or 32 (AES-256) Bytes.
key = b'this is a 16 key'

# Generate a non-repeatable key vector with a length
# equal to the size of the AES block
iv = Random.new().read(AES.block_size)

# Use key and iv to initialize AES object, use MODE_CFB mode
mycipher = AES.new(key, AES.MODE_CFB, iv)

# Add iv (key vector) to the beginning of the encrypted ciphertext
# and transmit it together
ciphertext = iv + mycipher.encrypt(plain_text.encode())


# To decrypt, use key and iv to generate a new AES object
mydecrypt = AES.new(key, AES.MODE_CFB, ciphertext[:16])

# Use the newly generated AES object to decrypt the encrypted ciphertext
decrypttext = mydecrypt.decrypt(ciphertext[16:])

# output
file_out = open("encrypted.bin", "wb")
file_out.write(ciphertext[16:])
file_out.close()

print("The key k is: ", key)
print("iv is: ", b2a_hex(ciphertext)[:16])
print("The encrypted data is: ", b2a_hex(ciphertext)[16:])
print("The decrypted data is: ", decrypttext.decode())

 

 

And now if you execute the python script app.py as shown below using the command

 

python app.py this is the text

 

Here we are passing the raw text that needs to be encrypt by using this script and when it finishes it will look like something this

 

 

 

It will also create a special .bin file which will contain the encrypted text

 

 

Leave a Reply