Skip to content

Instantly share code, notes, and snippets.

@Biswa96
Created July 7, 2018 06:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Biswa96/20cc5957d2aec300e83cb045790c3ad6 to your computer and use it in GitHub Desktop.
Save Biswa96/20cc5957d2aec300e83cb045790c3ad6 to your computer and use it in GitHub Desktop.
Encoding/decoding algorithm from Metalogix Archive Manager (MAM)
using System;
namespace Cryptography
{
public class EncodeUtils
{
public static string Encode6bit(string txt)
{
short num = (short)txt.Length;
string text = txt;
short num2 = num % 3;
if (num2 == 0)
{
num2 = 3;
}
for (int i = 0; i < (int)(3 - num2); i++)
{
text += " ";
}
string text2 = num.ToString("X").PadLeft(4, '0');
byte[] array = new byte[8];
for (int j = 0; j < (int)num; j += 3)
{
ushort num3 = (ushort)text[j];
ushort num4 = (ushort)text[j + 1];
ushort num5 = (ushort)text[j + 2];
array[0] = (byte)((num3 & 64512) >> 10);
array[1] = (byte)((num3 & 1008) >> 4);
array[2] = (byte)((int)(num3 & 15) << 2 | (num4 & 49152) >> 14);
array[3] = (byte)((num4 & 16128) >> 8);
array[4] = (byte)((num4 & 252) >> 2);
array[5] = (byte)((int)(num4 & 3) << 4 | (num5 & 61440) >> 12);
array[6] = (byte)((num5 & 4032) >> 6);
array[7] = (byte)(num5 & 63);
for (int k = 0; k < 8; k++)
{
byte b = array[k];
char c;
if (b < 10)
{
c = (char)(48 + b);
}
else if (b < 36)
{
c = (char)(97 + b - 10);
}
else if (b < 62)
{
c = (char)(65 + b - 36);
}
else if (b == 63)
{
c = '<';
}
else
{
c = '>';
}
text2 += c;
}
}
return text2;
}
public static string Decode6bit(string txt)
{
string value = txt.Substring(0, 4);
ushort num = Convert.ToUInt16(value, 16);
int length = txt.Length;
string text = "";
ushort[] array = new ushort[8];
for (int i = 4; i < length; i += 8)
{
for (int j = 0; j < 8; j++)
{
char c = txt[i + j];
ushort num2;
if (c == '>')
{
num2 = 63;
}
else if (c == '<')
{
num2 = 62;
}
else if (c >= 'a')
{
num2 = (ushort)((byte)c - 97 + 10);
}
else if (c >= 'A')
{
num2 = (ushort)((byte)c - 65 + 36);
}
else
{
num2 = (ushort)((byte)c - 48);
}
array[j] = num2;
}
ushort num3 = (ushort)((int)array[0] << 10 | (int)array[1] << 4 | (array[2] & 60) >> 2);
ushort num4 = (ushort)((int)(array[2] & 3) << 14 | (int)array[3] << 8 | (int)array[4] << 2 | (array[5] & 48) >> 4);
ushort num5 = (ushort)((int)(array[5] & 15) << 12 | (int)array[6] << 6 | (int)array[7]);
text += (char)num3;
text += (char)num4;
text += (char)num5;
}
if (text.Length > (int)num)
{
text = text.Substring(0, (int)num);
}
return text;
}
public static string EncodeXOR(string original, string key)
{
string text = "";
for (int i = 0; i < original.Length; i++)
{
char value = original[i];
int num = (int)Convert.ToByte(value);
int num2 = (int)key[i % key.Length];
text += (num ^ num2).ToString("X2");
}
return text;
}
public static string EncodeXORO365(string original)
{
return EncodeUtils.EncodeXOR(original, "<O365!!>");
}
public static string DecodeXOR(string encoded, string key)
{
string text = "";
for (int i = 0; i < encoded.Length; i += 2)
{
int num = (int)Convert.ToByte(encoded.Substring(i, 2), 16);
int num2 = (int)key[i / 2 % key.Length];
int num3 = num ^ num2;
text += (char)num3;
}
return text;
}
public static string DecodeXORO365(string original)
{
return EncodeUtils.DecodeXOR(original, "<O365!!>");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment