C# .NET ECB Cipher Mode Information Disclosure

High Risk Cryptographic Weakness
csharpdotnetencryptionecb-modecryptographic-weaknessinformation-disclosureaescipher-modessemgrep

What it is

A critical cryptographic vulnerability where .NET applications use Electronic Codebook (ECB) cipher mode for encryption, which reveals patterns in plaintext data and enables cryptanalytic attacks. ECB mode encrypts identical plaintext blocks to identical ciphertext blocks, allowing attackers to infer information about the original data structure and content. This vulnerability also lacks authentication, permitting undetected modification of encrypted data.

// VULNERABLE: Using ECB mode in .NET encryption
using System;
using System.Security.Cryptography;
using System.Text;

public class VulnerableEncryption
{
    // SECURITY ISSUE: ECB mode reveals patterns in plaintext
    public static byte[] EncryptDataECB(string plaintext, byte[] key)
    {
        using (Aes aes = Aes.Create())
        {
            aes.Key = key;
            aes.Mode = CipherMode.ECB;  // VULNERABLE: ECB mode
            aes.Padding = PaddingMode.PKCS7;
            
            using (ICryptoTransform encryptor = aes.CreateEncryptor())
            {
                byte[] plainBytes = Encoding.UTF8.GetBytes(plaintext);
                return encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
            }
        }
    }
    
    // VULNERABLE: Decryption without authentication
    public static string DecryptDataECB(byte[] ciphertext, byte[] key)
    {
        using (Aes aes = Aes.Create())
        {
            aes.Key = key;
            aes.Mode = CipherMode.ECB;  // VULNERABLE: ECB mode
            aes.Padding = PaddingMode.PKCS7;
            
            using (ICryptoTransform decryptor = aes.CreateDecryptor())
            {
                byte[] decryptedBytes = decryptor.TransformFinalBlock(ciphertext, 0, ciphertext.Length);
                return Encoding.UTF8.GetString(decryptedBytes);
            }
        }
    }
}
// SECURE: Using CBC mode with proper IV
using System;
using System.Security.Cryptography;
using System.Text;

public class SecureEncryption
{
    // SECURE: CBC mode with random IV
    public static (byte[] ciphertext, byte[] iv) EncryptDataCBC(string plaintext, byte[] key)
    {
        using (Aes aes = Aes.Create())
        {
            aes.Key = key;
            aes.Mode = CipherMode.CBC;  // SECURE: CBC mode
            aes.GenerateIV();  // Random IV for each encryption
            aes.Padding = PaddingMode.PKCS7;
            
            using (ICryptoTransform encryptor = aes.CreateEncryptor())
            {
                byte[] plainBytes = Encoding.UTF8.GetBytes(plaintext);
                byte[] ciphertext = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
                return (ciphertext, aes.IV);
            }
        }
    }
    
    // SECURE: CBC mode decryption
    public static string DecryptDataCBC(byte[] ciphertext, byte[] iv, byte[] key)
    {
        using (Aes aes = Aes.Create())
        {
            aes.Key = key;
            aes.Mode = CipherMode.CBC;  // SECURE: CBC mode
            aes.IV = iv;
            aes.Padding = PaddingMode.PKCS7;
            
            using (ICryptoTransform decryptor = aes.CreateDecryptor())
            {
                byte[] decryptedBytes = decryptor.TransformFinalBlock(ciphertext, 0, ciphertext.Length);
                return Encoding.UTF8.GetString(decryptedBytes);
            }
        }
    }
}

💡 Why This Fix Works

The vulnerable example shows ECB mode encryption that reveals patterns in plaintext data and lacks authentication. The secure implementations use AES-GCM for authenticated encryption or CBC mode with HMAC for older .NET versions, ensuring both confidentiality and integrity.

â„šī¸ Configuration Fix

Configuration changes required - see explanation below.

💡 Explanation

This demonstration shows how ECB mode reveals patterns when encrypting similar data, while secure modes like GCM produce different ciphertext even for identical plaintext due to random nonces.

Why it happens

Developers use ECB mode because it's sometimes the default or appears in basic cryptography examples. This commonly occurs when developers are unaware of the security implications of different cipher modes or when copying code from tutorials that prioritize simplicity over security. ECB mode is easier to implement but fundamentally insecure for most applications.

Root causes

Default Cipher Mode Selection

Developers use ECB mode because it's sometimes the default or appears in basic cryptography examples. This commonly occurs when developers are unaware of the security implications of different cipher modes or when copying code from tutorials that prioritize simplicity over security. ECB mode is easier to implement but fundamentally insecure for most applications.

Misunderstanding of Cipher Mode Security

Teams choose ECB mode thinking that strong encryption algorithms like AES automatically provide security regardless of the mode of operation. This happens when cryptographic education focuses on algorithm strength rather than proper mode selection and implementation. The belief that 'AES is secure' leads to overlooking the critical importance of cipher modes.

Legacy Code and Compatibility Requirements

Applications use ECB mode to maintain compatibility with legacy systems or existing encrypted data that was incorrectly encrypted using ECB. This often occurs when upgrading encryption is deemed too risky or expensive, leading to the perpetuation of insecure cryptographic practices for backward compatibility.

Fixes

1

Use AEAD Cipher Modes

Replace ECB mode with Authenticated Encryption with Associated Data (AEAD) modes like AES-GCM or ChaCha20-Poly1305. These modes provide both confidentiality and authenticity, preventing both pattern leakage and tampering. Use the .NET AesGcm class or similar AEAD implementations for new applications.

2

Implement Secure Cipher Modes with HMAC

If AEAD modes are not available, use secure modes like CBC or CTR with proper authentication using HMAC. Generate random initialization vectors (IVs) for each encryption operation, and always verify HMAC before decrypting. This provides both confidentiality and integrity protection.

3

Establish Cryptographic Standards

Create organizational standards that prohibit ECB mode and other insecure cryptographic practices. Implement code review processes that specifically check for proper cipher mode usage, require security review for all cryptographic implementations, and provide secure cryptographic libraries for developers to use.

Detect This Vulnerability in Your Code

Sourcery automatically identifies c# .net ecb cipher mode information disclosure and many other security issues in your codebase.