# Usage of ECB Cipher Mode

## Overview

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

## Description

The ECB (Electronic Codebook) cipher mode is considered insecure and should not be used in cryptographic protocols. It encrypts identical plaintext blocks into identical ciphertext blocks, making it susceptible to pattern analysis and inadequate for data protection. For stronger security, use encryption algorithms with built-in message integrity and no need for a configurable mode of operation, such as ChaCha20-Poly1305. For older applications that do not support these newer algorithms, AES-256-GCM is a recommended alternative.

## Remediation Guidelines

* **Do choose** ChaCha20-Poly1305 or AES-256-GCM for encryption. These algorithms include built-in message integrity, offering a more secure alternative to ECB mode.

  ```java
  Cipher chaChaCipher = Cipher.getInstance("ChaCha20-Poly1305/None/NoPadding");
  ```

## References

* [**Java Cipher class**](https://docs.oracle.com/en/java/javase/20/docs/api/java.base/javax/crypto/Cipher.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 Failuresn**](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-ecb-cipher-mode.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.
