首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >System.Security.Cryptography.CryptographicException -object已存在

System.Security.Cryptography.CryptographicException -object已存在
EN

Stack Overflow用户
提问于 2012-07-11 18:45:45
回答 1查看 14.5K关注 0票数 9
代码语言:javascript
复制
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){}
    }

抛出以下异常的原因是什么:

代码语言:javascript
复制
  System.Security.Cryptography.CryptographicException - object already exist 

堆栈跟踪如下:

代码语言:javascript
复制
   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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-12 12:30:57

这是因为程序使用不同的用户运行。一个是普通用户,另一个是启动用户。

密钥创建后,权限仅授予创建者。

因此,您需要更改密钥的权限,以便所有人都可以使用它。

代码语言:javascript
复制
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);

有关详细信息,请参见

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

票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11430966

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档