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