sipeed/picoclaw · error

invalid padding size

Error message

invalid padding size %d

What it means

Guard in pkcs7Unpad: the decrypted plaintext's final byte declares a padding length that is 0, exceeds 32, or exceeds the data length — not a legal PKCS7 padding value. This almost always means the AES decryption used the wrong key/IV (garbage plaintext), since correct decryption yields valid padding.

Solutions

  1. Verify the AES key matches the one used to encrypt the media (regenerate from WeCom admin)
  2. Confirm the IV derivation matches WeCom's convention (here IV is the first block of the key)
  3. Check the ciphertext was not truncated or reordered before decryption
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at pkg/channels/wecom/media.go:127 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of sipeed/picoclaw@49183d7e8d (2026-08-15). Data as JSON: /api/errors/0de1313f8358cc02. Report an issue: GitHub.

Appendix: source

Thrown at pkg/channels/wecom/media.go:127

		return nil, fmt.Errorf("ciphertext length %d is not a multiple of block size", len(ciphertext))
	}
	block, err := aes.NewCipher(key)
	if err != nil {
		return nil, fmt.Errorf("create cipher: %w", err)
	}
	plaintext := make([]byte, len(ciphertext))
	iv := key[:aes.BlockSize]
	cipher.NewCBCDecrypter(block, iv).CryptBlocks(plaintext, ciphertext)
	return pkcs7Unpad(plaintext)
}

func pkcs7Unpad(data []byte) ([]byte, error) {
	if len(data) == 0 {
		return nil, fmt.Errorf("empty plaintext")
	}
	padding := int(data[len(data)-1])
	if padding == 0 || padding > 32 || padding > len(data) {
		return nil, fmt.Errorf("invalid padding size %d", padding)
	}
	for i := 0; i < padding; i++ {
		if data[len(data)-1-i] != byte(padding) {
			return nil, fmt.Errorf("invalid padding byte")
		}
	}
	return data[:len(data)-padding], nil
}

func inferMediaExt(contentType, fallback string) string {
	contentType = normalizeWeComContentType(contentType)
	switch contentType {
	case "image/jpeg", "image/jpg":
		return ".jpg"
	case "image/png":
		return ".png"
	case "image/gif":
		return ".gif"

View on GitHub (pinned to 49183d7e8d)