Encrypt an Object

Problem

You want to use encryption on an entire object, not just individual pieces of data.

Solution

Use .NET serialization to convert the object to a stream of bytes, and encrypt the serialized data.

Discussion

Recipes Serialize an Object to Disk, Serialize Public Members of a Nonserializable Object, and Perform Selective Serialization with the Memento Pattern demonstrated different ways to serialize an object to a stream of bytes. You can use this technique in conjunction with symmetric or asymmetric encryption to encrypt the contents of a serializable object. If you’re using asymmetric encryption, you’ll need to convert the object to an array of bytes and then encrypt these bytes one block at a time. If you’re using symmetric encryption, ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.