C#idiomatic C#
ca5351fundamentals/code-analysis/quality-rules

Code analysis

CA5351: Do Not Use Broken Cryptographic Algorithms

Learn about code analysis rule CA5351: Do Not Use Broken Cryptographic Algorithms

CA5351 Security enabled by default in .NET 10: No
Fix is breaking or non-breaking Non-breakingApplicable languages C# and Visual Basic

Cause

Hashing functions such as System.Security.Cryptography.MD5 and encryption algorithms such as System.Security.Cryptography.DES and System.Security.Cryptography.RC2 can expose significant risk and may result in the exposure of sensitive information through trivial attack techniques, such as brute force attacks and hash collisions.

The cryptographic algorithms list below are subject to known cryptographic attacks. The cryptographic hash algorithm System.Security.Cryptography.MD5 is subject to hash collision attacks. Depending on the usage, a hash collision may lead to impersonation, tampering, or other kinds of attacks on systems that rely on the unique cryptographic output of a hashing function. The encryption algorithms System.Security.Cryptography.DES and System.Security.Cryptography.RC2 are subject to cryptographic attacks that may result in unintended disclosure of encrypted data.

Rule description

Broken cryptographic algorithms are not considered secure and their use should be discouraged. The MD5 hash algorithm is susceptible to known collision attacks, though the specific vulnerability will vary based on the context of use. Hashing algorithms used to ensure data integrity (for example, file signature or digital certificate) are particularly vulnerable. In this context, attackers could generate two separate pieces of data, such that benign data can be substituted with malicious data, without changing the hash value or invalidating an associated digital signature.

For encryption algorithms:

This rule triggers when it finds any of the above cryptographic functions in source code and throws a warning to the user.

How to fix violations

Use cryptographically stronger options:

When to suppress warnings

Do not suppress a warning from this rule, unless it's been reviewed by a cryptographic expert.

Suppress a warning

If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

csharp
#pragma warning disable CA5351
// The code that's violating the rule is on this line.
#pragma warning restore CA5351

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

ini
[*.{cs,vb}]
dotnet_diagnostic.CA5351.severity = none

For more information, see How to suppress code analysis warnings.

Pseudo-code examples

The following pseudo-code samples illustrate the pattern detected by this rule and possible alternatives.

MD5 Hashing Violation

csharp
using System.Security.Cryptography;
...
var hashAlg = MD5.Create();

Solution:

csharp
using System.Security.Cryptography;
...
var hashAlg = SHA256.Create();

RC2 Encryption Violation

csharp
using System.Security.Cryptography;
...
RC2 encAlg = RC2.Create();

Solution:

csharp
using System.Security.Cryptography;
...
using (AesManaged encAlg = new AesManaged())
{
  ...
}

DES Encryption Violation

csharp
using System.Security.Cryptography;
...
DES encAlg = DES.Create();

Solution:

csharp
using System.Security.Cryptography;
...
using (AesManaged encAlg = new AesManaged())
{
  ...
}