Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decode quoted printable correct

Tags:

c#

.net

I have the following string:

=?utf-8?Q?=5Bproconact_=2D_Verbesserung_=23=32=37=39=5D_=28Neu=29_Stellvertretungen_Benutzerrecht_=2D_andere_k=C3=B6nnen_f=C3=BCr_andere_Stellvertretungen_erstellen_=C3=A4ndern_usw=2E_dadurch_ist_der_Schutz_der_Aktivi=C3=A4ten_Mails_nicht_gew=C3=A4hrt=...

which is an encoding of

[proconact-Verbesserung #279] (Neu) Stellvertretungen Benutzerrecht - andere können für andere Stellvertretungen erstellen ändern usw. dadurch ist der Schutz der Aktiviäten Mails nicht gewährt.

I am searching for a way do decode the quoted string.

I have tried:

private static string DecodeQuotedPrintables(string input, string charSet) {
    Encoding enc = new ASCIIEncoding();
    try {
        enc = Encoding.GetEncoding(charSet);
    } catch {
        enc = new UTF8Encoding();
    }

    var occurences = new Regex(@"(=[0-9A-Z]{2}){1,}", RegexOptions.Multiline);
    var matches = occurences.Matches(input);

    foreach (Match match in matches) {
        try {
            byte[] b = new byte[match.Groups[0].Value.Length / 3];
            for (int i = 0; i < match.Groups[0].Value.Length / 3; i++) {
                b[i] = byte.Parse(match.Groups[0].Value.Substring(i * 3 + 1, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
            }
            char[] hexChar = enc.GetChars(b);
            input = input.Replace(match.Groups[0].Value, hexChar[0].ToString());
        } catch { ;}
    }
    input = input.Replace("?=", "").Replace("=\r\n", "");

    return input;
}

when I call (where s is my string)

var x = DecodeQuotedPrintables(s, "utf-8");

this will return

=?utf-8?Q?[proconact_-_Verbesserung_#_(Neu)_Stellvertretungen_Benutzerrecht_-_andere_können_für_andere_Stellvertretungen_erstellen_ändern_usw._dadurch_ist_der_Schutz_der_Aktiviäten_Mails_nicht_gewährt=...

What can I do, that there will also the _ and the starting =?utf-8?Q? and the trailing =.. be removed?

like image 404
BennoDual Avatar asked Dec 20 '22 22:12

BennoDual


1 Answers

The text you’re trying to decode is typically found in MIME headers, and is encoded according to the specification defined in the following Internet standard: RFC 2047: MIME (Multipurpose Internet Mail Extensions) Part Three: Message Header Extensions for Non-ASCII Text.

There is a sample implementation for such a decoder on GitHub; maybe you can draw some ideas from it: RFC2047 decoder in C#.

You can also use this online tool for comparing your results: Online MIME Headers Decoder.

Note that your sample text is incorrect. The specification declares:

encoded-word = "=?" charset "?" encoding "?" encoded-text "?="

Per the specification, any encoded word must end in ?=. Thus, your sample must be corrected from:

=?utf-8?Q?=5Bproconact_=2D_Verbesserung_=23=32=37=39=5D_=28Neu=29_Stellvertretungen_Benutzerrecht_=2D_andere_k=C3=B6nnen_f=C3=BCr_andere_Stellvertretungen_erstellen_=C3=A4ndern_usw=2E_dadurch_ist_der_Schutz_der_Aktivi=C3=A4ten_Mails_nicht_gew=C3=A4hrt=

…to (scroll to the far right):

=?utf-8?Q?=5Bproconact_=2D_Verbesserung_=23=32=37=39=5D_=28Neu=29_Stellvertretungen_Benutzerrecht_=2D_andere_k=C3=B6nnen_f=C3=BCr_andere_Stellvertretungen_erstellen_=C3=A4ndern_usw=2E_dadurch_ist_der_Schutz_der_Aktivi=C3=A4ten_Mails_nicht_gew=C3=A4hrt?=

Strictly speaking, your sample is also invalid because it exceeds the 75-character limit imposed on any encoded word; however, most decoders tend to be tolerant of this non-conformity.

like image 149
Douglas Avatar answered Mar 03 '23 07:03

Douglas