Guest User

Untitled

a guest
May 26th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. namespace Encryption
  2. {
  3. public class CEncryptServer
  4. {
  5. static int m_nPos1, m_nPos2;
  6.  
  7. void Init()
  8. {
  9. m_nPos1 = m_nPos2 = 0; new CEncryptCode();
  10. }
  11.  
  12. public class CEncryptCode
  13. {
  14. static Byte[] m_bufEncrypt1 = new Byte[256], m_bufEncrypt2 = new Byte[256];
  15.  
  16. int aa = 0x7E,
  17. bb = 0x33,
  18. cc = 0xA1;
  19. ulong key1, key2;
  20.  
  21. public CEncryptCode() { Init(); }
  22. void Init()
  23. {
  24. int a1, b1, c1, fst1;
  25. a1 = (int)((key1 >> 0) & 0xFF) ^ aa;
  26. b1 = (int)((key1 >> 8) & 0xFF) ^ bb;
  27. c1 = (int)((key1 >> 24) & 0xFF) ^ cc;
  28. fst1 = (int)(key1 >> 16) & 0xFF;
  29.  
  30. int a2, b2, c2, fst2;
  31. a2 = (int)((key2 >> 0) & 0xFF) ^ aa;
  32. b2 = (int)((key2 >> 8) & 0xFF) ^ bb;
  33. c2 = (int)((key2 >> 24) & 0xFF) ^ cc;
  34. fst2 = (int)(key2 >> 16) & 0xFF;
  35.  
  36. Byte nCode = (Byte)fst1;
  37. for (int i = 0; i < 256; i++)
  38. {
  39. m_bufEncrypt1[i] = nCode;
  40. nCode = (Byte)((a1 * nCode * nCode + b1 * nCode + c1) % 256);
  41. }
  42. Debug.Assert(nCode == fst1);
  43.  
  44. nCode = (Byte)fst2;
  45. for (int i = 0; i < 256; i++)
  46. {
  47. m_bufEncrypt2[i] = (Byte)nCode;
  48. nCode = (Byte)((a2 * nCode * nCode + b2 * nCode + c2) % 256);
  49. }
  50. Debug.Assert(nCode == fst2);
  51. }
  52. void Encrypt(Byte[] bufMsg, int nLen, bool bMove)
  53. {
  54. int nOldPos1 = m_nPos1;
  55. int nOldPos2 = m_nPos2;
  56. for (int i = 0; i < nLen; i++)
  57. {
  58. bufMsg[i] ^= m_bufEncrypt1[m_nPos1];
  59. bufMsg[i] ^= m_bufEncrypt2[m_nPos2];
  60.  
  61. if (++m_nPos1 >= 256)
  62. {
  63. m_nPos1 = 0;
  64. if (++m_nPos2 >= 256)
  65. m_nPos2 = 0;
  66. }
  67. Debug.Assert(m_nPos1 >= 0 && m_nPos1 < 256);
  68. Debug.Assert(m_nPos2 >= 0 && m_nPos2 < 256);
  69. }
  70.  
  71. if (!bMove)
  72. {
  73. m_nPos1 = nOldPos1;
  74. m_nPos2 = nOldPos2;
  75. }
  76. }
  77.  
  78. unsafe void ChangeCode(UInt32 dwData)
  79. {
  80. UInt32 dwData2 = dwData * dwData;
  81. for (int i = 0; i < 256; i += 4)
  82. {
  83. *(UInt32*)(m_bufEncrypt1[i]) ^= dwData;
  84. *(UInt32*)(m_bufEncrypt2[i]) ^= dwData2;
  85. }
  86. }
  87. }
  88. }
  89.  
  90. }
Add Comment
Please, Sign In to add comment