Send encrypted text on email using Python

Hemant Nikam
3 min readSep 1, 2024

--

Following is the python code to send AES-128 encrypted text on email using Python:

  • We have used AES-128 bit encryption with ECB (Electronic Code Book) mode which is the simplest mode for AES encryption.
  • We are using PyCryptodome library for AES encryption which can be installed using following command. Refer this link for more information:
C:\smime> pip install pycryptodome
  • We are using outlook email id to send the email in following code but any other email provider can be used with corresponding smtp configuration.
  • The receiver email id can be with any service provider.

Important: Replace the sender/receiver email id’s and sender’s email password before executing the code.

# required libraries for sending email
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

# required libraries for encrypting the message
import base64
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad,unpad

#AES ECB mode is used for encryption
def encrypt(raw, key):
raw = pad(raw.encode('utf-8', 'ignore'), 16)
cipher = AES.new(key.encode('utf-8'), AES.MODE_ECB)
return base64.b64encode(cipher.encrypt(raw))

def decrypt(enc, key):
enc = base64.b64decode(enc)
cipher = AES.new(key.encode('utf-8'), AES.MODE_ECB)
return unpad(cipher.decrypt(enc),16)

def send_email(sender_email, sender_password, receiver_email, text_message):
# Create the email message with details
msg = MIMEMultipart("text")
msg.attach(MIMEText(text_message, "plain", _charset = "UTF-8"))
msg['Subject'] = "send encrypted message on email"
msg['From'] = sender_email
msg['To'] = receiver_email
msg.add_header('Content-Type','text/plain')

#SMTP session for outlook
# we have used outlook as it is easier to setup/authenticate,
# any other provider can be used with corresponding smtp configurations
s = smtplib.SMTP('smtp-mail.outlook.com', 587)
#Start TLS for security
s.starttls()
#Your Outlook authentication - enter your email id and password
s.login(sender_email, sender_password)
#Send the mail - enter your email id and the receiver's email id
s.send_message(msg, sender_email, receiver_email)
#Terminate
s.quit()

print("Email sent successfully!")


try:
# message text to be sent over email
messagetext = "This is a demo for sending encrypted data on email."
#Secret key for encryption - must be 16 characters for AES128 (128 bits = 16 bytes)
key = '0123456701234567'

# encrypt the message
encrypted_msg = encrypt(messagetext, key)
print('Encrypted message:',encrypted_msg.decode("utf-8", "ignore"))

# set email parameters
sender_email = "[your outlook email id@outlook.com]"
sender_password = "[your email password]"
receiver_email = "[receiver's email id@gmail.com]"

# send email with encrypted data
send_email(sender_email, sender_password, receiver_email, encrypted_msg)

# decrypt the data
decrypted_msg = decrypt(encrypted_msg, key)
print('Decrypted message: ', decrypted_msg.decode("utf-8", "ignore"))
except (ValueError, KeyError):
print("Error Occured")

After executing the code following output will be displayed:

Open the email in the receiver’s mailbox, it should look similar to screenshot below.

This message can be copied and decrypted using decrypt function in above code.

Here are few resource links to further explore AES:

  1. https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.197-upd1.pdf
  2. https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38a.pdf
  3. https://ciit.finki.ukim.mk/data/papers/10CiiT/10CiiT-46.pdf

P.S. Please post your questions in comments section and I will reply.

--

--