AES is a symmetric cipher used for encryption and decryption of data. It operates on a 4x4 grid of 128 bits (16-byte blocks) and supports key lengths of 128, 192, or 256 bits.
plaintext
|
|
+
(XOR)@ <-- Initialisation vector (IV)
|
|
+
block cipher <-- Key
|
|
+
cipher text
- Initial XOR with Key: Plaintext is combined with the initial round key.
- Substitute Bytes: Byte substitution using an AES lookup table.
- Shift Rows: Rows of the grid are shifted cyclically to the left.
- Mix Columns: Matrix multiplication in a Galois finite field (except in the last round).
- Add Round Key: Round key is XORed with the result of the previous steps.
cipher text
|
|
+
(XOR)@ <-- Initialisation vector (IV)
|
|
+
block cipher <-- Key
|
|
+
plaintext
Decryption involves reversing the encryption steps, with the IV used to maintain randomness and uniqueness of ciphertexts.
- AES encryption involves multiple rounds, each performing a specific transformation on the data.
- CBC mode enhances security by XORing each plaintext block with the previous ciphertext block before encryption.
- The IV is crucial for CBC mode to ensure unique ciphertexts.
#ENCRYPTOR WHICH WRITES THE ENCRYPTION AND THE VI TO A FILE
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
from Crypto.Util.Padding import unpad
key = b'mysecretpassword' #16 bit key
cipher = AES.new(key,AES.MODE_CBC)
plaintext = b'this is some important data'
ciphertext = cipher.encrypt(pad(plaintext,AES.block_size))
with open('k_file','wb')as key_file:
key_file.write(cipher.iv)
key_file.write(ciphertext)
#DECRYPTOR WHICH TAKES FROM THAT FILE TO CONVERT BACK TO PLAIN TEXT
from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad
import os
import base64
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
from Crypto.Util.Padding import unpad
from Crypto import Random
from Crypto.Hash import SHA256
import base64
import sys
key = b'mysecretpassword' #16 bit key
with open('k_file', 'rb') as key_file:
iv = key_file.read(16)
ciphertext = key_file.read()
cipher= AES.new(key,AES.MODE_CBC, iv)
plaintext = unpad(cipher.decrypt(ciphertext), AES.block_size)
print(plaintext.decode())