nilaoda/N_m3u8DL-RE · error · Exception

Key must be 32 bytes!

Error message

Key must be 32 bytes!

What it means

ChaCha20Util.DecryptPer1024Bytes is the segment-decryption helper. It requires a 32-byte key and throws Exception("Key must be 32 bytes!") otherwise (note the nonce check message is likewise mislabeled 'Key must be 12 or 8 bytes!'). This is a plain Exception, so catch blocks targeting ArgumentException will not catch it.

Solutions

  1. Ensure keyBytes is exactly 32 bytes (64 hex chars) before calling the helper.
  2. Use the correct decryption routine for 16-byte AES keys instead of ChaCha20.
  3. Fix key decoding (strip whitespace, use correct hex/base64) so the decoded buffer is 32 bytes.
  4. Catch base Exception, not just ArgumentException, when wrapping this helper's failures.

Example fix

// before
ChaCha20Util.DecryptPer1024Bytes(buf, key16, nonce); // 16-byte key
// after
if (key.Length != 32) throw new InvalidOperationException("need 32-byte key");
ChaCha20Util.DecryptPer1024Bytes(buf, key32, nonce);
Defensive patterns

Strategy: validation

Validate before calling

if (keyBytes?.Length != 32) throw new InvalidOperationException("ChaCha20 requires a 32-byte key");
if (nonceBytes?.Length is not (8 or 12)) throw new InvalidOperationException("nonce must be 8 or 12 bytes");

Type guard

bool CanChaChaDecrypt(byte[] k, byte[] n) => k is { Length: 32 } && n is { Length: 8 or 12 };

Try / catch

try { dec = ChaCha20Util.DecryptPer1024Bytes(buf, key, nonce); }
catch (Exception ex) { Console.Error.WriteLine($"decrypt failed: {ex.Message}"); } // plain Exception, not ArgumentException

Prevention

When it happens

Trigger: Calling DecryptPer1024Bytes with keyBytes.Length != 32 — e.g. a 16-byte AES key from --key parsing, a truncated/extended key buffer, or a key decoded with wrong encoding.

Common situations: SAMPLE-AES (16-byte key) streams routed into the ChaCha20 path; key hex strings with whitespace causing decode size mismatch; mixing up KID and KEY buffers.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of nilaoda/N_m3u8DL-RE@e113dee70c (2026-09-13). Data as JSON: /api/errors/fe7a252e28a039af. Report an issue: GitHub.

Appendix: source

Thrown at src/N_m3u8DL-RE/Crypto/ChaCha20Util.cs:10

using CSChaCha20;

namespace N_m3u8DL_RE.Crypto;

internal static class ChaCha20Util
{
    public static byte[] DecryptPer1024Bytes(byte[] encryptedBuff, byte[] keyBytes, byte[] nonceBytes)
    {
        if (keyBytes.Length != 32)
            throw new Exception("Key must be 32 bytes!");
        if (nonceBytes.Length != 12 && nonceBytes.Length != 8)
            throw new Exception("Key must be 12 or 8 bytes!");
        if (nonceBytes.Length == 8)
            nonceBytes = (new byte[4] { 0, 0, 0, 0 }).Concat(nonceBytes).ToArray();

        var decStream = new MemoryStream();
        using BinaryReader reader = new BinaryReader(new MemoryStream(encryptedBuff));
        using (BinaryWriter writer = new BinaryWriter(decStream))
            while (true)
            {
                var buffer = reader.ReadBytes(1024);
                byte[] dec = new byte[buffer.Length];
                if (buffer.Length > 0)
                {
                    ChaCha20 forDecrypting = new ChaCha20(keyBytes, nonceBytes, 0);
                    forDecrypting.DecryptBytes(dec, buffer);
                    writer.Write(dec, 0, dec.Length);
                }

View on GitHub (pinned to e113dee70c)