The following code will always throw UnuthorizedAccessException (MemoryStream\'s internal buffer cannot be accessed.)
byte[] buf1 = { 2, 3, 5, 7, 11 }; var
Here is the documentation for MemoryStream(byte[]) constructor that you're using. It specifically says:
MemoryStream(byte[])
This constructor does not expose the underlying stream. GetBuffer throws UnauthorizedAccessException.
You should use this constructor instead, with publiclyVisible = true.
publiclyVisible = true