views:

217

answers:

1

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.

+3  A: 

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...");
Reed Copsey
OMG, I forgot docu.Thx man
Kelvin