{"record":{"id":"27a306610bc83faa","repo":"peass-ng/PEASS-ng","slug":"invalid-characters-encountered-in-hex-data","errorCode":null,"errorMessage":"invalid characters encountered in Hex data","messagePattern":"invalid characters encountered in Hex data","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"winPEAS/winPEASexe/winPEAS/3rdParty/BouncyCastle/util/encoders/HexEncoder.cs","lineNumber":130,"sourceCode":"            int i = off;\n            while (i < end)\n            {\n                while (i < end && Ignore((char)data[i]))\n                {\n                    i++;\n                }\n\n                b1 = decodingTable[data[i++]];\n\n                while (i < end && Ignore((char)data[i]))\n                {\n                    i++;\n                }\n\n                b2 = decodingTable[data[i++]];\n\n                if ((b1 | b2) >= 0x80)\n                    throw new IOException(\"invalid characters encountered in Hex data\");\n\n                buf[bufOff++] = (byte)((b1 << 4) | b2);\n\n                if (bufOff == buf.Length)\n                {\n                    outStream.Write(buf, 0, bufOff);\n                    bufOff = 0;\n                }\n\n                outLen++;\n            }\n\n            if (bufOff > 0)\n            {\n                outStream.Write(buf, 0, bufOff);\n            }\n\n            return outLen;","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/peass-ng/PEASS-ng/blob/53fb989abc2219826385683a6fee826bd6cd38d6/winPEAS/winPEASexe/winPEAS/3rdParty/BouncyCastle/util/encoders/HexEncoder.cs#L112-L148","documentation":"HexEncoder.Decode decodes hex-encoded bytes from an input stream into an output stream. Each decoded pair of characters is looked up in the decoder's table; if either nibble value has its high bit set (>= 0x80), the character was not a valid hex digit, so an IOException is thrown. This prevents silently producing garbage bytes from invalid input.","triggerScenarios":"Calling HexEncoder.Decode (or Hex.Decode) on a stream/string containing characters outside 0-9, a-f, A-F (e.g. whitespace mid-pair, 'g'-'z', punctuation, UTF-8 BOM, or bytes >0x7F feeding the decoding table).","commonSituations":"Decoding base64 or other non-hex strings by mistake; copying hex with spaces/newlines/quotes; data corrupted in transit; decoding output of a different encoder (e.g. MD5 digest printed with separators).","solutions":["Sanitize the input: strip whitespace, quotes, 0x prefixes, and other non-hex characters before decoding.","Verify the data really is hex (regex ^[0-9a-fA-F]+$ with even length) before calling Decode.","If input may contain invalid characters, use prevalidation or a lenient pre-clean step rather than relying on the exception.","Check the source of the data — if it was hex-encoded with this library's HexEncoder, re-encode instead of decoding a corrupted copy."],"exampleFix":"// before\nHex.Decode(input); // 'a b1...' throws IOException\n// after\nvar cleaned = new string(input.Where(char.IsAsciiHexDigit).ToArray());\nif (cleaned.Length % 2 != 0) throw new ArgumentException(\"not hex\");\nHex.Decode(cleaned);","handlingStrategy":"validation","validationCode":"static bool IsHexString(string s) => s != null && s.Length % 2 == 0 && System.Text.RegularExpressions.Regex.IsMatch(s, \"^[0-9a-fA-F]+$\");","typeGuard":"static bool IsValidHexPair(char a, char b) => Uri.IsHexDigit(a) && Uri.IsHexDigit(b);","tryCatchPattern":"try { Hex.Decode(input); } catch (IOException ex) when (ex.Message.Contains(\"invalid characters\")) { throw new FormatException(\"Input is not valid hex\", ex); }","preventionTips":["Trim/strip whitespace and 0x prefixes before decoding","Validate with a hex regex before any decode call","Confirm the encoding is hex (not base64) at the data boundary"],"tags":["csharp","hex","encoding","io"],"backgroundTag":"invalid-hex-character","analyzedSha":"53fb989abc2219826385683a6fee826bd6cd38d6","analyzedAt":"2026-09-02T04:25:09.259Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T11:17:12.671Z"}