Attilio Gelosa
Attilio Gelosa

Reputation: 561

Ecrypt/decrypt with CryptoStream

I'm trying to encrypt and decrypt streams using CryptoStream (Aes). These are the method I'm using:

public void Encrypt(Stream input, Stream output)
    {
        Aes aes = Aes.Create();

        aes.Key = Key;
        aes.IV = IV;
        aes.Padding = PaddingMode.PKCS7;
        //aes.Mode = CipherMode.CBC;
        //aes.BlockSize = 128;

        ICryptoTransform aesEncryptor = aes.CreateEncryptor();

        using (CryptoStream cryptoStream = new(output, aesEncryptor, CryptoStreamMode.Write))
        {
            input.CopyTo(cryptoStream);
            //cryptoStream.FlushFinalBlock();
            cryptoStream.Flush();
            cryptoStream.Close();
        }
    }

end

public void Decrypt(Stream input, Stream output)
    {
        Aes aes = Aes.Create();

        aes.Key = Key;
        aes.IV = IV;
        aes.Padding = PaddingMode.PKCS7;
        //aes.Mode = CipherMode.CBC;
        //aes.BlockSize = 128;

        ICryptoTransform aesDecryptor = aes.CreateDecryptor();

        using (CryptoStream cryptoStream = new(input, aesDecryptor, CryptoStreamMode.Read))
        {
            cryptoStream.CopyTo(output);
            cryptoStream.Flush();
            cryptoStream.Close();            }
    }

What I'm doing:

What I obtain are files with the last line not equal (compared using the old but gold WinMerge): the original file, as I wrote, has the "abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMOPQRSTUVWXYZ_1234567890_0987654321" line, while the result file has "abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMOPQRSTUVWXYZ_1234567890_0987" (note that six final characters are missing).

Any idea on how to solve the problem?

Upvotes: 0

Views: 1062

Answers (1)

Attilio Gelosa
Attilio Gelosa

Reputation: 561

Thanks to Mathias R. Jassen for the suggestion. Following the working code:

public void Encrypt(Stream input, Stream output)
    {
        Aes aes = Aes.Create();

        aes.Key = Key;
        aes.IV = IV;
        aes.Padding = PaddingMode.PKCS7;
        //aes.Mode = CipherMode.CBC;
        //aes.BlockSize = 128;

        ICryptoTransform aesEncryptor = aes.CreateEncryptor();

        using (CryptoStream cryptoStream = new(output, aesEncryptor, CryptoStreamMode.Write))
        {
            input.CopyTo(cryptoStream);
            cryptoStream.FlushFinalBlock();
        }
    }

and

public void Decrypt(Stream input, Stream output)
    {
        Aes aes = Aes.Create();

        aes.Key = Key;
        aes.IV = IV;
        aes.Padding = PaddingMode.PKCS7;
        //aes.Mode = CipherMode.CBC;
        //aes.BlockSize = 128;

        ICryptoTransform aesDecryptor = aes.CreateDecryptor();

        using (CryptoStream cryptoStream = new(input, aesDecryptor, CryptoStreamMode.Read))
        {
            cryptoStream.CopyTo(output);
            cryptoStream.Close();
        }
        output.Flush();
    }

Please, note the

output.Flush()

command!

Upvotes: 2

Related Questions