Python to Encrypt and Decrypt Text with Source Code

A small python program that encodes and decodes text.


  • pycryptodome 3.9.8
  • Python 3

Run the Script:

python "a text"
Code language: JavaScript (javascript)


python "hello world"
Code language: JavaScript (javascript)

An encrypted file (encrypted.bin) will be generated when the program has finished running.


  • pycryptodome==3.9.8

Source Code:

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 =

# Use key and iv to initialize AES object, use MODE_CFB mode
mycipher =, 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.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")

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


Leave a Comment