2021-04-26 17:29:13 +02:00
|
|
|
include::../description.adoc[]
|
|
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
|
|
|
|
----
|
|
|
|
val bytesIV = "7cVgr5cbdCZVw5WY".toByteArray(charset("UTF-8")) // Predictable / hardcoded IV
|
|
|
|
|
|
|
|
val iv = IvParameterSpec(bytesIV)
|
|
|
|
val skeySpec = SecretKeySpec(secretKey.toByteArray(), "AES")
|
|
|
|
|
|
|
|
val cipher: Cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING")
|
|
|
|
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv) // Noncompliant (s3329)
|
|
|
|
|
|
|
|
val encryptedBytes: ByteArray = cipher.doFinal("foo".toByteArray())
|
|
|
|
----
|
|
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
|
|
|
|
----
|
|
|
|
val random: SecureRandom = SecureRandom()
|
|
|
|
|
|
|
|
val bytesIV: ByteArray = ByteArray(16)
|
|
|
|
random.nextBytes(bytesIV); // Unpredictable / random IV
|
|
|
|
|
|
|
|
val iv = IvParameterSpec(bytesIV)
|
|
|
|
val skeySpec = SecretKeySpec(secretKey.toByteArray(), "AES")
|
|
|
|
|
|
|
|
val cipher: Cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING")
|
|
|
|
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv) //Compliant (s3329)
|
|
|
|
|
|
|
|
val encryptedBytes: ByteArray = cipher.doFinal("foo".toByteArray())
|
|
|
|
----
|
|
|
|
|
|
|
|
include::../see.adoc[]
|
2021-06-02 20:44:38 +02:00
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
ifdef::env-github,rspecator-view[]
|
2021-06-02 20:44:38 +02:00
|
|
|
== Comments And Links
|
|
|
|
(visible only on this page)
|
|
|
|
|
|
|
|
include::../comments-and-links.adoc[]
|
2021-06-03 09:05:38 +02:00
|
|
|
endif::env-github,rspecator-view[]
|