SymmetricAlgorithm Key 和初始化向量 (IV) 实现

SymmetricAlgorithm Key and initialization vector (IV) implementation

我有一个网站,它获取 userId 并使用 RijndaelManaged 提供商对其进行加密。

它把这些数据加密后,放入一个队列中。据我了解,每个数据都应该用不同的 IV 值加密,这可以是 public。

To decrypt data that was encrypted using one of the SymmetricAlgorithm classes, you must set the Key property and the IV property to the same values that were used for encryption. For a symmetric algorithm to be useful, the secret key must be known only to the sender and the receiver.

此外还有一个 Windows 服务从该队列中读取并尝试解密数据。但是由于队列中的每个值都使用不同的 IV 加密,Windows 服务如何知道使用哪个 IV?

我应该将它与加密数据一起存储吗?

MRibePnbXiN578TUAZcITw== - MyIVValueIsHere

或者是否需要采取已知的方法?

是的,如果您使用 AES-CBC,这正是您应该做的。您应该为每条消息随机生成 IV(使用加密安全的随机生成器,例如 RNGCryptoServiceProvider)。然后将 IV 与加密消息一起发送。

考虑使用 AESCryptoServiceProvider,因为 AES 是一个标准。 AES 与 Rijndael 相同,但具有一些固定的标准化参数。

通常的做法是在加密数据前加上 IV,以便解密。