34 lines
966 B
Plaintext
34 lines
966 B
Plaintext
![]() |
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[]
|