2023-06-28 17:25:56 +02:00
|
|
|
== How to fix it in Cryptodome
|
|
|
|
|
|
|
|
=== Code examples
|
|
|
|
|
|
|
|
==== Noncompliant code example
|
|
|
|
|
2023-08-21 15:22:49 +02:00
|
|
|
[source,python,diff-id=11,diff-type=noncompliant]
|
2023-06-28 17:25:56 +02:00
|
|
|
----
|
|
|
|
from Crypto.Cipher import AES
|
|
|
|
from Crypto.Random import get_random_bytes
|
|
|
|
from Crypto.Util.Padding import pad
|
|
|
|
|
|
|
|
iv = "doNotTryThis@Home2023"
|
|
|
|
cipher = AES.new(key, AES.MODE_CBC, iv)
|
|
|
|
cipher.encrypt(pad(data, AES.block_size)) # Noncompliant
|
|
|
|
----
|
|
|
|
|
|
|
|
==== Compliant solution
|
|
|
|
|
|
|
|
:explicit_strong: Crypto.Random.get_random_bytes
|
|
|
|
|
|
|
|
include::../../common/fix/explicit-fix.adoc[]
|
|
|
|
|
2023-08-21 15:22:49 +02:00
|
|
|
[source,python,diff-id=11,diff-type=compliant]
|
2023-06-28 17:25:56 +02:00
|
|
|
----
|
|
|
|
from Crypto.Cipher import AES
|
|
|
|
from Crypto.Random import get_random_bytes
|
|
|
|
from Crypto.Util.Padding import pad
|
|
|
|
|
|
|
|
iv = get_random_bytes(AES.block_size)
|
|
|
|
cipher = AES.new(key, AES.MODE_CBC, iv)
|
|
|
|
cipher.encrypt(pad(data, AES.block_size))
|
|
|
|
----
|
|
|
|
|
|
|
|
=== How does this work?
|
|
|
|
|
|
|
|
include::../../common/fix/fix.adoc[]
|
|
|
|
|