using (FileStream tgt = new FileStream(fileId + ".dec", FileMode.CreateNew))
{
cs.CopyTo(tgt);
cs.FlushFinalBlock();
tgt.Close();
}
CryptoStream padding in .NET 5.0 different from .NET Framework 4.6 ?
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();
}
}
}
}
1 answer
Sort by: Most helpful
-
Bruce (SqlWork.com) 71,266 Reputation points
2021-09-14T17:27:19.547+00:00