peass-ng/PEASS-ng · error · ArgumentException

encoding error in GetInstance:

Error message

encoding error in GetInstance: 

What it means

DerBitString.GetInstance, given a byte[], parses it as an ASN.1 object and casts to DerBitString; any exception in that parse/cast is rethrown as ArgumentException 'encoding error in GetInstance: ' + exception details. The byte[] did not decode to a BIT STRING.

Source

Thrown at winPEAS/winPEASexe/winPEAS/3rdParty/BouncyCastle/asn1/DerBitString.cs:38

		 *
		 * @exception ArgumentException if the object cannot be converted.
		 */
        public static DerBitString GetInstance(
            object obj)
        {
            if (obj == null || obj is DerBitString)
            {
                return (DerBitString)obj;
            }
            if (obj is byte[])
            {
                try
                {
                    return (DerBitString)FromByteArray((byte[])obj);
                }
                catch (Exception e)
                {
                    throw new ArgumentException("encoding error in GetInstance: " + e.ToString());
                }
            }

            throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj));
        }

        /**
		 * return a Bit string from a tagged object.
		 *
		 * @param obj the tagged object holding the object we want
		 * @param explicitly true if the object is meant to be explicitly
		 *              tagged false otherwise.
		 * @exception ArgumentException if the tagged object cannot
		 *               be converted.
		 */
        public static DerBitString GetInstance(
            Asn1TaggedObject obj,
            bool isExplicit)

View on GitHub (pinned to 53fb989abc)

Solutions

  1. Confirm the byte[] is a DER BIT STRING (first byte 0x03, then length, then unused-bits count)
  2. If the bytes are raw bit data, wrap directly: new DerBitString(bytes)
  3. Parse generically first (Asn1Object.FromByteArray) and check the concrete type before casting
  4. Read the appended e.ToString() to see the exact underlying failure

Example fix

// before
DerBitString bits = DerBitString.GetInstance(rawBits); // raw, not DER
// after
DerBitString bits = new DerBitString(rawBits); // wrap raw data directly
// or, when unsure of the encoded type:
Asn1Object o = Asn1Object.FromByteArray(bytes);
if (o is DerBitString bs) { /* use bs */ }
Defensive patterns

Strategy: type-guard

Validate before calling

bool IsBitStringEncoding(byte[] data) { return data.Length >= 2 && data[0] == 0x03; }

Type guard

bool IsDerBitString(byte[] data) { try { return Asn1Object.FromByteArray(data) is DerBitString; } catch { return false; } }

Try / catch

try { return DerBitString.GetInstance(bytes); }
catch (ArgumentException ex) { log.Warn("not a BIT STRING encoding: " + ex.Message); return null; }

Prevention

When it happens

Trigger: Calling DerBitString.GetInstance(byte[]) where the bytes encode something other than a BIT STRING (e.g. a SEQUENCE or OCTET STRING), or are not valid ASN.1 at all (the cast to DerBitString also throws InvalidCastException).

Common situations: Extracting signature/keys bits from a structure and passing the wrong embedded object; passing raw bits that were never DER-encoded; parsing a structure whose field order was misread.

Related errors


AI-assisted analysis of peass-ng/PEASS-ng@53fb989abc (2026-09-02). Data as JSON: /api/errors/62f89977112148a4. Report an issue: GitHub.