在阅读了企业库源代码一段时间后,我找到了一种方法来实现这一点。这只是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);
}