sipeed/picoclaw · error

invalid padding byte

Error message

invalid padding byte

What it means

Guard in pkcs7Unpad: the declared padding length is within legal range, but one of the trailing padding bytes does not equal the declared value — PKCS7 requires all padding bytes to be identical. As with the size check, this signals the decryption produced garbage, i.e. wrong key or corrupted ciphertext.

Solutions

  1. Confirm the EncodingAESKey is correct and decodes to the same 32-byte key used for encryption
  2. Verify ciphertext integrity (complete download, correct slicing)
  3. Compare the IV strategy against WeCom media decryption documentation
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at pkg/channels/wecom/media.go:131 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/8249053344ff8b37. Report an issue: GitHub.

Appendix: source

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

		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"
	case "image/webp":
		return ".webp"
	case "application/pdf":
		return ".pdf"

View on GitHub (pinned to 49183d7e8d)