Guest User

Untitled

a guest
May 22nd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.16 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////////////////////////////////
  2. // ·þÎñ¶ËSOCKETÓüÓÃÜÄ£°å
  3. ////////////////////////////////////////////////////////////////////////////////////////////////////
  4. // Ïɽ£ÐÞ, 2001.10.26
  5. // ÐÞ¸ÄΪģ°å£º2001.12.11
  6.  
  7. #ifndef ENCRYPTSERVER_HEAD
  8. #define ENCRYPTSERVER_HEAD
  9.  
  10. ////////////////////////////////////////////////////////////////////////////////////////////////////
  11. // 3¸öËæ»úÊý, ÓÃÓÚÒþ²Ø·þÎñ¶ËINIÖеÄSERVER KEY¡£²»±ä
  12. #define     aa  0x7E
  13. #define     bb  0x33
  14. #define     cc  0xA1
  15.  
  16. ////////////////////////////////////////////////////////////////////////////////////////////////////
  17. template <unsigned long key1, unsigned long key2>
  18. class   CEncryptServer
  19. {
  20. public:
  21.     CEncryptServer(){ Init(); }
  22. //default   CEncryptServer(CEncryptServer & cEncrypt);
  23. public:
  24.     void Init() { m_nPos1 = m_nPos2 = 0; m_cGlobalEncrypt.Init(); }
  25.  
  26.     void Encrypt(unsigned char * bufMsg, int nLen, bool bMove = true);
  27.  
  28.     void ChangeCode(DWORD dwData);
  29.  
  30. protected:
  31.     int     m_nPos1;
  32.     int     m_nPos2;
  33. protected:
  34.     class   CEncryptCode
  35.     {
  36.     public:
  37.         CEncryptCode() { Init(); }
  38.         void Init()
  39.         {
  40.             try{
  41.                 // Éú³É ABC
  42.                 int a1, b1, c1, fst1;
  43.                 a1      = ((key1 >> 0) & 0xFF) ^ aa;
  44.                 b1      = ((key1 >> 8) & 0xFF) ^ bb;
  45.                 c1      = ((key1 >> 24) & 0xFF) ^ cc;
  46.                 fst1    = (key1 >> 16) & 0xFF;
  47.  
  48.                 int a2, b2, c2, fst2;
  49.                 a2      = ((key2 >> 0) & 0xFF) ^ aa;
  50.                 b2      = ((key2 >> 8) & 0xFF) ^ bb;
  51.                 c2      = ((key2 >> 24) & 0xFF) ^ cc;
  52.                 fst2    = (key2 >> 16) & 0xFF;
  53.  
  54.                 unsigned char   nCode = fst1;
  55.                 for(int i = 0; i < 256; i++)
  56.                 {
  57.                     m_bufEncrypt1[i] = nCode;
  58.                     nCode = (a1*nCode*nCode + b1*nCode + c1) % 256;
  59.                 }
  60.  
  61.                 assert(nCode == fst1);
  62.  
  63.                     nCode = fst2;
  64.                 for( i = 0; i < 256; i++)
  65.                 {
  66.                     m_bufEncrypt2[i] = nCode;
  67.                     nCode = (a2*nCode*nCode + b2*nCode + c2) % 256;
  68.                 }
  69.                 assert(nCode == fst2);
  70.             }catch(...){ /*LOGCATCH("Encrypt init fail.");*/ }
  71.         }
  72.         unsigned char m_bufEncrypt1[256];
  73.         unsigned char m_bufEncrypt2[256];
  74.     }m_cGlobalEncrypt;
  75. };
  76.  
  77.  
  78. template <unsigned long key1, unsigned long key2>
  79. inline void CEncryptServer<key1, key2>::Encrypt(unsigned char * bufMsg, int nLen, bool bMove)
  80. {
  81.     try{
  82.         int     nOldPos1 = m_nPos1;
  83.         int     nOldPos2 = m_nPos2;
  84.         for(int i = 0; i < nLen; i++)
  85.         {
  86.             bufMsg[i] ^= m_cGlobalEncrypt.m_bufEncrypt1[m_nPos1];
  87.             bufMsg[i] ^= m_cGlobalEncrypt.m_bufEncrypt2[m_nPos2];
  88.             if(++m_nPos1 >= 256)
  89.             {
  90.                 m_nPos1 = 0;
  91.                 if(++m_nPos2 >= 256)
  92.                     m_nPos2 = 0;
  93.             }
  94.             assert(m_nPos1 >=0 && m_nPos1 < 256);
  95.             assert(m_nPos2 >=0 && m_nPos2 < 256);
  96.         }
  97.  
  98.         if(!bMove)
  99.         {
  100.             // »Ö¸´Ö¸Õë
  101.             m_nPos1 = nOldPos1;
  102.             m_nPos2 = nOldPos2;
  103.         }
  104.     }catch(...){ /*LOGCATCH("Encrypt fail.");*/ }
  105. }
  106.  
  107. template <unsigned long key1, unsigned long key2>
  108. inline void CEncryptServer<key1, key2>::ChangeCode(DWORD dwData)
  109. {
  110.     try{
  111.         DWORD   dwData2 = dwData*dwData;
  112.         for(int i = 0; i < 256; i += 4)
  113.         {
  114.             *(DWORD*)(&m_cGlobalEncrypt.m_bufEncrypt1[i]) ^= dwData;
  115.             *(DWORD*)(&m_cGlobalEncrypt.m_bufEncrypt2[i]) ^= dwData2;
  116.         }
  117.     }catch(...){ /*LOGCATCH("ChangeCode fail.");*/ }
  118. }
  119.  
  120.  
  121. #endif
Add Comment
Please, Sign In to add comment