代码之家  ›  专栏  ›  技术社区  ›  Brettski

还原RijndaelManaged密钥而不使用EntLibConfig.exe

  •  0
  • Brettski  · 技术社区  · 7 年前

    我正在使用一个旧的应用程序,它使用企业库加密应用程序块进行数据加密。

    这个手工的过程需要取消。有没有办法用命令行重新创建这个键?或许通过编写一个应用程序来生成密钥?使用.NET文档中的RijndaelManaged引用,我还没有幸运地解决这个问题。

    实际上,任何基于当前密钥恢复RijndaelManaged密钥的解决方案都可能有用。没有用户界面或手动过程/点击!

    1 回复  |  直到 7 年前
        1
  •  0
  •   Brettski    7 年前

    在阅读了企业库源代码一段时间后,我找到了一种方法来实现这一点。这只是POC代码,没有支票等。

    假设:

    • c:\key\engagementproviderexport.txt 密钥是否从工作服务器导出
    • c:\key\engagementprovider.key
    • exportKeyPw 是用于保护导出密钥的密码
    • Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.dll 已添加参考
    • System.Security.dll 已添加参考
    • ProtectedKey
    • KeyManager

    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);
    }
    
    推荐文章