0% found this document useful (0 votes)
16 views3 pages

AES Encryption Program in Python

The document discusses AES encryption and decryption. It describes AES, its advantages and disadvantages, and its modes of operation. It then provides a Python program to implement AES encryption and decryption using the CBC mode.

Uploaded by

prafulla
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

AES Encryption Program in Python

The document discusses AES encryption and decryption. It describes AES, its advantages and disadvantages, and its modes of operation. It then provides a Python program to implement AES encryption and decryption using the CBC mode.

Uploaded by

prafulla
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Sipna College of Engineering& Technology, Amravati.

Department of Electronics and Telecommunication Engineering

Department: Electronics & Telecommunication Class: IV year


Subject: Cryptography and Network Security Sem: VII

PRACTICAL NO.7

Aim: Write a program for AES encryption

Software Required: Python 3.7.1

Theory:
AES is an Advanced Encryption Standard algorithm. It is a type of symmetric, block cipher encryption and
decryption algorithm. It works with key size 128, 192, and 256 bits. It uses a valid and similar secret key for
both encryption and decryption.
In AES, the block cipher is used. It means that the data to be encrypted is converted into blocks for
encryption. The original data value is encrypted using different bits of padding such as 128, 192, or 256 bits.
Advantages of AES
The encrypted data cannot be decrypted without a valid secret [Link] is the most common security
algorithm used worldwide for various purposes like wireless communication, financial transactions,
encrypted data storage, [Link] companies who want to transfer their data safely and without breaking it can
always use the AES algorithm.
Disadvantages of AES
AES algorithm uses very simple algebraic formulae. Each block is encrypted using a similar kind of
[Link] can be difficult to implement with the software.

AES Encryption and Decryption

Modes of Operation of AES Algorithm


There are the following six modes of operation in the AES algorithm:
1. ECB (Electronic Code Book):
E&TC/SEM-VII/C&NS/PR07 Page 1
Sipna College of Engineering& Technology, Amravati.
Department of Electronics and Telecommunication Engineering
It is the simplest mode among all. It divides the plaintext message into blocks of size 128 bits. Then these
blocks are encrypted using the same key and algorithm. Hence, it generates the same cipher text for the same
block every time. It is considered a weakness and therefore it is suggested not to use ECB for encryption.
2. CBC (Cipher Block Chaining):
CBC uses an Initialization Vector (IV) to improve the encryption. In CBC, the encryption is performed by
XOR operation between the plaintext and IV. Then the cipher text is generated. It then uses the encryption
result to XOR with the plain text until the last block.
3. CFB (Cipher FeedBack):
CFB can be used as a stream cipher. It encrypts the initialization vector (IV) first and then XOR with the
plaintext to generate the cipher text. Then it encrypts the cipher text with the next plaintext block. In this
mode, decryption can be performed in a parallel manner but encryption cannot be performed in a parallel
manner.
4. OFB (Output FeedBack):
OFB can also be used as a stream cipher. It does not need padding data. First, the IV is encrypted and then the
encryption result is XOR with the plaintext to generate the cipher text. Here, the IV cannot be encrypted or
decrypted in a parallel manner.
5. CTR (Counter):
In CTR mode the encryption process is similar to OFB mode, the only difference is that it encrypts the
counter value instead of IV. It has two advantages, encryption or decryption can be performed in a parallel
manner and the noise of one block does not affect another block.
6. GCM (Galois/Counter Mode):
GCM mode is an extended version of CTR mode. It was introduced by NIST. The GCM mode provides the
cipher text as well as authentication tag after the encryption process
..

E&TC/SEM-VII/C&NS/PR07 Page 2
Sipna College of Engineering& Technology, Amravati.
Department of Electronics and Telecommunication Engineering
Program:
#pip install pycryptodome
from [Link] import AES
from [Link] import get_random_bytes
from [Link] import PBKDF2
from [Link] import pad, unpad
import base64

def generate_aes_key_and_iv(password, salt):


key = PBKDF2(password, salt, dkLen=32) # 256-bit key
iv = get_random_bytes(16) # 128-bit IV
return key, iv

def aes_encrypt(key, iv, plaintext):


cipher = [Link](key, AES.MODE_CBC, iv)
ciphertext = [Link](pad([Link](), AES.block_size))
return base64.b64encode(ciphertext).decode()

def aes_decrypt(key, iv, ciphertext):


cipher = [Link](key, AES.MODE_CBC, iv)
ciphertext = base64.b64decode(ciphertext)
decrypted_text = unpad([Link](ciphertext), AES.block_size)
return decrypted_text.decode()

# Example usage:
password = "SecretPassword"
salt = get_random_bytes(16) # 128-bit salt

plaintext = "Hello, AES Encryption!"

# Generate AES key and IV


key, iv = generate_aes_key_and_iv([Link](), salt)

# Encryption
encrypted_text = aes_encrypt(key, iv, plaintext)
print("Encrypted:", encrypted_text)

# Decryption
decrypted_text = aes_decrypt(key, iv, encrypted_text)
print("Decrypted:", decrypted_text)

Result:
Original value: AES Encryption

Encrypted: +UhNpr78x1mMAioctezLNV7IYzu1hMfhZBG6Mrei6iI=

Decrypted: Hello, AES Encryption!

Conclusion: Thus, we have studied the AES encryption

E&TC/SEM-VII/C&NS/PR07 Page 3

Common questions

Powered by AI

The key and Initialization Vector (IV) generation process using PBKDF2 involves using a password and salt to derive a 256-bit key. A random 128-bit IV is generated using a secure random generator. This key and IV are then used for AES encryption .

AES ensures data confidentiality by encrypting data using a symmetric key, where the same key is used for both encryption and decryption, making it difficult to decrypt without the key. However, its implementation can be complex due to the simplicity of its algebraic operations, which might not be straightforward in all software environments .

In CFB and OFB modes, the Initialization Vector (IV) is used to initiate the encryption process. In CFB, the IV is encrypted and then XORed with plaintext to create ciphertext, while in OFB, the IV is also encrypted with each iteration but does not need padding, as it directly contributes to the ciphertext by being XORed with the plaintext .

CTR mode enables parallel processing by using a unique counter value for each encryption block instead of a continuous chain. This allows each block to be processed independently, facilitating parallel encryption and decryption .

In CFB mode, decryption can be performed in parallel because each block of ciphertext is independently XORed with the encrypted IV or previous ciphertext, allowing simultaneous decryption. In contrast, encryption requires each block to be XORed with the previous ciphertext, enforcing a sequential dependency .

ECB mode is not recommended because it encrypts identical plaintext blocks into identical ciphertext blocks, leading to security vulnerabilities where patterns in data can be discerned from the ciphertext .

CBC mode enhances security by using an Initialization Vector (IV) for the encryption process, ensuring that identical plaintext blocks encrypt to different ciphertexts by XORing each plaintext block with the previous ciphertext block before encrypting .

GCM mode is more secure than CTR in terms of data integrity because it includes an authentication tag that verifies the integrity and authenticity of the data. This prevents unauthorized alterations of the plaintext and assures that the decrypted text is exactly what was originally encrypted .

The advantages of AES encryption include its widespread use for secure data transmission, such as in wireless communications and financial transactions, and the difficulty in decrypting data without a valid secret key . However, AES also has disadvantages, including the simplicity of its algebraic formulae and potential difficulty in software implementation .

GCM mode extends CTR mode by providing an additional authentication tag along with the ciphertext. This tag offers integrity and authenticity assurances for the encrypted data, ensuring that any alterations in the data can be detected .

You might also like