Skip to content

Instantly share code, notes, and snippets.

@roughconsensusandrunningcode
Last active May 13, 2024 16:58
Show Gist options
  • Save roughconsensusandrunningcode/7b19ac4e098c0055e965a7baed03252e to your computer and use it in GitHub Desktop.
Save roughconsensusandrunningcode/7b19ac4e098c0055e965a7baed03252e to your computer and use it in GitHub Desktop.
C# - Decode a X-VR-SPAMCAUSE mail header
using System;
public class Program
{
/// <summary>
/// Decode a X-VR-SPAMCAUSE mail header
/// </summary>
/// <param name="encodedSpamcause">The X-VR-SPAMCAUSE content to decode</param>
/// <returns>The decoded spam cause</returns>
public static string Decode(string encodedSpamcause)
{
// based upon the python version by @DoubleYouEl
// https://gist.github.com/DoubleYouEl/e3de97293ce3d5452b3be7a336a06ad7
string result = String.Empty;
const byte x = (byte)'x';
const byte g = (byte)'g';
for (int i = 0; i < encodedSpamcause.Length; i += 2)
{
byte first = (byte)encodedSpamcause[i];
byte second = (byte)encodedSpamcause[i+1];
if((i/2) % 2 == 0)
{
byte tmp = first;
first = second;
second = tmp;
}
var offset = 16 * (g - first);
var ch = first + second - x - offset;
result += (char)ch;
}
return result;
}
/// <summary>
/// Encode a X-VR-SPAMCAUSE mail header
/// </summary>
/// <param name="spamcause">The X-VR-SPAMCAUSE content to encode</param>
/// <returns>The encoded spam cause</returns>
public static string Encode(string spamcause)
{
const byte b = (byte)'b';
const byte x = (byte)'x';
const byte g = (byte)'g';
string result = String.Empty;
for(int i = 0; i < spamcause.Length; ++i)
{
var c = (byte)spamcause[i];
var first = b + c/17;
var offset = 16 * (g - first);
var second = c - first + x + offset;
if(i%2 == 0)
{
var tmp = first;
first = second;
second = tmp;
}
result += (char)first;
result += (char)second;
}
return result;
}
public static void Main()
{
string encodedSpamcause = "gggruggvucftvghtrhho";
string decoded = Decode(encodedSpamcause);
Console.WriteLine(decoded);
string reencoded = Encode(decoded);
if(!reencoded.Equals(encodedSpamcause))
{
Console.WriteLine("Error in encoding!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment