How to encrypt and decrypt text using AES in python?

Description

To see how to do simple encryption and decryption of the given text message using AES.

   Get the input message text.

   In AES the same key used for both encryption and decryption.

   Generate the secret key.

   Define the mode of AES.

   Encrypt the message using cipher.encrypt().

   Decrypt the message using cipher.decrypt().

#import crypto and base64

from Crypto.Cipher import AES

import base64

message =

‘FirstSoft Technologies Pvt Ltd’.rjust(32)

print(“Actual message is\n”,message)

secret_key = ‘1234589648789658’

#define AES mode

cipher = AES.new

(secret_key,AES.MODE_ECB)

#encode the message text

encryption = base64.b64encode

(cipher.encrypt(message))

print(“\n”)

#print the encoded message

print(“The encrypted message is\n”,encryption)

#decode the message

decryption = cipher.decrypt

(base64.b64decode(encryption))

print(“\n”)

#print the decode

print (“Original message after decryption

is\n”,decryption)

Leave Comment

Your email address will not be published. Required fields are marked *

clear formSubmit