python - AES 256 加密 -> 更改初始化向量会在不知道 key 的情况下稍微更改解密的消息

标签 python encryption aes pycrypto initialization-vector

我通过 AES 加密/解密示例进行了以下观察,这对我来说非常违反直觉。

我尝试在 CBC 模式下使用 AES 加密和解密简单的有效负载。 我的理解是,根据这个答案,初始化向量不必是 secret 的:https://security.stackexchange.com/a/17046 。 在我看到的大多数示例中,初始化向量是加密有效负载的非随机部分。

但是通过更改初始化向量,我能够在加密期间更改消息。

例如,请参阅我从 https://stackoverflow.com/a/21928790/669561 复制并改编的这个 python 示例。 我为加密设置了硬编码的iv,并稍微调整了iv作为解密。 通过此更改,我可以将消息从 "hello world" 更改为 "hello!world"

import base64
import hashlib

from Crypto.Cipher import AES


class AESCipher(object):

    def __init__(self, key):
        self.bs = AES.block_size
        self.key = hashlib.sha256(key.encode()).digest()

    def encrypt(self, raw):
        raw = self._pad(raw)
        #iv = Random.new().read(AES.block_size)
        #                    | here is the difference to the iv from decrypt
        iv = b'\xe2\xe0l3H\xc42*N\xb0\x152\x98\x9cBh'
        cipher = AES.new(self.key, AES.MODE_CBC, iv)
        code = cipher.encrypt((raw.encode()))
        return base64.b64encode(iv + code)

    def decrypt(self, enc):
        enc = base64.b64decode(enc)
        #iv = enc[:AES.block_size]
        #                    | here is the difference to the iv from encrypt
        iv = b'\xe2\xe0l3H\xc52*N\xb0\x152\x98\x9cBh'
        cipher = AES.new(self.key, AES.MODE_CBC, iv)
        return self._unpad(cipher.decrypt(enc[AES.block_size:])).decode('utf-8')

    def _pad(self, s):
        return s + (self.bs - len(s) % self.bs) * chr(self.bs - len(s) % self.bs)

    @staticmethod
    def _unpad(s):
        return s[:-ord(s[len(s) - 1:])]


if __name__ == '__main__':
    text = "hello world"
    print(text) # -> "hello world"
    aes = AESCipher("F56hnXWaUWMh6ThQZ5l3mBg9zHFx6vQg")
    payload = aes.encrypt(text)
    print(aes.decrypt(payload)) # -> "hello!world"

这个简单示例的结果对我来说完全违反直觉。 似乎中间有人可以获取有效负载,稍微更改iv,从而在不知道 key 的情况下更改解密的消息!

根据我的理解,仅通过更改初始化向量来更改加密消息的内容应该不是那么容易的。更改初始化向量应该会导致完全不同的结果!

我的想法有问题吗?

你能帮我澄清一下我的误解吗?

最佳答案

AES 和一般的分组密码通常只提供“保密性”——它们不保证完整性。

您的观察是正确的 - 更改 IV 确实会更改解密后生成的明文。您还会注意到,在我的情况下,更改密文本身的字节仍然可以在 AES-CBC 下成功解密(尽管是不同的明文)。

您想要的是一种方法来验证自初始加密操作发生以来 IV 和密文未被修改。

实现此目的的两种最常见方法是:

  • MAC(HMAC 很常见)
  • 首选经过身份验证的加密模式,例如 GCM。

您可能会发现 this example of AES-GCM encryption in Python有用。我已将其包含在下面:

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

关于python - AES 256 加密 -> 更改初始化向量会在不知道 key 的情况下稍微更改解密的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59060164/

相关文章:

python - O(N) 中的最长递增子序列代码?

encryption - 从 PFX (PKCS #12) 文件创建 RSA 私钥

java - 提高java中AES的性能

python - Pytest:捕获的 stderr 设置和捕获的日志设置重复

python - 如何在调用 is_valid() 方法后更改表单字段值?

JAVA PHP加密解密

java - 从哪里可以获得 AES key 计划加密 key ?

java - 如何在 java 服务器端解密 cryptojs AES 加密消息?

python - Gcloud 更新破坏了我的应用程序——GCP Python 2.7

encryption - 我的 PyCrypto 实现对于我的目的来说安全吗?