Search code examples
c#.netcryptographydsa

DSACryptoServiceProvider.ToXmlString throws 'Invalid flags specified' exception


I'm trying to generate a key pair with DSACryptoServiceProvider.

Here's the code:

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

        DSA dsa = new DSACryptoServiceProvider(2048, cspParameters); // Generate a new 2048 bit RSA key

        string publicPrivateKeyXML = dsa.ToXmlString(true);
        string publicOnlyKeyXML = dsa.ToXmlString(false);

On dsa.ToXmlString(true); I'm getting following exception: Invalid flags specified. What's wrong?


Solution

  • The maximum keysize property is 1024, see this article: DSACryptoServiceProvider.KeySize Property

    "This algorithm supports key lengths from 512 bits to 1024 bits in increments of 64 bits."