Copying from memory stream to File Stream fails - c#

I am attempting to copy a memory stream to a file stream. I noticed that the output exe is corrupt when decrypted. I am certain there is no issue with the decrypt function. Here is the code
private MemoryStream My_Encrypt(Stream inputFile)
{
//FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);
MemoryStream fsCrypt = new MemoryStream();
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write);
int data;
while ((data = inputFile.ReadByte()) != -1)
cs.WriteByte((byte)data);
inputFile.Flush();
return fsCrypt;
}
MemoryStream ms = My_Encrypt(bundleStream);
ms.Seek(0, SeekOrigin.Begin);
FileStream atest = new FileStream("c:\\Somefile.exe",FileMode.Create);
ms.Seek(0, SeekOrigin.Begin);
ms.CopyTo(atest);
atest.Close();
More details:
The reason I am saying that the memory stream method is not working is because in My_Encrypt method if I replace fsCrypt with FileStream instead of Memory Stream and close fsCrypt at the end of the method and then reopen the saved file and write it to another file it works. My question is why is the memory stream method not working.

I believe you have to call FlushFinalBlock on the CryptoStream.
private MemoryStream My_Encrypt(Stream inputFile)
{
//FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);
MemoryStream fsCrypt = new MemoryStream();
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write);
int data;
while ((data = inputFile.ReadByte()) != -1)
cs.WriteByte((byte)data);
cs.FlushFinalBlock();
return fsCrypt;
}

Related

Xml encrypted data error

I made the following code to insert data to an encrypted xml file, I used a memory stream for the decrypted file and load it in a XmlDocument to add the required data, then encrypted it again back to the same file.
public static void IN_EncryptFile(MemoryStream FLin,string Path)
{
UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(sKey);
string cryptFile = Path;
FileStream fsCrypt = new FileStream(cryptFile, FileMode.Open);
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream cs = new CryptoStream(fsCrypt,
RMCrypto.CreateEncryptor(key, key),
CryptoStreamMode.Write);
FLin.Position = 0;
int data;
while ((data = FLin.ReadByte()) != -1)
{
cs.WriteByte((byte)data);
}
FLin.Close();
FLin.Flush();
cs.Close();
fsCrypt.Close();
}
public static MemoryStream OUT_DecryptFile(string Path)
{
UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(sKey);
FileStream fsCrypt = new FileStream(Path, FileMode.Open);
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream cs = new CryptoStream(fsCrypt,
RMCrypto.CreateDecryptor(key, key),
CryptoStreamMode.Read);
MemoryStream fsOut = new MemoryStream();
int data;
while ((data = cs.ReadByte()) != -1)
fsOut.WriteByte((byte)data);
cs.Close();
fsCrypt.Close();
fsOut.Position = 0;
return fsOut;
}
public static void Add_Data_XML()
{
string XML_Pt = #"C:\Test.Trr";
XmlDocument XDt = new XmlDocument();
XDt.Load(Cryption.OUT_DecryptFile(XML_Pt));
/// Adding XMl data
MemoryStream Fnl = new MemoryStream();
XDt.Save(Fnl);
Cryption.IN_EncryptFile(Fnl, XML_Pt);
Fnl.Flush();
Fnl.Close();
}
I need an opinion about the code as it works fine for me but sometimes it generates some errors in the encrypted file which I still don't fully understand why, but when I dencrypt the xml file I find the added data are in the wrong format and placed after the main XMl node as follow :
<Main_Node>
</Main_Node>”ÇÛÏ”ö8—´Ú·…ï/1Ž"‹ÓÃåõ¶—QÝUŸy…¤Êç‹íîzR߆ô
nÃFçiŽÌm–FÆzÍW9 úv¤ï_øVO,ÈvÄ
Your issue is that you use MemoryStream to store the temporary data and you read from it while you can. MemoryStream has an internal buffer that is larger than the data you write to it usually. So if you keep reading from it, you may get garbage data out from it. I would recommend not reading byte by byte from it, or at least get the actual length of the data and read that many bytes.
You also first close the MemoryStream and then flush it. This is pointless.
Another problem could be because you use FileMode.Open and write less than the previously existed in the file. This will leave existing data after what you write and that will show as garbage. Using FileMode.Create would be the right way here, since it means "if there is no file with this name, create a new one. If there is a file, truncate it."
FileMode documentation
But if you surely always write more than you read, then this shouldn't be an issue.
Also I wouldn't say "it works fine for me" if it actually has issues.
I think you have an encoding issue. Try this
MemoryStream ms = new MemoryStream();
XDt.Save(ms);
StreamReader Fnl = new StreamReader(ms, Encoding.UTF8);

Open encrypted image as Bitmap C#

I need to perform data hiding in encrypted image. To perform data hiding i need to have bitmap Image. But i don't know how to save the image as bitmap.
Below is my encryption code.
public void EncryptFile(string source, string destination)
{
string sKey = "super545";
FileStream fsInput = new FileStream(source, FileMode.Open, FileAccess.Read);
FileStream fsEncrypted = new FileStream(destination, FileMode.Create, FileAccess.Write);
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
ICryptoTransform desencrypt = DES.CreateEncryptor();
CryptoStream cryptostream = new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write);
byte[] bytearrayinput = new byte[fsInput.Length - 1];
fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
cryptostream.Close();
fsInput.Close();
fsEncrypted.Close();
}
This is called like:
EncryptFile(originalimage, output);
output is a string variable with the path to store the encrypted image.
How can I call the function to run the encryption?
I receive the error the parameter is invalid when i hit this line:
Bitmap bitmap3 = new Bitmap(output);
I guess what you are trying to do is pretty close to this:
public void EncryptFile(string source, string destination)
{
string sKey = "super545";
FileStream fsInput = new FileStream(source, FileMode.Open, FileAccess.Read);
FileStream fsEncrypted = new FileStream(destination, FileMode.Create, FileAccess.Write);
//Consider to use something else, DES is dead
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
//use some key derivation function like pbkdf2 instead
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
//should be random, may be fixed ONLY for testing purposes
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
ICryptoTransform desencrypt = DES.CreateEncryptor();
CryptoStream cryptostream = new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write);
//byte[] bytearrayinput = new byte[fsInput.Length - 1]; // what do you need that big buffer for anyways?
//fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
//cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
byte[] headerBuffer = new byte[54]; // buffer for our bmp header ... without any color tables or masks
//No need for lots of checks in a proof of concept
fsInput.Read(headerBuffer, 0, headerBuffer.Length);
var biCompression = BitConverter.ToInt32(headerBuffer, 30); //get biComp from header
if (biCompression != 0 && biCompression != 3)
{
throw new Exception("Compression is not in the correct format");
}
//The buffer is copied without any encryption
fsEncrypted.Write(headerBuffer, 0, headerBuffer.Length);
//copy the rest and encrypt it ... don't care about color tables and masks for now
//and let's just hope plaintext and ciphertext have the right size
fsInput.CopyTo(cryptostream);
cryptostream.Close();
fsInput.Close();
fsEncrypted.Close();
}

XML writer and Memory Stream c#

I am creating a file using XmlWriter, XmlWriter writer = XmlWriter.Create(fileName); it is creating a file and then i have one more function which i am calling private void EncryptFile(string inputFile, string outputFile) which takes 2 string input and outpulfile and in the end i have two files one is encrypted and one is not. I just want one encrypted file but foor my encrypt function it takes inputfile which is created by XmlWriter. Is there any way i can create memorystream and pass that into my function instead of creating a inputfile.
my encrypt function
private void EncryptFile (string inputFile, string outputFile)
string password = #"fdds"; // Your Key Here
UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(password);
string cryptFile = outputFile;
FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream cs = new CryptoStream(fsCrypt,RMCrypto.CreateEncryptor(key,key),CryptoStreamMode.Write);
FileStream fsIn = new FileStream(inputFile, FileMode.Open);
int data;
while ((data = fsIn.ReadByte()) != -1)
cs.WriteByte((byte)data);
cs.FlushFinalBlock();
fsIn.Close();
cs.Close();
fsCrypt.Close();
}
}
You can create an XmlWriter that writes to a memory stream:
var stream = new MemoryStream();
var writer = XmlWriter.Create(stream);
Now you can pass this stream to your EncryptFile function instead of an inputFile. You have to make sure that you don't forget these two things before reading the stream:
Make a call to writer.Flush() when you are done writing.
Set stream.Position back to 0 before you start reading the stream.

Encrypting a ZipPackage Stream in Memory

I am using .NET 4.0 and trying to do the following:
Create a System.IO.Packaging.Package in Memory
Add Items to the package
encrypt package before it is written to file.
I tried to create a MemoryStream and add the files to it by:
using (var memoryZip = Package.Open(_memoryStream, FileMode.Open))
{
var partUri = PackUriHelper.CreatePartUri(_fileUri);
var part = memoryZip.CreatePart(
partUri,
String.Empty,
CompressionOption.NotCompressed);
using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
fileStream.CopyTo(packagePart.GetStream());
}
}
Then I tried to encrypt it and saved it to a file:
_key = new DESCryptoServiceProvider
{
Key = Encoding.ASCII.GetBytes(_password);
IV = Encoding.ASCII.GetBytes(_password);
}
var fileStream = File.Open(_fileName, FileMode.Create, FileAccess.Write);
var cryptoStream = new CryptoStream(streamToFile, _key.CreateEncryptor(), CryptoStreamMode.Write);
//Convert filestream to byte[]
var streamAsBytes = new byte[(_memoryStream.Length)];
_memoryStream.Read(streamAsBytes, 0, streamAsBytes.Length);
//Encrypt
cryptoStream.Write(streamAsBytes, 0, streamAsBytes.Length);
fileStream.Close();
cryptoStream.Flush();
cryptoStream.Close();
However, when I go to decrypt it:
var fileStream = new FileStream(_zipFileName, FileMode.Open, FileAccess.Read);
var cryptoStream = new CryptoStream(fileStream, _key.CreateDecryptor(), CryptoStreamMode.Read);
using(var zipPackage = Package.Open(cryptoStream, FileMode.Open))
I get a FileFormatException: "File contains corrupted data."
This isn't the same message you would get if decryption failed. I would expect that the stream that went in during encryption is the same one as the one that came out so any idea why the package would be corrupted?

how to encrypt/Decrypt .xlsx/.doc files with C#

I'm writing an application to Encrtpt/Decrypt files and using DESCryptoServiceProvider for this purpose. This seems to work for text files but when I use the same application for .xlsx file, the resulting encrypted file and the decrypted file get corrupted and I'm not able to open that any more. is there any way I can encryt / decrypt different kinds of file like .doc..xls etc.
update : added code for encryption/decryption
public static void EncryptFile(string filepath,string fileOutput, string key)
{
FileStream fsInput = new FileStream(filepath, FileMode.Open, FileAccess.Read);
FileStream fsEncrypted = new FileStream(fileOutput, FileMode.Create, FileAccess.Write);
DESCryptoServiceProvider DESc = new DESCryptoServiceProvider();
DESc.Key = ASCIIEncoding.ASCII.GetBytes(key);
DESc.IV = ASCIIEncoding.ASCII.GetBytes(key);
ICryptoTransform desEncrypt = DESc.CreateEncryptor();
CryptoStream cryptoStream = new CryptoStream(fsEncrypted, desEncrypt, CryptoStreamMode.Write);
byte[] byteArrayInput = new byte[fsInput.Length - 1];
fsInput.Read(byteArrayInput, 0, byteArrayInput.Length);
cryptoStream.Write(byteArrayInput, 0, byteArrayInput.Length);
cryptoStream.Close();
fsInput.Close();
fsEncrypted.Close();
}
public static void DecryptFile(string filepath, string fileOutput, string key)
{
DESCryptoServiceProvider DESc = new DESCryptoServiceProvider();
DESc.Key = ASCIIEncoding.ASCII.GetBytes(key);
DESc.IV = ASCIIEncoding.ASCII.GetBytes(key);
FileStream fsread = new FileStream(filepath, FileMode.Open, FileAccess.Read);
ICryptoTransform desDecrypt = DESc.CreateDecryptor();
CryptoStream cryptoStreamDcr = new CryptoStream(fsread, desDecrypt, CryptoStreamMode.Read);
StreamWriter fsDecrypted = new StreamWriter(fileOutput);
fsDecrypted.Write(new StreamReader(cryptoStreamDcr).ReadToEnd());
fsDecrypted.Flush();
fsDecrypted.Close();
}
static void Main(string[] args)
{
EncryptFile(#"C:\test1.xlsx", #"c:\test2.xlsx", "ABCDEFGH");
DecryptFile(#"C:\test2.xlsx", #"c:\test3.xlsx", "ABCDEFGH");
}
You are not encrypting or decrypting correctly. Encrypt -> Decrypt will always give a file identical to the input. If you post your code we may be able to assist in finding the bug in it.
You should use FileStreama as suggested in one of the comments by Kieren Johnstone. Also, you're not flushing the streams when encrypting - this may not be done automatically, so you should try and flush the streams, too.

Categories