include::../description.adoc[]

== Noncompliant Code Example

----
public void Encrypt(byte[] key, byte[] data, MemoryStream target)
{
    byte[] initializationVector = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };

    using var aes = new AesCryptoServiceProvider();
    var encryptor = aes.CreateEncryptor(key, initializationVector); // Noncompliant, hardcoded value is used

    using var cryptoStream = new CryptoStream(target, encryptor, CryptoStreamMode.Write);
    cryptoStream.Write(data);
}
----

== Compliant Solution

----
public byte[] Encrypt(byte[] key, byte[] data, MemoryStream target)
{
    using var aes = new AesCryptoServiceProvider();
    var encryptor = aes.CreateEncryptor(key, aes.IV); // aes.IV is automatically generated to random secure value

    using var cryptoStream = new CryptoStream(target, encryptor, CryptoStreamMode.Write);
    cryptoStream.Write(data);

    return aes.IV;
}
----

include::../see.adoc[]

ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)

include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]
