KidaCoding

Untitled

Nov 13th, 2026
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.43 KB | Source Code | 0 0
  1. using System;
  2.  
  3. namespace DatCryptor
  4. {
  5.     internal static class LastVersionCrypto
  6.     {
  7.         private static readonly uint[] Rc = BuildRc();
  8.         private static readonly uint[] CtxKey = BuildCtxKey();
  9.  
  10.         private static uint[] BuildRc()
  11.         {
  12.             uint[] rc = new uint[12];
  13.             uint v = 0xb1b1u;
  14.             for (int i = 0; i < 12; i++)
  15.             {
  16.                 rc[i] = v;
  17.                 v <<= 1;
  18.                 if ((v & 0x10000u) != 0)
  19.                     v ^= 0x11011u;
  20.             }
  21.             return rc;
  22.         }
  23.  
  24.         private static uint Bswap32(uint x)
  25.         {
  26.             return ((x & 0x000000FFu) << 24)
  27.                  | ((x & 0x0000FF00u) << 8)
  28.                  | ((x & 0x00FF0000u) >> 8)
  29.                  | ((x & 0xFF000000u) >> 24);
  30.         }
  31.  
  32.         private static uint Rol32(uint x, int n)
  33.         {
  34.             return (x << n) | (x >> (32 - n));
  35.         }
  36.  
  37.         private static uint Ror32(uint x, int n)
  38.         {
  39.             return (x >> n) | (x << (32 - n));
  40.         }
  41.  
  42.         private static uint BitPermute(uint x)
  43.         {
  44.             x = ((x & 0xAAAAAAAAu) >> 1) | ((x & 0x55555555u) << 1);
  45.             x = ((x & 0xCCCCCCCCu) >> 2) | ((x & 0x33333333u) << 2);
  46.             x = ((x & 0xF0F0F0F0u) >> 4) | ((x & 0x0F0F0F0Fu) << 4);
  47.             return x;
  48.         }
  49.  
  50.         private static void Theta(ref uint s0, ref uint s1, ref uint s2)
  51.         {
  52.             uint p = s0 ^ s1 ^ s2;
  53.             uint t = Rol32(p, 16) ^ Rol32(p, 8);
  54.             uint v28 = (s0 << 24) ^ (s2 >> 8) ^ (s1 << 8) ^ (s0 >> 24);
  55.             uint v2c = (s1 << 24) ^ (s0 >> 8) ^ (s2 << 8) ^ (s1 >> 24);
  56.             s0 ^= t ^ v28;
  57.             s1 ^= t ^ v2c;
  58.             s2 ^= (v28 >> 16) ^ t ^ (v2c << 16);
  59.         }
  60.  
  61.         private static void InvTheta(ref uint s0, ref uint s1, ref uint s2)
  62.         {
  63.             Theta(ref s0, ref s1, ref s2);
  64.             Theta(ref s0, ref s1, ref s2);
  65.             Theta(ref s0, ref s1, ref s2);
  66.         }
  67.  
  68.         private static void Gamma(ref uint s0, ref uint s1, ref uint s2)
  69.         {
  70.             uint v24 = Rol32(s2, 1);
  71.             uint v20 = Rol32(s0, 22);
  72.             uint ns0 = Rol32((~v24 | s1) ^ v20, 1);
  73.             uint ns2 = Rol32((~s1 | v20) ^ v24, 22);
  74.             uint ns1 = (~v20 | v24) ^ s1;
  75.             s0 = ns0;
  76.             s1 = ns1;
  77.             s2 = ns2;
  78.         }
  79.  
  80.         private static void InvGamma(ref uint s0, ref uint s1, ref uint s2)
  81.         {
  82.             uint b0 = Ror32(s0, 1);
  83.             uint b1 = s1;
  84.             uint b2 = Ror32(s2, 22);
  85.             for (int i = 0; i < 5; i++)
  86.             {
  87.                 uint nb0 = (~b2 | b1) ^ b0;
  88.                 uint nb1 = (~b0 | b2) ^ b1;
  89.                 uint nb2 = (~b1 | b0) ^ b2;
  90.                 b0 = nb0;
  91.                 b1 = nb1;
  92.                 b2 = nb2;
  93.             }
  94.             s0 = Ror32(b0, 22);
  95.             s1 = b1;
  96.             s2 = Ror32(b2, 1);
  97.         }
  98.  
  99.         private static uint[] BuildCtxKey()
  100.         {
  101.             // DA C0 BD DE x3
  102.             uint k0 = 0xDAC0BDDEu;
  103.             uint k1 = 0xDAC0BDDEu;
  104.             uint k2 = 0xDAC0BDDEu;
  105.             Theta(ref k0, ref k1, ref k2);
  106.             uint t0 = Bswap32(BitPermute(k2));
  107.             uint t1 = Bswap32(BitPermute(k1));
  108.             uint t2 = Bswap32(BitPermute(k0));
  109.             return new uint[] { t0, t1, t2 };
  110.         }
  111.  
  112.         private static uint ReadUInt32LE(byte[] buf, int off)
  113.         {
  114.             return (uint)(buf[off]
  115.                  | (buf[off + 1] << 8)
  116.                  | (buf[off + 2] << 16)
  117.                  | (buf[off + 3] << 24));
  118.         }
  119.  
  120.         private static void WriteUInt32LE(byte[] buf, int off, uint v)
  121.         {
  122.             buf[off] = (byte)(v & 0xFFu);
  123.             buf[off + 1] = (byte)((v >> 8) & 0xFFu);
  124.             buf[off + 2] = (byte)((v >> 16) & 0xFFu);
  125.             buf[off + 3] = (byte)((v >> 24) & 0xFFu);
  126.         }
  127.  
  128.         public static byte[] Decrypt(byte[] data)
  129.         {
  130.             if (data == null) return new byte[0];
  131.             byte[] dst = new byte[data.Length];
  132.             uint k0 = CtxKey[0], k1 = CtxKey[1], k2 = CtxKey[2];
  133.             int blocks = data.Length / 12;
  134.  
  135.             for (int b = 0; b < blocks; b++)
  136.             {
  137.                 int off = b * 12;
  138.                 uint s0 = Bswap32(ReadUInt32LE(data, off));
  139.                 uint s1 = Bswap32(ReadUInt32LE(data, off + 4));
  140.                 uint s2 = Bswap32(ReadUInt32LE(data, off + 8));
  141.  
  142.                 uint tmp = BitPermute(s2);
  143.                 s1 = BitPermute(s1);
  144.                 s2 = BitPermute(s0);
  145.                 s0 = tmp;
  146.  
  147.                 for (int r = 0; r < 11; r++)
  148.                 {
  149.                     s0 ^= (Rc[r] << 16) ^ k0;
  150.                     s1 ^= k1;
  151.                     s2 ^= k2 ^ Rc[r];
  152.                     Theta(ref s0, ref s1, ref s2);
  153.                     Gamma(ref s0, ref s1, ref s2);
  154.                 }
  155.  
  156.                 s0 ^= (Rc[11] << 16) ^ k0;
  157.                 s1 ^= k1;
  158.                 s2 ^= k2 ^ Rc[11];
  159.                 Theta(ref s0, ref s1, ref s2);
  160.  
  161.                 tmp = BitPermute(s2);
  162.                 s1 = BitPermute(s1);
  163.                 s2 = BitPermute(s0);
  164.                 s0 = tmp;
  165.  
  166.                 WriteUInt32LE(dst, off, Bswap32(s0));
  167.                 WriteUInt32LE(dst, off + 4, Bswap32(s1));
  168.                 WriteUInt32LE(dst, off + 8, Bswap32(s2));
  169.             }
  170.  
  171.             for (int i = blocks * 12; i < data.Length; i++)
  172.                 dst[i] = (byte)(~(data[i] ^ 0xAB));
  173.  
  174.             return dst;
  175.         }
  176.  
  177.         public static byte[] Encrypt(byte[] data)
  178.         {
  179.             if (data == null) return new byte[0];
  180.             byte[] dst = new byte[data.Length];
  181.             uint k0 = CtxKey[0], k1 = CtxKey[1], k2 = CtxKey[2];
  182.             int blocks = data.Length / 12;
  183.  
  184.             for (int b = 0; b < blocks; b++)
  185.             {
  186.                 int off = b * 12;
  187.                 uint s0 = Bswap32(ReadUInt32LE(data, off));
  188.                 uint s1 = Bswap32(ReadUInt32LE(data, off + 4));
  189.                 uint s2 = Bswap32(ReadUInt32LE(data, off + 8));
  190.  
  191.                 uint tmp = BitPermute(s2);
  192.                 s1 = BitPermute(s1);
  193.                 s2 = BitPermute(s0);
  194.                 s0 = tmp;
  195.  
  196.                 InvTheta(ref s0, ref s1, ref s2);
  197.                 s0 ^= (Rc[11] << 16) ^ k0;
  198.                 s1 ^= k1;
  199.                 s2 ^= k2 ^ Rc[11];
  200.  
  201.                 for (int r = 10; r >= 0; r--)
  202.                 {
  203.                     InvGamma(ref s0, ref s1, ref s2);
  204.                     InvTheta(ref s0, ref s1, ref s2);
  205.                     s0 ^= (Rc[r] << 16) ^ k0;
  206.                     s1 ^= k1;
  207.                     s2 ^= k2 ^ Rc[r];
  208.                 }
  209.  
  210.                 tmp = BitPermute(s2);
  211.                 s1 = BitPermute(s1);
  212.                 s2 = BitPermute(s0);
  213.                 s0 = tmp;
  214.  
  215.                 WriteUInt32LE(dst, off, Bswap32(s0));
  216.                 WriteUInt32LE(dst, off + 4, Bswap32(s1));
  217.                 WriteUInt32LE(dst, off + 8, Bswap32(s2));
  218.             }
  219.  
  220.             for (int i = blocks * 12; i < data.Length; i++)
  221.                 dst[i] = (byte)(((byte)(~data[i])) ^ 0xAB);
  222.  
  223.             return dst;
  224.         }
  225.  
  226.         public static bool LooksEncrypted(byte[] data)
  227.         {
  228.             if (data == null || data.Length == 0) return false;
  229.             byte[] dec = Decrypt(data);
  230.             return CryptoEngine.LooksLikeText(dec);
  231.         }
  232.     }
  233. }
  234.  
Advertisement
Add Comment
Please, Sign In to add comment