You can use the following code which allow you decoding a MIME-encoded string
static Encoding _encoding = Encoding.ASCII;
static string MatchEvaluatorHexToString(System.Text.RegularExpressions.Match m)
{
string str = m.ToString();
Byte[] b = new Byte[1];
b[0] = byte.Parse(m.Groups[1].Value.Replace("=", ""), System.Globalization.NumberStyles.HexNumber);
string str4 = _encoding.GetString(b);
return str.Replace(m.Groups[1].ToString(), str4);
}
static string MatchEvaluatorMIMEEnc2Str(System.Text.RegularExpressions.Match m)
{
_encoding = Encoding.GetEncoding(m.Groups[1].Value);
if (m.Groups[2].Value == "B")
{
Decoder sDecoder = _encoding.GetDecoder();
byte[] todecodeByte = Convert.FromBase64String(m.Groups[3].Value);
int charCount = sDecoder.GetCharCount(todecodeByte, 0, todecodeByte.Length);
char[] decodedChar = new char[charCount];
sDecoder.GetChars(todecodeByte, 0, todecodeByte.Length, decodedChar, 0);
string result = new String(decodedChar);
return (result);
}
System.Text.RegularExpressions.Regex regexItems = new System.Text.RegularExpressions.Regex("(=[0-9a-fA-F]{2})");
string str6 = regexItems.Replace(m.Groups[3].Value.Replace("_", " "), new System.Text.RegularExpressions.MatchEvaluator(MatchEvaluatorHexToString));
return str6;
}
static public string Decode(string str)
{
System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex("=\\?(.+?)\\?(.+?)\\?(.+?)\\?=");
System.Text.RegularExpressions.MatchCollection mc = regEx.Matches(str);
return regEx.Replace(str, new System.Text.RegularExpressions.MatchEvaluator(MatchEvaluatorMIMEEnc2Str));
}
Then call the Decode method to get your decoded subject:
String decodedSubject=Decode("=?utf-8?B?RnJvbSBSZWRpZmY=?=");
Do not forget to include the "=" signs.
Hope it will help ;)
Crusty Applesniffer