== How to fix it in .NET

=== Code examples

include::../../common/fix/code-rationale.adoc[]

==== Noncompliant code example

include::../../common/fix/rsa.adoc[]

[source,csharp,diff-id=1,diff-type=noncompliant]
----
using System;
using System.Security.Cryptography;

public void encrypt()
{
    var RsaCsp = new RSACryptoServiceProvider(); // Noncompliant
}
----

include::../../common/fix/dsa.adoc[]

[source,csharp,diff-id=2,diff-type=noncompliant]
----
using System;
using System.Security.Cryptography;

public void encrypt()
{
    var DsaCsp = new DSACryptoServiceProvider(); // Noncompliant
}
----

include::../../common/fix/ec.adoc[]

[source,csharp,diff-id=3,diff-type=noncompliant]
----
using System;
using System.Security.Cryptography;

public void encrypt()
{
    ECDsa ecdsa = ECDsa.Create(ECCurve.NamedCurves.brainpoolP160t1); // Noncompliant
}
----

==== Compliant solution

[source,csharp,diff-id=1,diff-type=compliant]
----
using System;
using System.Security.Cryptography;

public void encrypt()
{
    var RsaCsp = new RSACryptoServiceProvider(2048);
}
----

[source,csharp,diff-id=2,diff-type=compliant]
----
using System;
using System.Security.Cryptography;

public void encrypt()
{
    var Dsa = new DSACng(2048);
}
----

[source,csharp,diff-id=3,diff-type=compliant]
----
using System;
using System.Security.Cryptography;

public void encrypt()
{
    ECDsa ecdsa = ECDsa.Create(ECCurve.NamedCurves.nistP256);
}
----

=== How does this work?

include::../../common/fix/fix.adoc[]

=== Pitfalls

==== The KeySize Property is not a setter

The following code is invalid:

[source, csharp]
 ----
     var RsaCsp = new RSACryptoServiceProvider();
     RsaCsp.KeySize = 2048;
----
 
The KeySize property of CryptoServiceProviders cannot be updated because the setter simply
does not exist. This means that this line will not perform any update on `KeySize`, and the compiler
won't raise an Exception when compiling it. This should not be considered a workaround. +
To change the key size, use one of the overloaded constructors with the desired key size instead.


=== Going the extra mile

include::../../common/extra-mile/pre-quantum.adoc[]
