rspec/rules/S3329/java/rule.adoc

55 lines
1.4 KiB
Plaintext
Raw Normal View History

include::../description.adoc[]
2020-06-30 12:48:39 +02:00
== Noncompliant Code Example
2022-02-04 17:28:24 +01:00
[source,java]
2020-06-30 12:48:39 +02:00
----
2022-12-02 14:53:09 +01:00
public void encrypt(String key, String plainText) {
byte[] bytesIV = "7cVgr5cbdCZVw5WY".getBytes("UTF-8"); // Static initialization vector
2020-06-30 12:48:39 +02:00
IvParameterSpec iv = new IvParameterSpec(bytesIV);
SecretKeySpec skeySpec = new SecretKeySpec(strKey.getBytes("UTF-8"), "AES");
2022-12-02 14:53:09 +01:00
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); // Noncompliant: the IV is hard coded and thus not generated with a secure random generator
2020-06-30 12:48:39 +02:00
}
----
== Compliant Solution
2022-02-04 17:28:24 +01:00
[source,java]
2020-06-30 12:48:39 +02:00
----
2022-12-02 14:53:09 +01:00
public void encrypt(String key, String plainText) {
SecureRandom random = new SecureRandom();
2020-06-30 12:48:39 +02:00
byte[] bytesIV = new byte[16];
2022-12-02 14:53:09 +01:00
random.nextBytes(bytesIV); // Random initialization vector
2020-06-30 12:48:39 +02:00
IvParameterSpec iv = new IvParameterSpec(bytesIV);
SecretKeySpec skeySpec = new SecretKeySpec(strKey.getBytes("UTF-8"), "AES");
2022-12-02 14:53:09 +01:00
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
2020-06-30 12:48:39 +02:00
}
----
2022-12-02 14:53:09 +01:00
include::../see.adoc[]
2020-06-30 12:48:39 +02:00
* Derived from FindSecBugs rule https://find-sec-bugs.github.io/bugs.htm#STATIC_IV[STATIC_IV]
2022-12-02 14:53:09 +01:00
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]