public static Stream ReadSalt(this Stream stream, string key)
{
try
{
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
byte[] readBytes = stream.ReadEntireBlock(keyBytes.Length);
if (!readBytes.SequenceEqual(keyBytes))
{
throw new InvalidDataException("The encrypted stream's salt was different than the expected salt.");
}
return stream;
}
catch (Exception ex)
{
throw new IOException("Encrypted stream is not correctly salted with the document key.", ex);
}
}
}