AES 和一般的分组密码通常只提供“保密性”——它们不保证完整性。
您的观察是正确的 - 更改 IV 确实会更改解密后生成的明文。您还会注意到,在我的情况下,更改密文本身的字节仍然可以在 AES-CBC 下成功解密(尽管是不同的明文)。
您想要的是一种方法来验证自初始加密操作发生以来 IV 和密文没有被修改。
实现这一目标的两种最常见的方法是:
- MAC(HMAC 很常见)
- 首选经过身份验证的加密模式,例如 GCM。
你可能会发现这是 Python 中 AES-GCM 加密的示例 https://github.com/luke-park/SecureCompatibleEncryptionExamples/blob/master/Python/SCEE.py有用。我已将其包含在下面:
from Crypto.Hash import SHA256, HMAC
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
from Crypto.Protocol.KDF import PBKDF2
import base64
ALGORITHM_NONCE_SIZE = 12
ALGORITHM_TAG_SIZE = 16
ALGORITHM_KEY_SIZE = 16
PBKDF2_SALT_SIZE = 16
PBKDF2_ITERATIONS = 32767
PBKDF2_LAMBDA = lambda x, y: HMAC.new(x, y, SHA256).digest()
def encryptString(plaintext, password):
# Generate a 128-bit salt using a CSPRNG.
salt = get_random_bytes(PBKDF2_SALT_SIZE)
# Derive a key using PBKDF2.
key = PBKDF2(password, salt, ALGORITHM_KEY_SIZE, PBKDF2_ITERATIONS, PBKDF2_LAMBDA)
# Encrypt and prepend salt.
ciphertextAndNonce = encrypt(plaintext.encode('utf-8'), key)
ciphertextAndNonceAndSalt = salt + ciphertextAndNonce
# Return as base64 string.
return base64.b64encode(ciphertextAndNonceAndSalt)
def decryptString(base64CiphertextAndNonceAndSalt, password):
# Decode the base64.
ciphertextAndNonceAndSalt = base64.b64decode(base64CiphertextAndNonceAndSalt)
# Get the salt and ciphertextAndNonce.
salt = ciphertextAndNonceAndSalt[:PBKDF2_SALT_SIZE]
ciphertextAndNonce = ciphertextAndNonceAndSalt[PBKDF2_SALT_SIZE:]
# Derive the key using PBKDF2.
key = PBKDF2(password, salt, ALGORITHM_KEY_SIZE, PBKDF2_ITERATIONS, PBKDF2_LAMBDA)
# Decrypt and return result.
plaintext = decrypt(ciphertextAndNonce, key)
return plaintext.decode('utf-8')
def encrypt(plaintext, key):
# Generate a 96-bit nonce using a CSPRNG.
nonce = get_random_bytes(ALGORITHM_NONCE_SIZE)
# Create the cipher.
cipher = AES.new(key, AES.MODE_GCM, nonce)
# Encrypt and prepend nonce.
ciphertext, tag = cipher.encrypt_and_digest(plaintext)
ciphertextAndNonce = nonce + ciphertext + tag
return ciphertextAndNonce
def decrypt(ciphertextAndNonce, key):
# Get the nonce, ciphertext and tag.
nonce = ciphertextAndNonce[:ALGORITHM_NONCE_SIZE]
ciphertext = ciphertextAndNonce[ALGORITHM_NONCE_SIZE:len(ciphertextAndNonce) - ALGORITHM_TAG_SIZE]
tag = ciphertextAndNonce[len(ciphertextAndNonce) - ALGORITHM_TAG_SIZE:]
# Create the cipher.
cipher = AES.new(key, AES.MODE_GCM, nonce)
# Decrypt and return result.
plaintext = cipher.decrypt_and_verify(ciphertext, tag)
return plaintext