简体   繁体   English

使用CryptoStream的“指定的初始化向量(IV)与该算法的块大小不匹配”

[英]“Specified initialization vector (IV) does not match the block size for this algorithm” using an CryptoStream

I've had troubles using an CryptoStream for my file encryption.. 使用CryptoStream进行文件加密时遇到了麻烦。

Code: 码:

public static void EncryptFile(string inputFile, string outputFile)
    {
        int num;
        string s = "PUPlr";
        byte[] bytes = new UnicodeEncoding().GetBytes(s);
        string path = outputFile;
        FileStream stream = new FileStream(path, FileMode.Create);
        RijndaelManaged managed = new RijndaelManaged();
        CryptoStream crpytStream = new CryptoStream(stream, managed.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write);
        FileStream stream2 = new FileStream(inputFile, FileMode.Open);
        while ((num = stream2.ReadByte()) != -1)
        {
            crpytStream.WriteByte((byte)num);
        }
        stream2.Close();
        crpytStream.Close();
        stream.Close();
    }

Trying "managed.BlockSize = 16;" 尝试“ managed.BlockSize = 16;” or "= 128;" 或“ = 128;” doesn't seem to work, so how could I fix my error? 似乎不起作用,那么如何解决错误?

Block cyphers like Rijndael require keys and IVs of length equal to the block size, typically 256 bits. 像Rijndael这样的块密码需要长度等于块大小(通常为256位)的密钥和IV。

In addition, the IV must be unique for each message, or your data will not be secure. 此外,IV对于每条消息必须唯一,否则您的数据将不安全。

The error is: 错误是:

managed.CreateEncryptor(bytes, bytes)

where the bytes needs to be either 128 bits (16 bytes), 192 bits (24 bytes) or 256 bits (32 bytes) for the first (Key) and second parameter. 其中第一个(关键字) 第二个参数的bytes必须为128位(16字节),192位(24字节)或256位(32字节)。

Notes: 笔记:

  • for AES interoperability you should use Rijndael with a BlockSize of 128 bits (16 bytes). 为了实现AES互操作性,您应该使用BlockSize为128位(16字节)的Rijndael

  • UnicodeEncoding will (often) give you weak password. UnicodeEncoding通常会为您提供弱密码。 Look at using PKCS#5 to create a strong key (and IV) from a password. 看一下使用PKCS#5从密码创建一个强键(和IV)。 For .NET look at RFC2898DeriveBytes ; 对于.NET,请参阅RFC2898DeriveBytes

  • avoid using the same data for both the key and IV; 避免对密钥和IV使用相同的数据;

This line here: 这行在这里:

managed.CreateEncryptor(bytes, bytes)

Doesn't work. 不起作用 The first parameter is a key, and the second parameter is an initialization vector. 第一个参数是键,第二个参数是初始化向量。 If your intention was to use the string s as a "password" or key, try using Rfc2898DeriveBytes to generate a key from a password. 如果您打算将字符串s用作“密码”或密钥,请尝试使用Rfc2898DeriveBytes从密码生成密钥。

public static void EncryptFile(string input, string output)
{
    string theKey = "urKey";
    byte[] salt = new byte[] { 0x26, 0xdc, 0xff, 0x00, 0xad, 0xed, 0x7a, 0xee, 0xc5, 0xfe, 0x07, 0xaf, 0x4d, 0x08, 0x22, 0x3c };
    Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(theKey, salt);
    RijndaelManaged RMCrypto = new RijndaelManaged();
    using (var inputStream=new FileStream(input))
        using (var outputStream = new FileStream(output))
            using (CryptoStream cs = new CryptoStream(inputStream, RMCrypto.CreateEncryptor(pdb.GetBytes(32), pdb.GetBytes(16)), CryptoStreamMode.Read))
            {
                byte[] buffer = new byte[1024];
                int bytesRead = 0;
                do
                {
                    bytesRead = cs.Read(buffer, 0, buffer.Length);
                    outputStream.Write(buffer, 0, bytesRead);
                } while (bytesRead > 0);
            }
}

暂无
暂无

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

相关问题 指定的初始化向量(IV)与该算法的块大小不匹配 - The specified initialization vector (IV) does not match the block size for this algorithm 指定的初始化向量(IV)与此算法的块大小不匹配 - Specified initialization vector(IV) does not match the block size for this algorithm 指定的初始化向量 (IV) 与此算法的块大小不匹配 - Specified initialization vector (IV) does not match the block size for this algorithm 指定的初始化向量 (IV) 与此算法的块大小不匹配 - Specified initialization vector (IV) does not match the block size for this algorithm 连接 WCF Cryptography.CryptographicException:指定的初始化向量 (IV) 与此算法的块大小不匹配 - Connect WCF Cryptography.CryptographicException: Specified initialization vector (IV) does not match the block size for this algorithm 指定的初始化向量(IV)与使用AES的c#中的块大小不匹配 - Specified initialization vector (IV) does not match the block size in c# using AES AESCrypt如何处理文件格式2的初始化向量(IV)? - How does AESCrypt handle the initialization vector (IV) for file format 2? 将 IV 添加到 CryptoStream 的开头 - Add IV to beginning of CryptoStream C#Rijndael IV大小与块大小不匹配,但它应该 - C# Rijndael IV size doesn't match block size, while it should SymmetricAlgorithm密钥和初始化向量(IV)的实现 - SymmetricAlgorithm Key and initialization vector (IV) implementation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM