简体   繁体   English

C# 中的 Aes 加密 使用其他 IV 解密

[英]Aes Encryption in C# Decrypt using other IV

They say you can decrypt the data by using the same secret key but another Initialization Vector, How to achieve that one?他们说您可以使用相同的密钥但另一个初始化向量来解密数据,如何实现呢? Example: My IV for encryption is as follows byte[] bytes = {12,12,5,12,32,62,91,16,89,92,17,72,45,52,13,42};示例:我的加密IV如下 byte[] bytes = {12,12,5,12,32,62,91,16,89,92,17,72,45,52,13,42}; but for my Decryption is random IV that is 16bytes in length.但对于我的解密是长度为 16 字节的随机 IV。

You need to call SymmetricAlgorithm.CreateDecryptor() with the same IV that was used for encrypting.您需要使用用于加密的相同 IV 调用SymmetricAlgorithm.CreateDecryptor()

Quoting from the docs :引用文档

The SymmetricAlgorithm.CreateDecryptor method from the Aes instance is passed the IV value and the same key that was used for encryption.来自 Aes 实例的 SymmetricAlgorithm.CreateDecryptor 方法被传递 IV 值和用于加密的相同密钥。

Code sample, also from docs page:代码示例,也来自文档页面:

Aes aes = Aes.Create();
CryptoStream cryptStream = new CryptoStream(
    fileStream,
    aes.CreateDecryptor(key, iv),
    CryptoStreamMode.Read
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM