O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Symmetrically Encrypt Any Data

Problem

You need to symmetrically encrypt data that isn’t stream-based, such as a series of variables.

Solution

Write these variables to a MemoryStream, and wrap this with a CryptoStream.

Discussion

All symmetric encryption is stream-based. If you need to convert data that’s stored in ordinary variables such as integers and strings, you must write this data to an in-memory stream.

The following Console application demonstrates this approach.

Public Module EncryptionTest Public Sub Main() ' Declare the data. Dim Name As String = "Joe Tester" Dim Age As Integer = 28 ' Create the in-memory stream. Dim ms As New System.IO.MemoryStream() ' Create the cryptographic object. Dim Rijndael As New RijndaelManaged() ' Write and encrypt ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required