Skip to Content
oxcrypto

CBC

CBC mode for any block cipher selector.

from oxcrypto.cipher.cbc import CBC
from oxcrypto.cipher import Padding
from oxcrypto.cipher.aes import AES

mode = CBC.CBC
key = AES.AES128.generate_key()
iv = bytes(16)  # use a fresh, unpredictable IV for every message
ciphertext = mode.encrypt(key, b"hello", cipher=AES.AES128, iv=iv, padding=Padding.PKCS7)
assert mode.decrypt(key, ciphertext, cipher=AES.AES128, iv=iv, padding=Padding.PKCS7) == b"hello"

encryptor = mode.encryptor(key, cipher=AES.AES128, iv=iv, padding=Padding.PKCS7)
assert encryptor.update(b"hel") + encryptor.update(b"lo") + encryptor.finalize() == ciphertext

Standard: NIST SP 800-38A.

Padded modes take padding=: Padding.PKCS7 (default), Padding.ISO10126, Padding.ISO7816, Padding.ANSI_X923 or Padding.NONE. With Padding.NONE the data must be a whole number of blocks. Padding.ISO10126 writes PKCS #7 bytes, which ISO 10126 allows, and accepts any filler on decryption. A wrong key or tampered data raises ValueError for invalid padding; these modes provide no integrity, so prefer an AEAD.

encryptor and decryptor return a single-use CipherContext: call update any number of times, then finalize once. Further calls raise ValueError.