2014-11-24 153 views
-1

這很奇怪。我有這個方法來加密的字符串:System.Security.Cryptography.CryptographicException:對象已存在

[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: Object already exists.

你有任何想法,這裏發生了什麼?我知道我可以grant access to the key to everyone我在問什麼是在服務器上發生了什麼?什麼改變了?什麼樣的改變會導致這個問題?

+0

你已經看過這個嗎? http://stackoverflow.com/questions/11430966/system-security-cryptography-cryptographicexception-object-already-exist/11445029#11445029 – rodrigogq 2014-11-24 15:51:28

+0

@rodrigogq是的我已經在我的問題中提到過。我想知道服務器發生了什麼?什麼樣的改變會導致這個問題? – 2014-11-24 15:54:12

+0

對不起,我以前沒有鏈接過鏈接。在此提供一些背景知識:你剛剛改變了你的應用程序嗎?任何Windows更新安裝,什麼IIS?您的AppPool使用任何特定用戶還是默認值?有人改變了嗎? – rodrigogq 2014-11-24 16:03:54

回答

1

What I'm asking is, what just happened at the server? What is changed? What kind of changes can cause the problem?

一種可能性是最近發佈的Windows secuirty更新MS14-059,雖然我不能解釋你所得到的錯誤消息。

基本上,該更新完全卸載MVC 4.0.0.0並將其替換爲您的服務器上的4.0.0.1,並且它已經導致許多破損構建人員的悲痛。由於加密可能取決於DLL的版本號非常特定的內容,因此您可能需要從此處開始。您可以通過在未安裝上述安全修補程序的計算機上測試您的應用程序來驗證或反駁該理論,以查看它是否再次開始工作。

+0

我更換了鑰匙,並得到解決。但關於服務器,我認爲你是對的。在發生這種情況之前,服務器大約在3到4分鐘內無法訪問。他們可能已經安裝了(或類似的)更新。不幸的是我的託管公司不負責。每件事都是猜測。但多虧了幫助。 +1 – 2014-11-25 06:46:04

相關問題