
* Modify JVM Crypto rules: Change title * changed names * Apply suggestions from code review * fixed includes
89 lines
2.0 KiB
Plaintext
89 lines
2.0 KiB
Plaintext
== How to fix it in Java Cryptography Extension
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
include::../../common/fix/aes-noncompliant-example.adoc[]
|
|
|
|
[source,kotlin,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
import javax.crypto.Cipher
|
|
import javax.crypto.NoSuchPaddingException
|
|
import java.security.NoSuchAlgorithmException
|
|
|
|
fun main(args: Array<String>) {
|
|
try {
|
|
val aes = Cipher.getInstance("AES/CBC/PKCS5Padding"); // Noncompliant
|
|
} catch (e: NoSuchAlgorithmException) {
|
|
// ...
|
|
} catch (e: NoSuchPaddingException) {
|
|
// ...
|
|
}
|
|
}
|
|
----
|
|
|
|
include::../../common/fix/rsa-noncompliant-example.adoc[]
|
|
|
|
[source,kotlin,diff-id=2,diff-type=noncompliant]
|
|
----
|
|
import javax.crypto.Cipher
|
|
import javax.crypto.NoSuchPaddingException
|
|
import java.security.NoSuchAlgorithmException
|
|
|
|
fun main(args: Array<String>) {
|
|
try {
|
|
val rsa = Cipher.getInstance("RSA/None/NoPadding"); // Noncompliant
|
|
} catch (e: NoSuchAlgorithmException) {
|
|
// ...
|
|
} catch (e: NoSuchPaddingException) {
|
|
// ...
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
==== Compliant solution
|
|
|
|
include::../../common/fix/aes-compliant-example.adoc[]
|
|
|
|
[source,kotlin,diff-id=1,diff-type=compliant]
|
|
----
|
|
import javax.crypto.Cipher
|
|
import javax.crypto.NoSuchPaddingException
|
|
import java.security.NoSuchAlgorithmException
|
|
|
|
fun main(args: Array<String>) {
|
|
try {
|
|
val aes = Cipher.getInstance("AES/GCM/NoPadding");
|
|
} catch (e: NoSuchAlgorithmException) {
|
|
// ...
|
|
} catch (e: NoSuchPaddingException) {
|
|
// ...
|
|
}
|
|
}
|
|
----
|
|
|
|
include::../../common/fix/rsa-compliant-example.adoc[]
|
|
|
|
[source,kotlin,diff-id=2,diff-type=compliant]
|
|
----
|
|
import javax.crypto.Cipher
|
|
import javax.crypto.NoSuchPaddingException
|
|
import java.security.NoSuchAlgorithmException
|
|
|
|
fun main(args: Array<String>) {
|
|
try {
|
|
val rsa = Cipher.getInstance("RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING");
|
|
} catch (e: NoSuchAlgorithmException) {
|
|
// ...
|
|
} catch (e: NoSuchPaddingException) {
|
|
// ...
|
|
}
|
|
}
|
|
----
|
|
|
|
=== How does this work?
|
|
|
|
include::../../common/fix/fix.adoc[]
|