Changing key in runtime

Topics: Cryptography Application Block
Apr 7, 2011 at 2:38 PM


I'm writting a web-service, one of service's task - generating IDs and encrypting them with 3DES.  I need to change key  in runtime. how can i make it?

i.e. I need to generate 1k Ids, and than 2k Ids with another key.

Apr 8, 2011 at 9:03 AM
Edited Apr 8, 2011 at 9:03 AM


You can achieve this by manually creating your own Algorithm Provider and Protected Key. You'll have to reference the Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.dll. Here is a sample code:

string textToEncrypt = "Sample";

 ProtectedKey key = KeyManager.GenerateSymmetricKey("System.Security.Cryptography.TripleDESCryptoServiceProvider", DataProtectionScope.LocalMachine);

SymmetricAlgorithmProvider provider = new SymmetricAlgorithmProvider(typeof(System.Security.Cryptography.TripleDESCryptoServiceProvider), key);

string encryptedData = Convert.ToBase64String(provider.Encrypt(UnicodeEncoding.Unicode.GetBytes(textToEncrypt.ToCharArray(), 0, textToEncrypt.Length)));

string decryptedData = UnicodeEncoding.Unicode.GetString(provider.Decrypt(Convert.FromBase64String(encryptedData)));



Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.