2012-07-11 92 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){} 
    } 

¿cuál es la razón para tirar siguiente excepción:System.Security.Cryptography.CryptographicException -objeto ya existen

System.Security.Cryptography.CryptographicException - object already exist 

seguimiento de la pila es el siguiente:

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

Una rápida de Google volvió esto: http://pwnedcode.wordpress.com/2008/11/10/fixing-cryptographicexception-%E2%80 % 9Cobjeto-ya-existe% E2% 80% 9D /. ¿Has intentado arreglar el acceso al contenedor de llaves? –

+0

@owlstead eso es para la línea de comando asp.net uno. :( – DevT

Respuesta

11

Esto sucede porque el programa se ejecuta con diferentes usuarios. Uno con usuario normal y otro con usuario de inicio.

Cuando se crea la clave, su permiso solo se concede al creador.

Por lo tanto, debe cambiar el permiso de la clave para que pueda ser utilizada por todos.

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); 

para más detalles,

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

Cuestiones relacionadas