Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Encryption
- {
- public class CEncryptServer
- {
- static int m_nPos1, m_nPos2;
- void Init()
- {
- m_nPos1 = m_nPos2 = 0; new CEncryptCode();
- }
- public class CEncryptCode
- {
- static Byte[] m_bufEncrypt1 = new Byte[256], m_bufEncrypt2 = new Byte[256];
- int aa = 0x7E,
- bb = 0x33,
- cc = 0xA1;
- ulong key1, key2;
- public CEncryptCode() { Init(); }
- void Init()
- {
- int a1, b1, c1, fst1;
- a1 = (int)((key1 >> 0) & 0xFF) ^ aa;
- b1 = (int)((key1 >> 8) & 0xFF) ^ bb;
- c1 = (int)((key1 >> 24) & 0xFF) ^ cc;
- fst1 = (int)(key1 >> 16) & 0xFF;
- int a2, b2, c2, fst2;
- a2 = (int)((key2 >> 0) & 0xFF) ^ aa;
- b2 = (int)((key2 >> 8) & 0xFF) ^ bb;
- c2 = (int)((key2 >> 24) & 0xFF) ^ cc;
- fst2 = (int)(key2 >> 16) & 0xFF;
- Byte nCode = (Byte)fst1;
- for (int i = 0; i < 256; i++)
- {
- m_bufEncrypt1[i] = nCode;
- nCode = (Byte)((a1 * nCode * nCode + b1 * nCode + c1) % 256);
- }
- Debug.Assert(nCode == fst1);
- nCode = (Byte)fst2;
- for (int i = 0; i < 256; i++)
- {
- m_bufEncrypt2[i] = (Byte)nCode;
- nCode = (Byte)((a2 * nCode * nCode + b2 * nCode + c2) % 256);
- }
- Debug.Assert(nCode == fst2);
- }
- void Encrypt(Byte[] bufMsg, int nLen, bool bMove)
- {
- int nOldPos1 = m_nPos1;
- int nOldPos2 = m_nPos2;
- for (int i = 0; i < nLen; i++)
- {
- bufMsg[i] ^= m_bufEncrypt1[m_nPos1];
- bufMsg[i] ^= m_bufEncrypt2[m_nPos2];
- if (++m_nPos1 >= 256)
- {
- m_nPos1 = 0;
- if (++m_nPos2 >= 256)
- m_nPos2 = 0;
- }
- Debug.Assert(m_nPos1 >= 0 && m_nPos1 < 256);
- Debug.Assert(m_nPos2 >= 0 && m_nPos2 < 256);
- }
- if (!bMove)
- {
- m_nPos1 = nOldPos1;
- m_nPos2 = nOldPos2;
- }
- }
- unsafe void ChangeCode(UInt32 dwData)
- {
- UInt32 dwData2 = dwData * dwData;
- for (int i = 0; i < 256; i += 4)
- {
- *(UInt32*)(m_bufEncrypt1[i]) ^= dwData;
- *(UInt32*)(m_bufEncrypt2[i]) ^= dwData2;
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment