CryptoStream padding in .NET 5.0 different from .NET Framework 4.6 ?

Udo Riedel 1 Reputation point
2021-02-02T17:33:39.177+00:00

I have a program de-/encrypting data and running under .NET Framework 4.6 using CryptoStream and AES.
When I run the same code in a .NET 5.0 program, I can decrypt anything encrypted with 4.6. But I cannot decrypt files in 4.6 that were encrypted by the 5.0 program.
In 4.6 this gives the exception "The input data is not a complete block.".

In one of my example files the original file size is 22.263 bytes. Encrypted with 4.6 it gives a file of 22.272 bytes which is in my opinion the correct size for AES (128 bit block length, so 1392 blocks). Encrypted with 5.0 it gives a file of 22.264 bytes (1391 and a half block?!).
I tried every available padding (I use PKCS7) in .NET 5.0 but every time it gives 22.264 bytes as file size.
Does anyone know whats wrong there?

Here is some sample code:

        static void EncryptFile(string fileId)
        {
            AesCryptoServiceProvider cryptic = new AesCryptoServiceProvider();

            cryptic.Mode = CipherMode.CFB;
            cryptic.Key = ASCIIEncoding.ASCII.GetBytes(docId.ToLower().Substring(0, 32));
            cryptic.IV = ASCIIEncoding.ASCII.GetBytes(docId.ToLower().Substring(0, 16));

            using (FileStream fsTarget = new FileStream(fileId, FileMode.CreateNew))
            {
                using (CryptoStream cs = new CryptoStream(fsTarget, cryptic.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    using (FileStream fsSource = new FileStream(fileId + ".enc", FileMode.Open, FileAccess.Read)) //, FileShare.ReadWrite);
                    {
                        fsSource.CopyTo(cs);
                    }
                }
            }
        }

        static void DecryptFile(string fileId)
        {
            using (FileStream src = new FileStream(fileId, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                AesCryptoServiceProvider cryptic = new AesCryptoServiceProvider();

                cryptic.Mode = CipherMode.CFB;
                cryptic.Key = ASCIIEncoding.ASCII.GetBytes(fileId.ToLower().Substring(0, 32));
                cryptic.IV = ASCIIEncoding.ASCII.GetBytes(fileId.ToLower().Substring(0, 16));

                using (CryptoStream cs = new CryptoStream(src, cryptic.CreateDecryptor(), CryptoStreamMode.Read))
                {
                    using (FileStream tgt = new FileStream(fileId + ".dec", FileMode.CreateNew))
                    {
                        cs.CopyTo(tgt);
                        tgt.Close();
                    }
                }
            }
        }
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,309 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,126 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 56,931 Reputation points
    2021-09-14T17:27:19.547+00:00
                         using (FileStream tgt = new FileStream(fileId + ".dec", FileMode.CreateNew))
                         {
                             cs.CopyTo(tgt);
                             cs.FlushFinalBlock();
                             tgt.Close();
                         }
    
    0 comments No comments