Is it possible to get the encrypted form of a value that has just been encrypted using .NET Rijndael

I want to encrypt and decript small snippets of data. The data will be stored locally in a cookie, and remotely, in the database, in encrypted form. So, after the data value is encrypted in ASP.NET, how can I get the encrypted version so it can be passed to a stored procedure as a string value that gets written to a table in the database?

Using the .NET Rijndael encryption approach described here is it possible, immediately after writing the plain text value to the CryptoStream, to fetch its encrypted form? So that instead of writing "The message was sent" to the console, one would write the encrypted form of the value?

If so, how do we read the encrypted value from the CryptoStream after the encryption is complete?

Also, does this have to be done with a NetStream? Is there another kind of stream that doesn't require a TCP client?

 Dim CryptStream As New CryptoStream(NetStream, RMCrypto.CreateEncryptor(Key, IV), CryptoStreamMode.Write)

  'Create a StreamWriter for easy writing to the 
  'network stream.
  Dim SWriter As New StreamWriter(CryptStream)

  'Write to the stream.
  SWriter.WriteLine("Hello World!")

  'Inform the user that the message was written
  'to the stream.
  Console.WriteLine("The message was sent.")
Jon Skeet
people
quotationmark

The simplest way of doing this would be just to use a MemoryStream

  • Encrypt to the MemoryStream
  • Close the writer at the end, to make sure everything's flushed
  • Call MemoryStream.ToArray to get the encrypted data
  • Write the encrypted data to the original target (NetStream in this case, whatever that is)
  • Display the encrypted data - probably after base64-encoding it

people

See more on this question at Stackoverflow