A
Andrew Jocelyn
Hi
I've created an encrypt and decrypt function, modified from article
http://www.dotnetthis.com/Articles/Crypto.htm. It accepts streams instead of
file paths. The functions work if I send in FileStreams. I can encrypt and
decrypt. If I send in a MemoryStream to it then write the stream to the
website response stream the stream seems to be truncated. It needs to work
with all file types. It breaks even with a short text file.
Code below. What am I doing wrong?
using (var fileStream = new FileStream(fileName, FileMode.Open,
FileAccess.Read))
{
var ms = new MemoryStream();
Cryptography.Decrypt(fileStream, ms, "password");
byte[] prescan = ms.ToArray();
ms.Close();
Response.Clear();
Response.ContentType = ContentType;
Response.OutputStream.Write(prescan, 0, prescan.Length);
Response.End();
}
// decrypt function
public static void Decrypt(Stream fsIn,
Stream fsOut, string Password)
{
var pdb = new Rfc2898DeriveBytes(Password,
new byte[]
{
0x49, 0x76, 0x61, 0x6e,
0x20, 0x4d,
0x65, 0x64, 0x76, 0x65,
0x64, 0x65, 0x76
});
TripleDES alg = TripleDES.Create();
alg.Key = pdb.GetBytes(16);
alg.IV = pdb.GetBytes(8);
var cs = new CryptoStream(fsOut, alg.CreateDecryptor(),
CryptoStreamMode.Write);
const int bufferLen = 4096;
var buffer = new byte[bufferLen];
int bytesRead;
do
{
bytesRead = fsIn.Read(buffer, 0, bufferLen);
cs.Write(buffer, 0, bytesRead);
} while (bytesRead != 0);
//cs.Close(); // commented out to stop output steam being closed
before it's used
fsIn.Close();
}
Many thanks
Andrew
I've created an encrypt and decrypt function, modified from article
http://www.dotnetthis.com/Articles/Crypto.htm. It accepts streams instead of
file paths. The functions work if I send in FileStreams. I can encrypt and
decrypt. If I send in a MemoryStream to it then write the stream to the
website response stream the stream seems to be truncated. It needs to work
with all file types. It breaks even with a short text file.
Code below. What am I doing wrong?
using (var fileStream = new FileStream(fileName, FileMode.Open,
FileAccess.Read))
{
var ms = new MemoryStream();
Cryptography.Decrypt(fileStream, ms, "password");
byte[] prescan = ms.ToArray();
ms.Close();
Response.Clear();
Response.ContentType = ContentType;
Response.OutputStream.Write(prescan, 0, prescan.Length);
Response.End();
}
// decrypt function
public static void Decrypt(Stream fsIn,
Stream fsOut, string Password)
{
var pdb = new Rfc2898DeriveBytes(Password,
new byte[]
{
0x49, 0x76, 0x61, 0x6e,
0x20, 0x4d,
0x65, 0x64, 0x76, 0x65,
0x64, 0x65, 0x76
});
TripleDES alg = TripleDES.Create();
alg.Key = pdb.GetBytes(16);
alg.IV = pdb.GetBytes(8);
var cs = new CryptoStream(fsOut, alg.CreateDecryptor(),
CryptoStreamMode.Write);
const int bufferLen = 4096;
var buffer = new byte[bufferLen];
int bytesRead;
do
{
bytesRead = fsIn.Read(buffer, 0, bufferLen);
cs.Write(buffer, 0, bytesRead);
} while (bytesRead != 0);
//cs.Close(); // commented out to stop output steam being closed
before it's used
fsIn.Close();
}
Many thanks
Andrew