【问题标题】:System.Security.Cryptography.CryptographicException: Object already existsSystem.Security.Cryptography.CryptographicException:对象已存在
【发布时间】:2014-11-24 15:44:53
【问题描述】:

这很奇怪。我有这个方法来加密一个字符串:

[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Assert, Unrestricted = true)]
public static string Encrypt(this string stringToEncrypt, string key) {
    var cspp = new CspParameters { 
        KeyContainerName = key, 
        Flags = CspProviderFlags.UseMachineKeyStore 
    };
    var rsa = new RSACryptoServiceProvider(cspp) { 
        PersistKeyInCsp = true 
    };
    var bytes = rsa.Encrypt(System.Text.Encoding.UTF8.GetBytes(stringToEncrypt), true);
    return BitConverter.ToString(bytes);
}

这是我的客户:

private const string EncryptionKey = "pezhman";

static Random random = new Random();
public static int CreateSalt() {
    return random.Next(1000, 9999);
}

public void EncryptSomething() {
    var salt = CreateSalt();
    var plainText = salt + "," + DateTime.Now;
    var encryptionSaltKey = EncryptionKey + DateTime.Now.Date;
    // here im calling encryptor:
    var encryptedValue = plainText.Encrypt(encryptionSaltKey);
}

我在 ASP.NET MVC 4 应用程序中使用它。它运行良好;但突然它停止工作了。实际上,在本地,我没有问题并且它有效。但是,当我将代码发布到服务器时,我收到了这个错误:

System.Security.Cryptography.CryptographicException: 对象已经 存在。

你知道这里发生了什么吗?我知道我可以grant access to the key to everyone我要问的是服务器上发生了什么?有什么改变?什么样的变化会导致问题?

【问题讨论】:

  • 你已经看过这个了吗? stackoverflow.com/questions/11430966/…
  • @rodrigogq 是的,我已经在我的问题中提到了这一点。我想知道服务器发生了什么?什么样的变化会导致问题?
  • 对不起,我之前没有点击链接。提供一些背景信息:您刚刚更改了您的应用程序吗?安装了任何 Windows 更新,什么 IIS?您的 AppPool 是使用任何特定用户还是默认用户?有人改过吗?
  • @rodrigogq 好吧,这就是问题所在。因为我不知道而且我的托管公司没有回答!另外,我改变了密钥,问题解决了。但是,我仍然想知道刚刚发生了什么!无论如何,感谢 cmets。祝你好运

标签: c# asp.net-mvc asp.net-mvc-4 c#-4.0 cryptography


【解决方案1】:

我要问的是,服务器刚刚发生了什么?有什么改变?什么样的变化会导致问题?

一种可能性是最近发布的 Windows 安全更新 MS14-059,尽管我无法解释您收到的错误消息。

基本上,该更新完全卸载了 MVC 4.0.0.0 并在您的服务器上将其替换为 4.0.0.1,这让许多构建损坏的人感到悲痛。由于加密可能依赖于非常特定于 DLL 版本号的内容,因此您可能希望从那里开始。你可以通过在没有安装上述安全补丁的机器上测试你的应用程序来证明或反驳这个理论,看看它是否再次开始工作。

【讨论】:

  • 我更换了钥匙,它得到了解决。但是关于服务器,我认为你是对的。在此发生之前,服务器大约 3 或 4 分钟无法访问。他们可能已经安装了提到的(或类似的)更新。不幸的是,我的托管公司概不负责。每件事都是猜测。但感谢帮助。 +1
猜你喜欢
  • 2012-07-10
  • 2012-08-19
  • 1970-01-01
  • 1970-01-01
  • 2011-06-13
  • 2012-10-06
  • 2018-10-28
  • 2011-03-31
  • 2012-09-12
相关资源
最近更新 更多