jeffa00 pointed out that this can be done by simple serialization if your data set isn't very large. Here is a complete working implementation to show how easy it is:
public class DataSerializer<T>
{
static XmlSerializer _serializer = new XmlSerializer(typeof(T));
static SymmetricAlgorithm _encryptionAlgorithm;
public static void SetEncryptionKey(byte[] key)
{
_encryptionAlgorithm = Aes.Create();
_encryptionAlgorithm.Key = key;
}
public static void WriteData(string filePath, T data)
{
using(var fileStream = File.OpenWrite(filePath))
{
var cryptoStream = new CryptoStream(fileStream, _encryptionAlgorithm, CryptoStreamMode.Write);
_serializer.Serialize(cryptoStream, data);
cryptoStream.Flush();
}
}
public static T ReadData(string filePath)
{
using(var fileStream = File.OpenRead(filePath))
{
var cryptoStream = new CryptoStream(fileStream, _encryptionAlgorithm, CryptoStreamMode.Read);
return (T)_serializer.Deserialize(Stream);
}
}
}
With this you can save an entire tree of objects to disk in encrypted form by simply doing:
DataSerializer<MyObjectType>.WriteData(@"c:\somewhere\something.data", myObject);
and read it back with:
myObject = DataSerializer<MyObjectType>.ReadData(@"c:\somewhere\something.data");
This is much nicer than working with SQL Server Compact or even full SQL Server, since LINQ to Objects is so much more powerful than LINQ to SQL. Faster too in many cases.
But this only works if your dataset easily fits in RAM.