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

lý do ném ngoại lệ sau đây là những gì:System.Security.Cryptography.CryptographicException -object đã tồn tại

System.Security.Cryptography.CryptographicException - object already exist 

stack trace như sau:

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

Một google nhanh chóng quay này lên: http://pwnedcode.wordpress.com/2008/11/10/fixing-cryptographicexception-%E2%80 % 9Cobject-đã tồn tại% E2% 80% 9D /. Bạn đã thử khắc phục quyền truy cập vào vùng chứa khóa chưa? –

+0

@owlstead dành cho dòng lệnh asp.net. :( – DevT

Trả lời

11

Điều này xảy ra bởi vì chương trình đang chạy với những người dùng khác nhau. Một với người dùng bình thường và một với người dùng khởi động.

Khi khóa được tạo, quyền của nó chỉ được cấp cho người tạo.

Vì vậy, bạn cần thay đổi quyền của khóa để mọi người có thể sử dụng khóa đó.

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

để biết thêm chi tiết,

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

Các vấn đề liên quan