Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace DatCryptor
- {
- internal static class LastVersionCrypto
- {
- private static readonly uint[] Rc = BuildRc();
- private static readonly uint[] CtxKey = BuildCtxKey();
- private static uint[] BuildRc()
- {
- uint[] rc = new uint[12];
- uint v = 0xb1b1u;
- for (int i = 0; i < 12; i++)
- {
- rc[i] = v;
- v <<= 1;
- if ((v & 0x10000u) != 0)
- v ^= 0x11011u;
- }
- return rc;
- }
- private static uint Bswap32(uint x)
- {
- return ((x & 0x000000FFu) << 24)
- | ((x & 0x0000FF00u) << 8)
- | ((x & 0x00FF0000u) >> 8)
- | ((x & 0xFF000000u) >> 24);
- }
- private static uint Rol32(uint x, int n)
- {
- return (x << n) | (x >> (32 - n));
- }
- private static uint Ror32(uint x, int n)
- {
- return (x >> n) | (x << (32 - n));
- }
- private static uint BitPermute(uint x)
- {
- x = ((x & 0xAAAAAAAAu) >> 1) | ((x & 0x55555555u) << 1);
- x = ((x & 0xCCCCCCCCu) >> 2) | ((x & 0x33333333u) << 2);
- x = ((x & 0xF0F0F0F0u) >> 4) | ((x & 0x0F0F0F0Fu) << 4);
- return x;
- }
- private static void Theta(ref uint s0, ref uint s1, ref uint s2)
- {
- uint p = s0 ^ s1 ^ s2;
- uint t = Rol32(p, 16) ^ Rol32(p, 8);
- uint v28 = (s0 << 24) ^ (s2 >> 8) ^ (s1 << 8) ^ (s0 >> 24);
- uint v2c = (s1 << 24) ^ (s0 >> 8) ^ (s2 << 8) ^ (s1 >> 24);
- s0 ^= t ^ v28;
- s1 ^= t ^ v2c;
- s2 ^= (v28 >> 16) ^ t ^ (v2c << 16);
- }
- private static void InvTheta(ref uint s0, ref uint s1, ref uint s2)
- {
- Theta(ref s0, ref s1, ref s2);
- Theta(ref s0, ref s1, ref s2);
- Theta(ref s0, ref s1, ref s2);
- }
- private static void Gamma(ref uint s0, ref uint s1, ref uint s2)
- {
- uint v24 = Rol32(s2, 1);
- uint v20 = Rol32(s0, 22);
- uint ns0 = Rol32((~v24 | s1) ^ v20, 1);
- uint ns2 = Rol32((~s1 | v20) ^ v24, 22);
- uint ns1 = (~v20 | v24) ^ s1;
- s0 = ns0;
- s1 = ns1;
- s2 = ns2;
- }
- private static void InvGamma(ref uint s0, ref uint s1, ref uint s2)
- {
- uint b0 = Ror32(s0, 1);
- uint b1 = s1;
- uint b2 = Ror32(s2, 22);
- for (int i = 0; i < 5; i++)
- {
- uint nb0 = (~b2 | b1) ^ b0;
- uint nb1 = (~b0 | b2) ^ b1;
- uint nb2 = (~b1 | b0) ^ b2;
- b0 = nb0;
- b1 = nb1;
- b2 = nb2;
- }
- s0 = Ror32(b0, 22);
- s1 = b1;
- s2 = Ror32(b2, 1);
- }
- private static uint[] BuildCtxKey()
- {
- // DA C0 BD DE x3
- uint k0 = 0xDAC0BDDEu;
- uint k1 = 0xDAC0BDDEu;
- uint k2 = 0xDAC0BDDEu;
- Theta(ref k0, ref k1, ref k2);
- uint t0 = Bswap32(BitPermute(k2));
- uint t1 = Bswap32(BitPermute(k1));
- uint t2 = Bswap32(BitPermute(k0));
- return new uint[] { t0, t1, t2 };
- }
- private static uint ReadUInt32LE(byte[] buf, int off)
- {
- return (uint)(buf[off]
- | (buf[off + 1] << 8)
- | (buf[off + 2] << 16)
- | (buf[off + 3] << 24));
- }
- private static void WriteUInt32LE(byte[] buf, int off, uint v)
- {
- buf[off] = (byte)(v & 0xFFu);
- buf[off + 1] = (byte)((v >> 8) & 0xFFu);
- buf[off + 2] = (byte)((v >> 16) & 0xFFu);
- buf[off + 3] = (byte)((v >> 24) & 0xFFu);
- }
- public static byte[] Decrypt(byte[] data)
- {
- if (data == null) return new byte[0];
- byte[] dst = new byte[data.Length];
- uint k0 = CtxKey[0], k1 = CtxKey[1], k2 = CtxKey[2];
- int blocks = data.Length / 12;
- for (int b = 0; b < blocks; b++)
- {
- int off = b * 12;
- uint s0 = Bswap32(ReadUInt32LE(data, off));
- uint s1 = Bswap32(ReadUInt32LE(data, off + 4));
- uint s2 = Bswap32(ReadUInt32LE(data, off + 8));
- uint tmp = BitPermute(s2);
- s1 = BitPermute(s1);
- s2 = BitPermute(s0);
- s0 = tmp;
- for (int r = 0; r < 11; r++)
- {
- s0 ^= (Rc[r] << 16) ^ k0;
- s1 ^= k1;
- s2 ^= k2 ^ Rc[r];
- Theta(ref s0, ref s1, ref s2);
- Gamma(ref s0, ref s1, ref s2);
- }
- s0 ^= (Rc[11] << 16) ^ k0;
- s1 ^= k1;
- s2 ^= k2 ^ Rc[11];
- Theta(ref s0, ref s1, ref s2);
- tmp = BitPermute(s2);
- s1 = BitPermute(s1);
- s2 = BitPermute(s0);
- s0 = tmp;
- WriteUInt32LE(dst, off, Bswap32(s0));
- WriteUInt32LE(dst, off + 4, Bswap32(s1));
- WriteUInt32LE(dst, off + 8, Bswap32(s2));
- }
- for (int i = blocks * 12; i < data.Length; i++)
- dst[i] = (byte)(~(data[i] ^ 0xAB));
- return dst;
- }
- public static byte[] Encrypt(byte[] data)
- {
- if (data == null) return new byte[0];
- byte[] dst = new byte[data.Length];
- uint k0 = CtxKey[0], k1 = CtxKey[1], k2 = CtxKey[2];
- int blocks = data.Length / 12;
- for (int b = 0; b < blocks; b++)
- {
- int off = b * 12;
- uint s0 = Bswap32(ReadUInt32LE(data, off));
- uint s1 = Bswap32(ReadUInt32LE(data, off + 4));
- uint s2 = Bswap32(ReadUInt32LE(data, off + 8));
- uint tmp = BitPermute(s2);
- s1 = BitPermute(s1);
- s2 = BitPermute(s0);
- s0 = tmp;
- InvTheta(ref s0, ref s1, ref s2);
- s0 ^= (Rc[11] << 16) ^ k0;
- s1 ^= k1;
- s2 ^= k2 ^ Rc[11];
- for (int r = 10; r >= 0; r--)
- {
- InvGamma(ref s0, ref s1, ref s2);
- InvTheta(ref s0, ref s1, ref s2);
- s0 ^= (Rc[r] << 16) ^ k0;
- s1 ^= k1;
- s2 ^= k2 ^ Rc[r];
- }
- tmp = BitPermute(s2);
- s1 = BitPermute(s1);
- s2 = BitPermute(s0);
- s0 = tmp;
- WriteUInt32LE(dst, off, Bswap32(s0));
- WriteUInt32LE(dst, off + 4, Bswap32(s1));
- WriteUInt32LE(dst, off + 8, Bswap32(s2));
- }
- for (int i = blocks * 12; i < data.Length; i++)
- dst[i] = (byte)(((byte)(~data[i])) ^ 0xAB);
- return dst;
- }
- public static bool LooksEncrypted(byte[] data)
- {
- if (data == null || data.Length == 0) return false;
- byte[] dec = Decrypt(data);
- return CryptoEngine.LooksLikeText(dec);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment