C# rijndael CryptoStream can I write to file bits and bits like the way textwriter does?
Was stuggling in how to encrypt 2 strings together.
Because I add bits and bits string in real time (by str = str + bitString;) and at the end. I generate the string and encrpt it.
Now the question is can I encypt and write the bitString in real time like textwriter? Something like: CrytoStream开发者_开发问答 cr = new (outFile,xxx,write) cr.write(bitString);
Examples would be appricated.
This is exactly what CryptoStream is designed to handle.
For a full sample, see MSDN's Documentation, in particular, the EncryptTextToFile method.
You just construct the CryptoStream "on top" of any FileStream (or any other stream), then write data directly to the cryptostream:
FileStream fStream = File.Open(fileName, FileMode.OpenOrCreate);
// Create the CryptoStream
CryptoStream cStream = new CryptoStream(fStream, Rijndael.Create().CreateEncryptor(Key, IV), CryptoStreamMode.Write);
// Create a StreamWriter using the CryptoStream.
StreamWriter sWriter = new StreamWriter(cStream);
// Write data to be encrypted:
sWriter.WriteLine("Some text that will get encrypted");
sWriter.WriteLine("More data...");
精彩评论