# Usage of CBC (Cipher Block Chaining) Mode with Padding

## Overview

* **Rule ID**: `java_lang_padding_oracle_encryption_vulnerability`
* **Applicable Languages**: Java
* **Weakness ID**: CWE-327

## Description

Using a block cipher algorithm mode like CBC (Cipher Block Chaining) with a padding scheme is susceptible to Padding Oracle attacks. This vulnerability occurs because attackers can exploit the padding scheme to decrypt messages.

## Remediation Guidelines

* **Avoid** using CBC mode with padding for encryption, as this combination is vulnerable to security breaches.

  ```java
  Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding"); // unsafe
  ```
* **Instead, consider using GCM (Galois/Counter Mode)** for encryption implementation. GCM provides a more secure alternative that mitigates the risks associated with CBC mode.

  ```java
  Cipher c = Cipher.getInstance("AES/GCM/PKCS5Padding");
  ```

## References

* [**Java Cipher class**](https://docs.oracle.com/en/java/javase/20/docs/api/java.base/javax/crypto/Cipher.html)
* [**Java Security Standard Algorithm Names**](https://docs.oracle.com/en/java/javase/20/docs/specs/security/standard-names.html)
* [**CWE-327: Use of a Broken or Risky Cryptographic Algorithm**](https://cwe.mitre.org/data/definitions/327.html)
* [**OWASP Top 10: A02:2021 - Cryptographic Failures**](https://owasp.org/Top10/A02_2021-Cryptographic_Failures/)

## Configuration

To omit this rule during a scan, and to provide you with continuous 24/7 code-level scanning, you can employ our [**SAST TOOL**](https://scopy.sec1.io/login)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.sec1.io/user-docs/4-sast/2-java/usage-of-cbc-mode-with-padding.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
