Print Page | Close Window

Using an Encrypted EntitySet

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce Classic
Forum Discription: For .NET 2.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=363
Printed Date: 12-Jun-2026 at 1:26am


Topic: Using an Encrypted EntitySet
Posted By: IdeaBlade
Subject: Using an Encrypted EntitySet
Date Posted: 08-Aug-2007 at 3:53pm
A recent Tech Tip called "Saving and Restoring the Cache" briefly covered one method of encrypting the cache.  Here's another, better performing, way to save and restore an encrypted cache which takes advantage of stream chaining.  This simple example uses DES (remember to use the same key and initialization vector for both encryption and decryption):
 
 
using System.Security.Cryptography;
 
string fileName = Application.UserAppDataPath + file://\\MyLocalData.bin - "\\MyLocalData.bin ";
DES des = DES.Create();
DESCryptoServiceProvider desCSP = new DESCryptoServiceProvider();
 
To save to an encrypted file:
 
Stream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);
CryptoStream cs = new CryptoStream(fs, desCSP.CreateEncryptor(des.Key, des.IV), CryptoStreamMode.Write);
 
mPersistenceManager.SaveEntitySet(cs, true);
 
And to restore an encrypted file:
 
Stream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
CryptoStream cs = new CryptoStream(fs, desCSP.CreateDecryptor(des.Key, des.IV), CryptoStreamMode.Read);
 
mPersistenceManager.RestoreEntitySet(cs, RestoreStrategy.Normal, true);

 




Print Page | Close Window