peass-ng/PEASS-ng · error · InvalidOperationException
invalid encoding value:
Error message
invalid encoding value:
What it means
The Encoding property setter enforces the same constraint as the constructor: the External encoding value must be 0 (single-ASN1-type), 1 (octet-aligned), or 2 (arbitrary). Assigning any other value throws InvalidOperationException with the offending value appended to the message.
Source
Thrown at winPEAS/winPEASexe/winPEAS/3rdParty/BouncyCastle/asn1/DerExternal.cs:166
/**
* The encoding of the content. Valid values are
* <ul>
* <li><code>0</code> single-ASN1-type</li>
* <li><code>1</code> OCTET STRING</li>
* <li><code>2</code> BIT STRING</li>
* </ul>
*/
public int Encoding
{
get
{
return encoding;
}
set
{
if (encoding < 0 || encoding > 2)
throw new InvalidOperationException("invalid encoding value: " + encoding);
this.encoding = value;
}
}
public Asn1Object ExternalContent
{
get { return externalContent; }
set { this.externalContent = value; }
}
public DerInteger IndirectReference
{
get { return indirectReference; }
set { this.indirectReference = value; }
}
private static Asn1Object GetObjFromVector(Asn1EncodableVector v, int index)View on GitHub (pinned to 53fb989abc)
Solutions
- Clamp or validate the value before assignment: only 0, 1, or 2 are legal
- Map your application's encoding enum to the correct 0-2 range before setting
- If the encoding is unknown, don't assign a sentinel — construct a new DerExternal once the real value is known
Example fix
// before
external.Encoding = encodingId; // may be -1 or 3
// after
if (encodingId < 0 || encodingId > 2)
throw new ArgumentOutOfRangeException(nameof(encodingId));
external.Encoding = encodingId; Defensive patterns
Strategy: validation
Validate before calling
if (encodingValue < 0 || encodingValue > 2)
throw new ArgumentOutOfRangeException(nameof(encodingValue), "External encoding must be 0, 1 or 2");
external.Encoding = encodingValue; Type guard
static bool IsValidExternalEncoding(int v) => v == 0 || v == 1 || v == 2;
Try / catch
try { external.Encoding = value; }
catch (InvalidOperationException ex) when (ex.Message.StartsWith("invalid encoding value")) {
// clamp or map the value before retrying
} Prevention
- Treat Encoding as an enum with exactly three members (0-2), not a free int
- Map your app's encoding constants to the 0-2 range once, centrally
- Never use sentinel values like -1 for 'unset' on this property
When it happens
Trigger: Assigning external.Encoding = n where n < 0 or n > 2 in C# code that builds or mutates a DerExternal instance.
Common situations: Application code mapping its own protocol enum onto the Encoding property with out-of-range values; copy/paste code using a sentinel like -1 for 'unknown'; off-by-one constants (e.g. 3) for encodings.
Related errors
- invalid encoding value
- unknown object encountered in constructed OCTET STRING:
- unknown tag {tagNo} encountered
- unexpected end-of-contents marker
- indefinite-length primitive encoding encountered
AI-assisted analysis of peass-ng/PEASS-ng@53fb989abc (2026-09-02).
Data as JSON: /api/errors/c29328384b159984.
Report an issue: GitHub.