.net SecureString, mas e os outros tipos de dados?

Eu usei string segura antes, mas agora eu precisava proteger outros tipos de objetos na memória, qual é a melhor maneira de fazer isso?

Comece com o seguinte exemplo, tirado daqui :

using System;
using System.Security.Cryptography;

public class DataProtectionSample
{
// Create byte array for additional entropy when using Protect method.
static byte [] s_aditionalEntropy = { 9, 8, 7, 6, 5 };

public static void Main()
{
// Create a simple byte array containing data to be encrypted.

byte [] secret = { 0, 1, 2, 3, 4, 1, 2, 3, 4 };

//Encrypt the data.
byte [] encryptedSecret = Protect( secret );
Console.WriteLine("The encrypted byte array is:");
PrintValues(encryptedSecret);

// Decrypt the data and store in a byte array.
byte [] originalData = Unprotect( encryptedSecret );
Console.WriteLine("{0}The original data is:", Environment.NewLine);
PrintValues(originalData);

}

public static byte [] Protect( byte [] data )
{
try
{
// Encrypt the data using DataProtectionScope.CurrentUser. The result can be decrypted
// only by the same current user.
return ProtectedData.Protect( data, s_aditionalEntropy, DataProtectionScope.CurrentUser );
}
catch (CryptographicException e)
{
Console.WriteLine("Data was not encrypted. An error occurred.");
Console.WriteLine(e.ToString());
return null;
}
}

public static byte [] Unprotect( byte [] data )
{
try
{
//Decrypt the data using DataProtectionScope.CurrentUser.
return ProtectedData.Unprotect( data, s_aditionalEntropy, DataProtectionScope.CurrentUser );
}
catch (CryptographicException e)
{
Console.WriteLine("Data was not decrypted. An error occurred.");
Console.WriteLine(e.ToString());
return null;
}
}

public static void PrintValues( Byte[] myArr )
{
foreach ( Byte i in myArr )
{
Console.Write( "t{0}", i );
}
Console.WriteLine();
}

}

Use esses métodos neste exemplo para proteger objetos mantidos na memória, todos os objetos .net podem ser representados com uma matriz de bytes, alguns com métodos nativos para serializá-los e desserializá-los. Você pode então usar os métodos get e set públicos para recuperar e armazenar os dados.

O seguinte mostra como podemos armazenar um guid em uma classe usando dados protegidos:

public Guid secureGUID
{
get
{
return new Guid(Unprotect(_secureGUID));
}
set
{
_secureGUID
= Protect(value.ToByteArray());
}
}

private byte[] _secureGUID;

A outra coisa que você vai querer ter certeza de fazer é executar o Array.Clear ao descartar a classe, para garantir que os dados do array foram substituídos por zeros na memória.