2023-06-20 10:27:53 +02:00
|
|
|
== How to fix it in PyCrypto
|
|
|
|
|
|
|
|
=== Code examples
|
|
|
|
|
|
|
|
==== Noncompliant code example
|
|
|
|
|
|
|
|
include::../../common/fix/aes-noncompliant-example.adoc[]
|
|
|
|
|
2023-08-21 15:22:49 +02:00
|
|
|
[source,python,diff-id=11,diff-type=noncompliant]
|
2023-06-20 10:27:53 +02:00
|
|
|
----
|
|
|
|
from Crypto.Cipher import AES
|
|
|
|
|
|
|
|
AES.new(key, AES.MODE_ECB) # Noncompliant
|
|
|
|
----
|
|
|
|
|
|
|
|
include::../../common/fix/rsa-noncompliant-example.adoc[]
|
|
|
|
|
2023-08-21 15:22:49 +02:00
|
|
|
[source,python,diff-id=12,diff-type=noncompliant]
|
2023-06-20 10:27:53 +02:00
|
|
|
----
|
|
|
|
from Crypto.Cipher import PKCS1_v1_5
|
|
|
|
|
|
|
|
PKCS1_v1_5.new(key) # Noncompliant
|
|
|
|
----
|
|
|
|
|
|
|
|
==== Compliant solution
|
|
|
|
|
|
|
|
Since PyCrypto is not supported anymore, another library should be used.
|
|
|
|
In the current context, Cryptodome uses a similar API.
|
|
|
|
|
|
|
|
include::../../common/fix/aes-compliant-example.adoc[]
|
|
|
|
|
2023-08-21 15:22:49 +02:00
|
|
|
[source,python,diff-id=11,diff-type=compliant]
|
2023-06-20 10:27:53 +02:00
|
|
|
----
|
|
|
|
from Crypto.Cipher import AES
|
|
|
|
|
|
|
|
AES.new(key, AES.MODE_GCM)
|
|
|
|
----
|
|
|
|
|
|
|
|
include::../../common/fix/rsa-compliant-example.adoc[]
|
|
|
|
|
2023-08-21 15:22:49 +02:00
|
|
|
[source,python,diff-id=12,diff-type=compliant]
|
2023-06-20 10:27:53 +02:00
|
|
|
----
|
|
|
|
from Crypto.Cipher import PKCS1_OAEP
|
|
|
|
|
|
|
|
PKCS1_OAEP.new(key)
|
|
|
|
----
|
|
|
|
|
|
|
|
=== How does this work?
|
|
|
|
|
|
|
|
include::../../common/fix/fix.adoc[]
|