Skip to main content

Weak random generator

Back to rules list


The random numbers generated could be predicted. The use of a predictable random value can lead to vulnerabilities when used in certain security critical contexts.

Risk

Vulnerable Code

Random rnd = new Random();
byte[] buffer = new byte[16];
rnd.GetBytes(buffer);
return BitConverter.ToString(buffer);

Solution

using System.Security.Cryptography;

RandomNumberGenerator rnd = RandomNumberGenerator.Create();

References

WASC-04: Insufficient Transport Layer Protection
CWE-295: Improper Certificate Validation