I’m working on a procedure for decrypting binary data using AesCryptoServiceProvider. For the final step, retrieving the decrypted data and returning it as an array of bytes, I’m currently using the following implementation:
let rec streamBytes (s : CryptoStream) (b : int) = seq {
if b >= 0 then
yield byte b
yield! streamBytes s (s.ReadByte()) }
streamBytes cryptoStream (cryptoStream.ReadByte())
|> Seq.toArray
It works, but it doesn’t feel “correct” to me. Passing the result of CryptoStream.ReadByte() as an argument to streamBytes(), and then checking is value in that recursive call seems a bit Rube Goldberg-y. Is there a better way to be doing it?
Draining stream byte by byte will be very slow.
if you have .NET 4.0 then the most straightforward way will be:
else you need to reproduce CopyTo functionality manually