.netenterprise-libraryrijndaelmanaged

Restore RijndaelManaged key without using EntLibConfig.exe


I am working with an older application which uses the Enterprise Library Cryptography Application Block for data encryption.

Currently with every new server which is setup we have to manually run EntLibConfig.exe (a ui app), import a key exported from another machine, and restore the key for the new server.

This manual process needs to go away. Is there a way I can recreate this key using the command line? Perhaps by writing an application to generate the key? I haven't had luck figuring this out using the RijndaelManaged references in the .NET docs.

Using a Symmetric Provider, RijndaelManaged.

Actually any solution to restore a RijndaelManaged key based off of a current key would probably be useful. No UIs or manual processes/clicks!


Solution

  • After reading through the Enterprise Library source for a while I worked out a way to do this. This is just POC code, no checks, etc.

    Assumptions:

    POC:

    using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;
    using System.Security;
    using System.IO;
    
    string exportKeyFile = @"C:\key\EngagementProviderExport.txt";
    string exportKeyPw = "p@ssword";
    string saveKeyFile = @"C:\key\EngagementProvider.key";
    ProtectedKey NewServerKey;
    
    using (FileStream fs = File.OpenRead(exportKeyFile))
    {
        NewServerKey = KeyManager.RestoreKey(fs, exportKeyPw, System.Security.Cryptography.DataProtectionScope.LocalMachine);
    }
    
    using (FileStream ofs = File.OpenWrite(saveKeyFile)) 
    {
        KeyManager.Write(ofs, NewServerKey);
    }