Advertisement
Guest User

Untitled

a guest
Jun 29th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 0 0
  1. #pragma once
  2.  
  3. /*
  4. //-------------------------------------------------------------//
  5. // "Malware related compile-time hacks with C++11" by LeFF //
  6. // You can use this code however you like, I just don't really //
  7. // give a shit, but if you feel some respect for me, please //
  8. // don't cut off this comment when copy-pasting... ;-) //
  9. //-------------------------------------------------------------//
  10.  
  11. ////////////////////////////////////////////////////////////////////
  12. template <int X> struct EnsureCompileTime {
  13. enum : int {
  14. Value = X
  15. };
  16. };
  17. ////////////////////////////////////////////////////////////////////
  18.  
  19.  
  20. ////////////////////////////////////////////////////////////////////
  21. //Use Compile-Time as seed
  22. #define Seed ((__TIME__[7] - '0') * 1 + (__TIME__[6] - '0') * 10 + \
  23. (__TIME__[4] - '0') * 60 + (__TIME__[3] - '0') * 600 + \
  24. (__TIME__[1] - '0') * 3600 + (__TIME__[0] - '0') * 36000)
  25. ////////////////////////////////////////////////////////////////////
  26.  
  27.  
  28. ////////////////////////////////////////////////////////////////////
  29. constexpr int LinearCongruentGenerator(int Rounds) {
  30. return 1013904223 + 1664525 * ((Rounds> 0) ? LinearCongruentGenerator(Rounds - 1) : Seed & 0xFFFFFFFF);
  31. }
  32. #define Random() EnsureCompileTime<LinearCongruentGenerator(10)>::Value //10 Rounds
  33. #define RandomNumber(Min, Max) (Min + (Random() % (Max - Min + 1)))
  34. ////////////////////////////////////////////////////////////////////
  35.  
  36.  
  37. ////////////////////////////////////////////////////////////////////
  38. template <int... Pack> struct IndexList {};
  39. ////////////////////////////////////////////////////////////////////
  40.  
  41.  
  42. ////////////////////////////////////////////////////////////////////
  43. template <typename IndexList, int Right> struct Append;
  44. template <int... Left, int Right> struct Append<IndexList<Left...>, Right> {
  45. typedef IndexList<Left..., Right> Result;
  46. };
  47. ////////////////////////////////////////////////////////////////////
  48.  
  49.  
  50. ////////////////////////////////////////////////////////////////////
  51. template <int N> struct ConstructIndexList {
  52. typedef typename Append<typename ConstructIndexList<N - 1>::Result, N - 1>::Result Result;
  53. };
  54. template <> struct ConstructIndexList<0> {
  55. typedef IndexList<> Result;
  56. };
  57. ////////////////////////////////////////////////////////////////////
  58.  
  59.  
  60. ////////////////////////////////////////////////////////////////////
  61. const char XORKEY = static_cast<char>(RandomNumber(0, 0xFF));
  62. constexpr char EncryptCharacter(const char Character, int Index) {
  63. return Character ^ (XORKEY + Index);
  64. }
  65.  
  66. template <typename IndexList> class CXorString;
  67. template <int... Index> class CXorString<IndexList<Index...> > {
  68. private:
  69. char Value[sizeof...(Index) + 1];
  70. bool Decrypted;
  71. public:
  72. #pragma optimize("", off)
  73. __forceinline
  74. constexpr CXorString(const char* const String)
  75. : Value{ EncryptCharacter(String[Index], Index)... }
  76. , Decrypted (false) {}
  77. #pragma optimize("", on)
  78.  
  79. __forceinline
  80. char* decrypt() {
  81. if (Decrypted) {
  82. return Value;
  83. }
  84. for(int t = 0; t < sizeof...(Index); t++) {
  85. Value[t] = Value[t] ^ (XORKEY + t);
  86. }
  87. Value[sizeof...(Index)] = '\0';
  88. Decrypted = true;
  89. return Value;
  90. }
  91.  
  92. char* get() {
  93. return Value;
  94. }
  95. };
  96. #define XorS(X, String) CXorString<ConstructIndexList<sizeof(String)-1>::Result> X(String)
  97.  
  98. #ifndef _DEBUG
  99. #define XS(String) (CXorString<ConstructIndexList<sizeof(String)-1>::Result>(String).decrypt())
  100. #else
  101. #define XS(String) (String)
  102. #endif
  103. ////////////////////////////////////////////////////////////////////
  104.  
  105. */
  106.  
  107. #include <string>
  108. #include <array>
  109. #include <cstdarg>
  110.  
  111. constexpr auto time = __TIME__;
  112. constexpr auto seed = static_cast< int >( time[ 7 ] ) + static_cast< int >( time[ 6 ] ) * 10 + static_cast< int >( time[ 4 ] ) * 60 + static_cast< int >( time[ 3 ] ) * 600 + static_cast< int >( time[ 1 ] ) * 3600 + static_cast< int >( time[ 0 ] ) * 36000;
  113.  
  114. // 1988, Stephen Park and Keith Miller
  115. // "Random Number Generators: Good Ones Are Hard To Find", considered as "minimal standard"
  116. // Park-Miller 31 bit pseudo-random number generator, implemented with G. Carta's optimisation:
  117. // with 32-bit math and without division
  118.  
  119. template < int N >
  120. struct RandomGenerator
  121. {
  122. private:
  123. static constexpr unsigned a = 16807; // 7^5
  124. static constexpr unsigned m = 2147483647; // 2^31 - 1
  125.  
  126. static constexpr unsigned s = RandomGenerator< N - 1 >::value;
  127. static constexpr unsigned lo = a * ( s & 0xFFFF ); // Multiply lower 16 bits by 16807
  128. static constexpr unsigned hi = a * ( s >> 16 ); // Multiply higher 16 bits by 16807
  129. static constexpr unsigned lo2 = lo + ( ( hi & 0x7FFF ) << 16 ); // Combine lower 15 bits of hi with lo's upper bits
  130. static constexpr unsigned hi2 = hi >> 15; // Discard lower 15 bits of hi
  131. static constexpr unsigned lo3 = lo2 + hi;
  132.  
  133. public:
  134. static constexpr unsigned max = m;
  135. static constexpr unsigned value = lo3 > m ? lo3 - m : lo3;
  136. };
  137.  
  138. template <>
  139. struct RandomGenerator< 0 >
  140. {
  141. static constexpr unsigned value = seed;
  142. };
  143.  
  144. template < int N, int M >
  145. struct RandomInt
  146. {
  147. static constexpr auto value = RandomGenerator< N + 1 >::value % M;
  148. };
  149.  
  150. template < int N >
  151. struct RandomChar
  152. {
  153. static const char value = static_cast< char >( 1 + RandomInt< N, 0x7F - 1 >::value );
  154. };
  155.  
  156. template < size_t N, int K >
  157. struct XorString
  158. {
  159. private:
  160. const char _key;
  161. std::array< char, N + 1 > _encrypted;
  162.  
  163. constexpr char enc( char c ) const
  164. {
  165. return c ^ _key;
  166. }
  167.  
  168. char dec( char c ) const
  169. {
  170. return c ^ _key;
  171. }
  172.  
  173. public:
  174. template < size_t... Is >
  175. constexpr __forceinline XorString( const char* str, std::index_sequence< Is... > ) : _key( RandomChar< K >::value ), _encrypted{ enc( str[ Is ] )... }
  176. {
  177. }
  178.  
  179. __forceinline decltype( auto ) decrypt( void )
  180. {
  181. for( size_t i = 0; i < N; ++i ) {
  182. _encrypted[ i ] = dec( _encrypted[ i ] );
  183. }
  184. _encrypted[ N ] = '\0';
  185. return _encrypted.data();
  186. }
  187. };
  188.  
  189. #ifdef _DEBUG
  190. #define XS(s) (s)
  191. #else
  192. #define XS(s) (XorString<sizeof(s)-1,__COUNTER__>(s,std::make_index_sequence<sizeof(s)-1>()).decrypt())
  193. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement