Advertisement
spiritovod

Untitled

Jun 23rd, 2022
652
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. typedef unsigned char uint8_t;
  2. typedef unsigned long uint32_t;
  3.  
  4. static const uint8_t Table1[256] = {
  5.     0x20, 0xDB, 0x4B, 0x46, 0xEE, 0x0F, 0xBC, 0xAC, 0x43, 0x32, 0xB1, 0x1E, 0x0B, 0x7C, 0x6E, 0x9A,
  6.     0x06, 0x55, 0xBB, 0xA6, 0xFA, 0x89, 0x41, 0x63, 0x23, 0xA8, 0xCE, 0x01, 0x53, 0xDA, 0x87, 0x78,
  7.     0x2E, 0x81, 0x49, 0x70, 0x28, 0xCF, 0x60, 0x2B, 0x40, 0x9C, 0x5F, 0x1C, 0x44, 0x39, 0x4D, 0xC1,
  8.     0x21, 0x3A, 0xCC, 0xB4, 0x94, 0xAF, 0x5E, 0xAE, 0x31, 0x6F, 0x4C, 0xEC, 0x9D, 0xF8, 0xD4, 0x54,
  9.     0xFE, 0x1A, 0x12, 0xCB, 0xF4, 0x8E, 0x7B, 0xEB, 0xD1, 0x34, 0x83, 0x50, 0x18, 0x64, 0xE5, 0xEA,
  10.     0xB3, 0xFB, 0x6D, 0xDC, 0x0E, 0x66, 0x3D, 0x2D, 0xE4, 0xE1, 0x6A, 0x1B, 0xF6, 0xF0, 0x3C, 0x0D,
  11.     0x22, 0xC2, 0x5C, 0x51, 0xBA, 0x86, 0xF5, 0x48, 0x71, 0xCD, 0xEF, 0x27, 0xE9, 0xD3, 0xDF, 0xA2,
  12.     0xE2, 0xFD, 0x3E, 0x73, 0x90, 0xC0, 0x68, 0x00, 0xFF, 0xB8, 0x7F, 0x82, 0x8A, 0x95, 0xB2, 0x2F,
  13.     0x1D, 0x5A, 0x17, 0x80, 0x74, 0x9E, 0x7D, 0x0A, 0xE3, 0x6C, 0x72, 0x45, 0x24, 0x5B, 0xA9, 0xFC,
  14.     0x84, 0x36, 0x37, 0xA7, 0x8B, 0xDD, 0x16, 0x56, 0xC8, 0xC9, 0x13, 0x42, 0xE6, 0xC4, 0x08, 0xA1,
  15.     0x98, 0x1F, 0x29, 0x26, 0xC6, 0x77, 0x35, 0x79, 0x38, 0xF9, 0xB5, 0x3F, 0xD8, 0x6B, 0x8D, 0x99,
  16.     0xAB, 0x97, 0xF3, 0x69, 0xAD, 0xD2, 0x91, 0xE7, 0x9B, 0xB0, 0xA5, 0x93, 0xA0, 0x76, 0x7E, 0x19,
  17.     0xE0, 0x3B, 0xF2, 0x2C, 0x09, 0xD5, 0xBF, 0xBE, 0xA4, 0x2A, 0x8F, 0x11, 0x4A, 0xD0, 0x58, 0x61,
  18.     0x5D, 0x10, 0xED, 0xCA, 0xB6, 0x57, 0x67, 0xD6, 0xC3, 0xA3, 0xD7, 0xBD, 0x52, 0xAA, 0x7A, 0x47,
  19.     0x33, 0x62, 0xF1, 0xC7, 0x65, 0x04, 0x25, 0x75, 0xE8, 0x15, 0x0C, 0x88, 0x85, 0x96, 0x05, 0x02,
  20.     0x92, 0xF7, 0x03, 0xC5, 0x4E, 0x8C, 0xD9, 0x9F, 0xB9, 0x4F, 0xB7, 0xDE, 0x14, 0x59, 0x07, 0x30 };
  21.  
  22. static const uint32_t Table2[32] = {
  23.     0x0009EDEB, 0xD0456773, 0x7F8B6647, 0x9DC6722F, 0x895B671B, 0x55B8CC43, 0x238970D7, 0x9F477CEF,
  24.     0x4D55284B, 0x8AB83693, 0x81571417, 0xB60BF4DF, 0x7857417B, 0x0E597B33, 0x8025ADC7, 0x90E010CF,
  25.     0x74A8521B, 0xFA6B7F83, 0xA35D27B7, 0x9854F89F, 0x87E21A6B, 0xAFFBDB23, 0x586603B7, 0x7684E99F,
  26.     0xEB25AB2B, 0x2AA80D53, 0xDF395E67, 0x6B6DDFCF, 0x2898375B, 0x81DBACB3, 0xEE228217, 0xAC13E5FF };
  27.  
  28. uint32_t ByteSwap(uint32_t value)
  29. {
  30.     return (value >> 24) | ((value >> 8) & 0xFF00) | ((value << 8) & 0xFF0000) | (value << 24);
  31. }
  32.  
  33.  
  34. void ApexSetupDecrypt(uint8_t* key, uint32_t* rk)
  35. {
  36.     uint32_t* internalKey = (uint32_t*)key;
  37.  
  38.     internalKey[0] = ByteSwap(internalKey[0]) ^ 0x1E3DFA;
  39.     internalKey[1] = ByteSwap(internalKey[1]) ^ 0x78F36777;
  40.     internalKey[2] = ByteSwap(internalKey[2]) ^ 0xD99D2CF;
  41.     internalKey[3] = ByteSwap(internalKey[3]) ^ 0x5E144852;
  42.  
  43.     for (int i = 0; i < 32; i++)
  44.     {
  45.         uint32_t tempInt = Table2[i] ^ internalKey[(i + 1) % 4] ^ internalKey[(i + 2) % 4] ^ internalKey[(i + 3) % 4];
  46.         uint8_t* temp = (uint8_t*)&tempInt;
  47.         uint32_t tablePart01 = Table1[temp[0]] | (uint32_t)(Table1[temp[1]] << 8);
  48.         uint32_t tablePart012 = tablePart01 | (uint32_t)(Table1[temp[2]] << 16);
  49.         uint32_t tableEntry = tablePart012 | (uint32_t)(Table1[temp[3]] << 24);
  50.         uint32_t rkValue = tableEntry ^ internalKey[i % 4] ^
  51.             ((tableEntry >> 19) | tablePart012 << 13) ^
  52.             ((tableEntry >> 9) | tablePart01 << 23);
  53.         internalKey[i % 4] = rkValue;
  54.         rk[i] = rkValue;
  55.     }
  56. }
  57.  
  58. void ApexDecrypt(uint32_t* rk, uint8_t* in, uint8_t* out)
  59. {
  60.     uint32_t* internalData = (uint32_t*)in;
  61.  
  62.     internalData[0] = ByteSwap(internalData[0]);
  63.     internalData[1] = ByteSwap(internalData[1]);
  64.     internalData[2] = ByteSwap(internalData[2]);
  65.     internalData[3] = ByteSwap(internalData[3]);
  66.  
  67.     for (int i = 0; i < 32; i++)
  68.     {
  69.         uint32_t tempInt = rk[31 - i] ^ internalData[(i + 1) % 4] ^ internalData[(i + 2) % 4] ^ internalData[(i + 3) % 4];
  70.         uint8_t* temp = (uint8_t*)&tempInt;
  71.         uint8_t tablePart3 = Table1[temp[3]];
  72.         uint8_t tablePart0 = Table1[temp[0]];
  73.         uint32_t tablePart01 = tablePart0 | (uint32_t)(Table1[temp[1]] << 8);
  74.         uint32_t tablePart012 = tablePart01 | (uint32_t)(Table1[temp[2]] << 16);
  75.         uint32_t tableEntry = tablePart012 | (uint32_t)(tablePart3 << 24);
  76.         internalData[i % 4] ^= tableEntry ^
  77.             ((4 * tableEntry) | (uint32_t)(tablePart3 >> 6)) ^
  78.             ((tableEntry >> 22) | (uint32_t)tablePart012 << 10) ^
  79.             ((tableEntry >> 14) | (uint32_t)tablePart01 << 18) ^
  80.             ((tableEntry >> 8) | (uint32_t)tablePart0 << 24);
  81.     }
  82.     uint32_t* intData = (uint32_t*)out;
  83.     intData[0] = ByteSwap(internalData[3]);
  84.     intData[1] = ByteSwap(internalData[2]);
  85.     intData[2] = ByteSwap(internalData[1]);
  86.     intData[3] = ByteSwap(internalData[0]);
  87. }
  88.  
  89. void decryptAll(uint8_t* key, uint8_t* inBuffer, uint8_t* outBuffer, uint32_t size)
  90. {
  91.     uint32_t rk[32];
  92.     ApexSetupDecrypt(key, rk);
  93.  
  94.     uint32_t paddedLength = 16 * ((size + 15) >> 4);
  95.     uint8_t* tempEncrypted = (uint8_t*)malloc(paddedLength);
  96.     memcpy(tempEncrypted, inBuffer, size);
  97.     uint8_t* tempDecrypted = (uint8_t*)malloc(paddedLength);
  98.     for (uint32_t i = 0; i < paddedLength / 16; i++)
  99.     {
  100.         ApexDecrypt(rk, &tempEncrypted[i * 16], &tempDecrypted[i * 16]);
  101.     }
  102.     memcpy(outBuffer, tempDecrypted, size);
  103.     free(tempDecrypted);
  104.     free(tempEncrypted);
  105. }
Advertisement
RAW Paste Data Copied
Advertisement