Как исправить «Длина данных для расшифровки неверна» в этом коде - PullRequest
0 голосов
/ 04 февраля 2019

У меня проблемы с кодом ниже.Это не мое, я скопировал его из этого вопроса в Переполнение стека .

public readonly byte[] salt = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // Must be at least eight bytes.  MAKE THIS SALTIER!
public const int iterations = 1042; // Recommendation is >= 1000.       


/// <summary>Decrypt a file.</summary>
/// <remarks>NB: "Padding is invalid and cannot be removed." is the Universal CryptoServices error.  Make sure the password, salt and iterations are correct before getting nervous.</remarks>
/// <param name="sourceFilename">The full path and name of the file to be decrypted.</param>
/// <param name="destinationFilename">The full path and name of the file to be output.</param>
/// <param name="password">The password for the decryption.</param>
/// <param name="salt">The salt to be applied to the password.</param>
/// <param name="iterations">The number of iterations Rfc2898DeriveBytes should use before generating the key and initialization vector for the decryption.</param>
public void DecryptFile(string sourceFilename, string destinationFilename, string password, byte[] salt, int iterations)
{
    AesManaged aes = new AesManaged();
    aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
    aes.KeySize = aes.LegalKeySizes[0].MaxSize;
    // NB: Rfc2898DeriveBytes initialization and subsequent calls to   GetBytes   must be eactly the same, including order, on both the encryption and decryption sides.
    Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(password, salt, iterations);
    aes.Key = key.GetBytes(aes.KeySize / 8);
    aes.IV = key.GetBytes(aes.BlockSize / 8);
    aes.Mode = CipherMode.CBC;
    ICryptoTransform transform = aes.CreateDecryptor(aes.Key, aes.IV);

    using (FileStream destination = new FileStream(destinationFilename, FileMode.CreateNew, FileAccess.Write, FileShare.None))
    {
        using (CryptoStream cryptoStream = new CryptoStream(destination, transform, CryptoStreamMode.Write))
        {
            try
            {
                using (FileStream source = new FileStream(sourceFilename, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    source.CopyTo(cryptoStream);
                    source.Flush();
                }
            }
            catch (CryptographicException exception)
            {
                if (exception.Message == "Padding is invalid and cannot be removed.")
                    throw new ApplicationException("Universal Microsoft Cryptographic Exception (Not to be believed!)", exception);
                else
                    throw;
            }
        }
    }
}

/// <summary>Encrypt a file.</summary>
/// <param name="sourceFilename">The full path and name of the file to be encrypted.</param>
/// <param name="destinationFilename">The full path and name of the file to be output.</param>
/// <param name="password">The password for the encryption.</param>
/// <param name="salt">The salt to be applied to the password.</param>
/// <param name="iterations">The number of iterations Rfc2898DeriveBytes should use before generating the key and initialization vector for the decryption.</param>
public void EncryptFile(string sourceFilename, string destinationFilename, string password, byte[] salt, int iterations)
{
    AesManaged aes = new AesManaged();
    aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
    aes.KeySize = aes.LegalKeySizes[0].MaxSize;
    // NB: Rfc2898DeriveBytes initialization and subsequent calls to   GetBytes   must be eactly the same, including order, on both the encryption and decryption sides.
    Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(password, salt, iterations);
    aes.Key = key.GetBytes(aes.KeySize / 8);
    aes.IV = key.GetBytes(aes.BlockSize / 8);
    aes.Mode = CipherMode.CBC;
    ICryptoTransform transform = aes.CreateEncryptor(aes.Key, aes.IV);

    using (FileStream destination = new FileStream(destinationFilename, FileMode.CreateNew, FileAccess.Write, FileShare.None))
    {
        using (CryptoStream cryptoStream = new CryptoStream(destination, transform, CryptoStreamMode.Write))
        {
            using (FileStream source = new FileStream(sourceFilename, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                source.CopyTo(cryptoStream);
            }
        }
    }
}

После этого я получил исключение: Длина данных для расшифровки недопустима.Но после того, как тот файл, который я зашифровал, все равно зашифрован, но когда вы пытаетесь расшифровать его, он показывает сообщение: Недостаточно памяти для завершения этой операции.Во всяком случае, он работает правильно с мультимедийными файлами (я пробовал .mp3, .mkv, .jpg), но не правильно с документами (.txt, .docx).Итак, я надеюсь, что кто-то может помочь мне с этим, и еще один вопрос (для этой темы).Как сохранить пароль для каждого файла?Каждый раз, когда вы используете неверный пароль для расшифровки файла, он не расшифровывает его.Есть ли способ показать, что пароль неверный?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...