【问题标题】:ASP.NET RSACryptoServiceProvider throws Invalid flags specified exceptionASP.NET RSACryptoServiceProvider 引发 Invalid flags specified 异常
【发布时间】:2011-01-16 16:30:54
【问题描述】:

我在尝试使用 ASP.NET (.NET 4) 为 RSA 生成密钥时遇到问题,RSACryptoServiceProvider 会抛出无效标志指定异常。

[CryptographicException:指定的标志无效。 ] System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 小时)+33 System.Security.Cryptography.Utils._GenerateKey(SafeProvHandle hProv, Int32 algid, CspProviderFlags flags, Int32 keySize, SafeKeyHandle& hKey) +0 System.Security.Cryptography.Utils.GetKeyPairHelper(CspAlgorithmType keyType, CspParameters 参数, Boolean randomKeyContainer, Int32 dwKeySize, SafeProvHandle& safeProvHandle, SafeKeyHandle& safeKeyHandle) +9719339 System.Security.Cryptography.RSACryptoServiceProvider.GetKeyPair() +89 System.Security.Cryptography.RSACryptoServiceProvider.ExportParameters(Boolean includePrivateParameters) +38 System.Security.Cryptography.RSA.ToXmlString(Boolean includePrivateParameters) +45

我使用以下代码初始化 RSA:

            var _cpsParameter = new CspParameters();
        _cpsParameter.Flags = CspProviderFlags.UseMachineKeyStore;

        var rsaProvider = new RSACryptoServiceProvider(bitStrength, _cpsParameter);

        string publicAndPrivateKeys = rsaProvider.ToXmlString(true);
        string justPublicKey = rsaProvider.ToXmlString(false);

知道如何解决这个问题吗?

【问题讨论】:

  • bitStrength 值是多少?

标签: asp.net cryptography


【解决方案1】:

一种可能的解释是,您在密钥容器中已经有一个名为“”的不可导出密钥(在这种情况下,.NET 将只使用现有密钥,而不是用新密钥覆盖它)。

试试这个代码,看看是否有什么不同:

    var cspParameters = new CspParameters();
    cspParameters.Flags = CspProviderFlags.UseMachineKeyStore;
    cspParameters.KeyContainerName = Guid.NewGuid().ToString();

    var rsaProvider = new RSACryptoServiceProvider(bitStrength, cspParameters);

    string publicAndPrivateKeys = rsaProvider.ToXmlString(true);
    string justPublicKey = rsaProvider.ToXmlString(false);

【讨论】:

    猜你喜欢
    • 2010-09-10
    • 2020-05-08
    • 1970-01-01
    • 1970-01-01
    • 2012-09-27
    • 2014-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多