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

PR7 AES Java Final

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)
29 views3 pages

PR7 AES Java Final

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
You are on page 1/ 3

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 key.AES is the most common security
algorithm used worldwide for various purposes like wireless communication, financial transactions,
encrypted data storage, etc.The 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
encryption.AES 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 Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
from Crypto.Protocol.KDF import PBKDF2
from Crypto.Util.Padding 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 = AES.new(key, AES.MODE_CBC, iv)
ciphertext = cipher.encrypt(pad(plaintext.encode(), AES.block_size))
return base64.b64encode(ciphertext).decode()

def aes_decrypt(key, iv, ciphertext):


cipher = AES.new(key, AES.MODE_CBC, iv)
ciphertext = base64.b64decode(ciphertext)
decrypted_text = unpad(cipher.decrypt(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(password.encode(), 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

You might also like