Skip to content

Instantly share code, notes, and snippets.

@HakanL
Created January 10, 2025 19:45
Show Gist options
  • Save HakanL/f67fb9452d086856f105d64bc13a3f46 to your computer and use it in GitHub Desktop.
Save HakanL/f67fb9452d086856f105d64bc13a3f46 to your computer and use it in GitHub Desktop.
ARACrypt C# implementation
public class ARACrypt
{
private uint lfsrA = 0x13579BDF;
private uint lfsrB = 0x2468ACE0;
private uint lfsrC = 0xFDB97531;
private uint maskA = 0x80000062;
private uint maskB = 0x40000020;
private uint maskC = 0x10000002;
private uint rot0A = 0x7FFFFFFF;
private uint rot0B = 0x3FFFFFFF;
private uint rot0C = 0x0FFFFFFF;
private uint rot1A = 0x80000000;
private uint rot1B = 0xC0000000;
private uint rot1C = 0xF0000000;
private string key = "";
public void SetKey(string key)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException(nameof(key));
this.key = key;
string csSeed = this.key;
int nIdx = 0;
while (csSeed.Length < 12)
{
csSeed += csSeed[nIdx++];
}
for (nIdx = 0; nIdx < 4; nIdx++)
{
this.lfsrA = ((this.lfsrA <<= 8) | csSeed[nIdx + 0]);
this.lfsrB = ((this.lfsrB <<= 8) | csSeed[nIdx + 4]);
this.lfsrC = ((this.lfsrC <<= 8) | csSeed[nIdx + 8]);
}
if (this.lfsrA == 0x00000000)
this.lfsrA = 0x13579BDF;
if (this.lfsrB == 0x00000000)
this.lfsrB = 0x2468ACE0;
if (this.lfsrC == 0x00000000)
this.lfsrC = 0xFDB97531;
}
public byte TransformChar(byte input)
{
byte crypto = 0;
uint outB = (this.lfsrB & 0x00000001);
uint outC = (this.lfsrC & 0x00000001);
for (int i = 0; i < 8; i++)
{
if ((this.lfsrA & 0x00000001) != 0)
{
this.lfsrA = (((this.lfsrA ^ this.maskA) >> 1) | this.rot1A);
if ((this.lfsrB & 0x00000001) != 0)
{
this.lfsrB = (((this.lfsrB ^ this.maskB) >> 1) | this.rot1B);
outB = 0x00000001;
}
else
{
this.lfsrB = ((this.lfsrB >> 1) & this.rot0B);
outB = 0x00000000;
}
}
else
{
this.lfsrA = ((this.lfsrA >> 1) & this.rot0A);
if ((this.lfsrC & 0x00000001) != 0)
{
this.lfsrC = (((this.lfsrC ^ this.maskC) >> 1) | this.rot1C);
outC = 0x00000001;
}
else
{
this.lfsrC = ((this.lfsrC >> 1) & this.rot0C);
outC = 0x00000000;
}
}
crypto = (byte)((byte)(crypto << 1) | (outB ^ outC));
}
input = (byte)(input ^ crypto);
if (input == 0)
input = (byte)(input ^ crypto);
return input;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment