2012-07-11 10 views
7
public RSAKeyPair() 
    { 
     string keyContainerName="pEncKey" 
     CspParameters cspp = new CspParameters(); 
     cspp.Flags = CspProviderFlags.UseMachineKeyStore; 
     cspp.KeyContainerName = keyContainerName; 
     try 
     { 
      m_RSA = new RSACryptoServiceProvider(1024, cspp); 
     } 
     catch(Exception e){} 
    } 

co jest powodem do rzucania następujący wyjątek:System.Security.Cryptography.CryptographicException -przedmiot już istnieją

System.Security.Cryptography.CryptographicException - object already exist 

ślad stosu jest następująca:

at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr) 
    at System.Security.Cryptography.Utils._CreateCSP(CspParameters param, Boolean randomKeyContainer, SafeProvHandle& hProv) 
    at System.Security.Cryptography.Utils.CreateProvHandle(CspParameters parameters, Boolean randomKeyContainer) 
    at System.Security.Cryptography.Utils.GetKeyPairHelper(CspAlgorithmType keyType, CspParameters parameters, Boolean randomKeyContainer, Int32 dwKeySize, SafeProvHandle& safeProvHandle, SafeKeyHandle& safeKeyHandle) 
    at System.Security.Cryptography.RSACryptoServiceProvider.GetKeyPair() 
    at System.Security.Cryptography.RSACryptoServiceProvider..ctor(Int32 dwKeySize, CspParameters parameters, Boolean useDefaultKeySize) 
    at System.Security.Cryptography.RSACryptoServiceProvider..ctor(Int32 dwKeySize, CspParameters parameters) 
    at XXXXXXXX.Core.RSAKeyPair..ctor(String keyContainerName) 
+0

Szybkie google to się okazało: http://pwnedcode.wordpress.com/2008/11/10/fixing-cryptographicexception-%E2%80 % 9Cobject-już-istnieje% E2% 80% 9D /. Czy próbowałeś naprawić dostęp do głównego kontenera? –

+0

@whestead dla wiersza poleceń asp.net. :( – DevT

Odpowiedz

11

Dzieje się tak dlatego, że program działa z różnymi użytkownikami. Jeden z normalnym użytkownikiem i drugi z użytkownikiem startowym.

Po utworzeniu klucza jego uprawnienia są przyznawane tylko twórcy.

Dlatego należy zmienić uprawnienia klucza, aby mógł on być używany przez wszystkich.

CspParameters cspParams; 
cspParams = new CspParameters(PROVIDER_RSA_FULL); 
cspParams.KeyContainerName = CONTAINER_NAME; 
cspParams.Flags = CspProviderFlags.UseMachineKeyStore; 
cspParams.ProviderName = "Microsoft Strong Cryptographic Provider"; 

CryptoKeyAccessRule rule = new CryptoKeyAccessRule("everyone", CryptoKeyRights.FullControl, AccessControlType.Allow); 

cspParams.CryptoKeySecurity = new CryptoKeySecurity(); 
cspParams.CryptoKeySecurity.SetAccessRule(rule); 

więcej szczegółów

http://whowish-programming.blogspot.com/2010/10/systemsecuritycryptographycryptographic.html

Powiązane problemy