我有一个使用 RSACryptoServiceProvider 使用已知私钥(存储在变量中)解密一些数据的应用程序。
当 IIS 应用程序池配置为使用网络服务时,一切运行良好。
但是,当我们配置 IIS 应用程序池以在不同的身份下运行代码时,我们会得到以下信息:
System.Security.Cryptography.CryptographicException:系统找不到指定的文件。 在 System.Security.Cryptography.Utils.CreateProvHandle(CspParameters 参数,布尔随机密钥容器) 在 System.Security.Cryptography.RSACryptoServiceProvider.ImportParameters(RSAParameters 参数) 在 System.Security.Cryptography.RSA.FromXmlString(字符串 xmlString)
代码是这样的:
byte[] input;
byte[] output;
string private_key_xml;
var provider = new System.Cryptography.RSACryptoServiceProvider(this.m_key.Key_Size);
provider.FromXmlString(private_key_xml); // Fails Here when Application Pool Identity != Network Service
ouput = provider.Decrypt(input, false); // False = Use PKCS#1 v1.5 Padding
有一些资源试图通过说明您应该授予用户对机器密钥存储的读取权限来回答它 - 但是没有明确的答案来解决这个问题。
环境:IIS 6.0、Windows Server 2003 R2、.NET 3.5 SP1