Beispiel #1
0
        public RandomEx()
        {
            Random rnd = new Random();

            // test/generate the random algorithm
            while (true)
            {
                List <int> temp = new List <int>();
                IntData = BitConverter.GetBytes(rnd.Next());
                WopEx.GenerateCryptoCode(rnd.Next(), 10, ref encryptCode, ref decryptCode);
                this.wopEx = new WopEx(BitConverter.GetBytes(rnd.Next()), BitConverter.GetBytes(rnd.Next()), InitialVector, encryptCode, decryptCode, WopEncMode.ShuffleInstructions, 2, false);
                bool success = true;

                for (int i = 0; i < 100; i++)
                {
                    int tmpInt = GetNext();
                    if (!temp.Contains(tmpInt))
                    {
                        temp.Add(tmpInt);
                    }
                    else
                    {
                        success = false;
                        break;
                    }
                }
                if (success)
                {
                    break;
                }
                temp.Clear();
            }
        }
 public InstructionInfo(WopEx.Instruction Inst, BigInteger Value, BigInteger Value2, BigInteger Value3)
 {
     this.Inst = Inst;
     this.Value = Value;
     this.Value2 = Value2;
     this.Value3 = Value3;
 }
Beispiel #3
0
        public void Test_WopEx_Simple()
        {
            byte[] encCode = new byte[0];
            byte[] decCode = new byte[0];
            WopEx.GenerateCryptoCode(123456, 15, ref encCode, ref decCode);

            WopEx wopEx = new WopEx(TestKey, TestSalt, TestIV, encCode, decCode, SecureSocketProtocol3.WopEncMode.Simple, 1, false);

            Random rnd = new Random(DateTime.Now.Millisecond);

            for (int j = 1; j < 1024; j++)    //test 1024 bytes
            {
                for (int k = 0; k < 100; k++) //test the encryption / decryption 100x
                {
                    //test the byte a 100x
                    byte[] TestData    = new byte[j];
                    byte[] TestOrgData = new byte[j];
                    rnd.NextBytes(TestData);
                    Array.Copy(TestData, TestOrgData, TestOrgData.Length);

                    wopEx.Encrypt(TestOrgData, 0, TestData.Length);
                    wopEx.Decrypt(TestOrgData, 0, TestData.Length);

                    Assert.IsTrue(TestOrgData.Length == TestData.Length, "Size did not match after decryption");

                    for (int x = 0; x < TestData.Length; x++)
                    {
                        Assert.IsTrue(TestData[x] == TestOrgData[x], "Decryption failed");
                    }
                }
            }
        }
Beispiel #4
0
 internal void ApplyKey(WopEx wopEx, byte[] key)
 {
     for (int i = 0; i < wopEx.Key.Length + (key.Length * 3); i++)
     {
         wopEx.Key[i % wopEx.Key.Length]   += key[i % key.Length];
         wopEx.Salt[i % wopEx.Salt.Length] += key[(i + 2) % key.Length];
     }
 }
        public override MazeErrorCode onReceiveData(byte[] Data, ref byte[] ResponseData)
        {
            ResponseData = new byte[0];
            switch (base.Step)
            {
            case 1:
            {
                if (Data.Length != 32)
                {
                    SysLogger.Log("[MazeHandShake][Server] Receive Length missmatch", SysLogType.Debug);
                    return(MazeErrorCode.Error);
                }

                wopEx = base.GetWopEncryption();
                wopEx.Decrypt(Data, 0, Data.Length);

                BigInteger server_prime = new BigInteger(Data);
                if (server_prime.isProbablePrime())
                {
                    //verify the prime from the server
                    BigInteger server_Prime_test = BigInteger.genPseudoPrime(256, 50, new Random(BitConverter.ToInt32(wopEx.Key, 0)));

                    if (server_prime != server_Prime_test)
                    {
                        //Attacker detected ?
                        SysLogger.Log("[MazeHandShake][Server] Man-In-The-Middle detected", SysLogType.Debug);
                        return(MazeErrorCode.Error);
                    }

                    //successful
                    //generate another prime and send it back
                    BigInteger client_Prime = BigInteger.genPseudoPrime(256, 50, new Random(server_prime.IntValue()));

                    byte[] primeData = client_Prime.getBytes();
                    wopEx.Encrypt(primeData, 0, primeData.Length);
                    ResponseData = primeData;

                    BigInteger key = base.ModKey(server_prime, client_Prime);
                    //apply key to encryption
                    ApplyKey(wopEx, key);

                    base.FinalKey  = wopEx.Key;
                    base.FinalSalt = wopEx.Salt;

                    Step++;
                    return(MazeErrorCode.Finished);
                }
                else
                {
                    //connection failed, using old keys ?
                    SysLogger.Log("[MazeHandShake][Server] Invalid received data", SysLogType.Debug);
                    return(MazeErrorCode.Error);
                }
            }
            }

            return(MazeErrorCode.Success);
        }
Beispiel #6
0
        public WopEx GetWopEncryption(byte[] Key, byte[] Salt)
        {
            byte[] CryptCode   = new byte[0];
            byte[] DecryptCode = new byte[0];
            WopEx.GenerateCryptoCode(BitConverter.ToInt32(Key, 0) + BitConverter.ToInt32(Salt, 0), 15, ref CryptCode, ref DecryptCode);

            //badly hardcoded, should be dynamic
            byte[] IV = new byte[] { 71, 140, 33, 100, 118, 9, 92, 129, 42, 113, 247, 20, 250, 36, 90, 204, 108, 64, 151, 34, 216, 92, 188, 191, 132, 127, 15, 28, 135, 247, 32, 246, };

            //apply Public Key to IV just to make it a little harder
            if (_publicKeyData != null)
            {
                for (int i = 0; i < _publicKeyData.Length; i++)
                {
                    IV[i % IV.Length] += _publicKeyData[i];
                }
            }

            return(new WopEx(Key, Salt, IV, CryptCode, DecryptCode, WopEncMode.Simple, ROUNDS, true));
        }
Beispiel #7
0
        public void Test_WopEx_GenerateNewAlgorithm()
        {
            byte[] Key  = new byte[] { 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5 };
            byte[] Salt = new byte[] { 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1 };

            byte[] encCode = new byte[0];
            byte[] decCode = new byte[0];
            WopEx.GenerateCryptoCode(123456, 15, ref encCode, ref decCode);

            WopEx wopEx = new WopEx(TestKey, TestSalt, TestIV, encCode, decCode, SecureSocketProtocol3.WopEncMode.GenerateNewAlgorithm, 1, false);

            Random rnd = new Random(12345678);

            for (int j = 1; j < 1024; j++)    //test 1024 bytes
            {
                for (int k = 0; k < 100; k++) //test the encryption / decryption 100x
                {
                    //test the byte a 100x
                    byte[] TestData    = new byte[j];
                    byte[] TestOrgData = new byte[j];
                    rnd.NextBytes(TestData);
                    Array.Copy(TestData, TestOrgData, TestOrgData.Length);

                    wopEx.Encrypt(TestData, 0, TestData.Length);
                    wopEx.Decrypt(TestData, 0, TestData.Length);

                    if (TestOrgData.Length != TestData.Length)
                    {
                        throw new Exception("Size did not match after decryption");
                    }

                    for (int x = 0; x < TestData.Length; x++)
                    {
                        if (TestData[x] != TestOrgData[x])
                        {
                            throw new Exception("Decryption failed, j=" + j + ", k=" + k);
                        }
                    }
                }
            }
        }
Beispiel #8
0
 private static void ShowAlgorithm(WopEx wop)
 {
     /*Log("Instructions: " + wop..EncInstructions.Length);
      *
      * foreach (WopEx.InstructionInfo inf in wop.EncInstructions)
      * {
      *  if (inf.Inst != WopEx.Instruction.ForLoop_PlusMinus)
      *  {
      *      Log("Instruction:" + inf.Inst + ", value: " + inf.Value);
      *  }
      *  else
      *  {
      *      Log("\r\nInstruction:" + inf.Inst + ":");
      *      Log("for(int i = 0; i < " + (inf.Value3 >> 1) + "; i++)");
      *      Log("{");
      *      Log("\tvalue -= " + inf.Value + ";");
      *      Log("\tvalue += " + inf.Value2 + ";");
      *      Log("}");
      *      Log();
      *  }
      * }*/
 }
Beispiel #9
0
        public void Test_WopEx_GenerateNewAlgorithm_100MB_Total_65K_Chunk()
        {
            byte[] Key  = new byte[] { 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5 };
            byte[] Salt = new byte[] { 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1 };

            byte[] encCode = new byte[0];
            byte[] decCode = new byte[0];
            WopEx.GenerateCryptoCode(123456, 15, ref encCode, ref decCode);

            WopEx wopEx = new WopEx(TestKey, TestSalt, TestIV, encCode, decCode, SecureSocketProtocol3.WopEncMode.Simple, 1, false);

            Random rnd = new Random(12345678);

            long TotalData = (1000 * 1000) * 100;
            long TotalDone = 0;

            byte[] DataChunk = new byte[65535];
            rnd.NextBytes(DataChunk);

            Stopwatch     SW          = Stopwatch.StartNew();
            List <double> SpeedPerSec = new List <double>();
            double        TempSpeed   = 0;

            while (TotalDone < TotalData)
            {
                wopEx.Encrypt(DataChunk, 0, DataChunk.Length);
                //wopEx.Decrypt(DataChunk, 0, DataChunk.Length);
                TotalDone += DataChunk.Length;
                TempSpeed += DataChunk.Length;

                if (SW.ElapsedMilliseconds >= 1000)
                {
                    SpeedPerSec.Add(Math.Round((TempSpeed / 1000D) / 1000D, 2));
                    TempSpeed = 0;
                    SW        = Stopwatch.StartNew();
                }
            }
            SW.Stop();
        }
Beispiel #10
0
        static void Main(string[] args)
        {
            RSA_Test();

            while (true)
            {
                byte[] Key  = new byte[] { 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, };
                byte[] Salt = new byte[] { 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 2, };
                byte[] IV   = new byte[] { 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 2, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, };

                byte[] EncryptCode = new byte[0];
                byte[] DecryptCode = new byte[0];
                WopEx.GenerateCryptoCode(12345678, 20, ref EncryptCode, ref DecryptCode);
                wop_enc = new WopEx(Key, Salt, IV, EncryptCode, DecryptCode, WopEncMode.GenerateNewAlgorithm, 1, true);
                wop_dec = new WopEx(Key, Salt, IV, EncryptCode, DecryptCode, WopEncMode.GenerateNewAlgorithm, 1, true);

                //if (File.Exists("./temp.txt"))
                //    File.Delete("./temp.txt");

                Log("WopEx Algorithm with Shuffle algorithm enabled");
                Log("Key:  " + BitConverter.ToString(Key));
                Log("Salt: " + BitConverter.ToString(Salt));
                Log();

                Log("Encryption Algorithm:");
                ShowAlgorithm(wop_enc);

                byte[] Data = new byte[] { 1, 3, 3, 7 };

                //while(true)
                {
                    EncDec(Data);
                }
            }
            Process.GetCurrentProcess().WaitForExit();
        }
 public InstructionInfo(WopEx.Instruction Inst, BigInteger Value)
 {
     this.Inst = Inst;
     this.Value = Value;
 }
Beispiel #12
0
        static void Main(string[] args)
        {
            Test  lel = new Test();
            ulong um  = lel.CalculateULong(1);

            if (um == 0)
            {
                return;
            }

            byte[] Key  = new byte[] { 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5 };
            byte[] Salt = new byte[] { 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1 };

            byte[] encCode = new byte[0];
            byte[] decCode = new byte[0];
            WopEx.GenerateCryptoCode(123456, 100, ref encCode, ref decCode);

            WopEx wopEx = new WopEx(TestKey, TestSalt, TestIV, encCode, decCode, SecureSocketProtocol3.WopEncMode.GenerateNewAlgorithm, 1, true);

            Random rnd = new Random(12345678);

            long TotalData = (1000 * 1000) * 100;
            long TotalDone = 0;

            byte[] DataChunk = new byte[65535];
            //rnd.NextBytes(DataChunk);

            Stopwatch SW = Stopwatch.StartNew();

            //simple AES test
            HwAes AES  = new HwAes(Key, new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, 256, CipherMode.CBC, PaddingMode.PKCS7);
            HwAes AES2 = new HwAes(Key, new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, 256, CipherMode.CBC, PaddingMode.PKCS7);

            while (true)
            {
                byte[] enc = AES.Encrypt(DataChunk, 0, DataChunk.Length);
                TotalDone += DataChunk.Length;

                if (SW.ElapsedMilliseconds >= 1000)
                {
                    double speed = Math.Round((TotalDone / 1000D) / 1000D, 2);
                    Console.WriteLine("Speed: " + speed + "MBps (" + Math.Round((((double)speed * 8F) / 1000), 2) + " Gbps)");
                    TotalDone = 0;
                    SW        = Stopwatch.StartNew();
                }
            }


            Stopwatch TotalTimeSW = Stopwatch.StartNew();
            double    TempSpeed   = 0;

            while (TotalDone < TotalData)
            {
                wopEx.Encrypt(DataChunk, 0, DataChunk.Length);
                //wopEx.Decrypt(DataChunk, 0, DataChunk.Length);
                TotalDone += DataChunk.Length;
                TempSpeed += DataChunk.Length;

                if (SW.ElapsedMilliseconds >= 1000)
                {
                    double speed = Math.Round((TempSpeed / 1000D) / 1000D, 2);
                    Console.WriteLine("Speed: " + speed + "MBps");
                    TempSpeed = 0;
                    SW        = Stopwatch.StartNew();
                }
            }
            SW.Stop();
            TotalTimeSW.Stop();

            if (TempSpeed > 0)
            {
                double speeds = Math.Round((TempSpeed / 1000D) / 1000D, 2);
                Console.WriteLine("Speed: " + speeds + "MBps");
            }

            Console.WriteLine("Done encrypting 100MB in " + TotalTimeSW.Elapsed.Seconds + " second(s)");
            Console.ReadLine();
        }
        public override MazeErrorCode onReceiveData(byte[] Data, ref byte[] ResponseData)
        {
            ResponseData = new byte[0];

            if (LastErrorCode != MazeErrorCode.Success)
            {
                //don't continue if the client/server messed something up
                return(LastErrorCode);
            }

            switch (base.Step)
            {
            case 1:
            {
                //step 2
                if (Data.Length != Mazing.ByteCode.Length)
                {
                    SysLogger.Log("[MazeHandShake][Server] ByteCode Length Missmatch", SysLogType.Debug);
                    return(MazeErrorCode.WrongByteCode);
                }

                for (int i = 0; i < Mazing.ByteCode.Length; i++)
                {
                    if (Mazing.ByteCode[i] != Data[i])
                    {
                        SysLogger.Log("[MazeHandShake][Server] WrongByteCode from client", SysLogType.Debug);
                        return(MazeErrorCode.WrongByteCode);
                    }
                }
                Step++;
                break;
            }

            case 2:
            {
                if (onFindKeyInDatabase == null)     //programmer error
                {
                    SysLogger.Log("[MazeHandShake][Server] onFindKeyInDatabase is null", SysLogType.Debug);
                    ResponseData = GetFailResponseData();     //not encrypted, client knows this will fail
                    return(MazeErrorCode.Error);
                }

                string EncHashedMsg = BitConverter.ToString(SHA512Managed.Create().ComputeHash(Data, 0, Data.Length)).Replace("-", "");
                byte[] _key         = new byte[0];
                byte[] _salt        = new byte[0];
                byte[] _publicKey   = new byte[0];
                string _userName    = "";

                if (onFindKeyInDatabase(EncHashedMsg, ref _key, ref _salt, ref _publicKey, ref _userName))
                {
                    this.PublicKeyData = TrimArray(_publicKey, Mazing.MAX_KEY_SIZE);
                    this.wopEx         = base.GetWopEncryption(_key, _salt);

                    base.FinalKey  = _key;
                    base.FinalSalt = _salt;

                    //let's try to decrypt the data, should go successful
                    wopEx.Decrypt(Data, 0, Data.Length);

                    if (Data.Length != _publicKey.Length)
                    {
                        SysLogger.Log("[MazeHandShake][Server] Public key length missmatch", SysLogType.Debug);
                        //key size not the same... strange
                        ResponseData = GetFailResponseData();
                        return(MazeErrorCode.Error);
                    }

                    for (int i = 0; i < _publicKey.Length; i++)
                    {
                        if (Data[i] != _publicKey[i])
                        {
                            SysLogger.Log("[MazeHandShake][Server] Public key missmatch", SysLogType.Debug);
                            //public key did not match... strange
                            ResponseData = GetFailResponseData();
                            return(MazeErrorCode.Error);
                        }
                    }

                    //encryption / public key went successful for now
                    this.server_Prime = BigInteger.genPseudoPrime(256, 50, new Random(BitConverter.ToInt32(_key, 0)));
                    byte[] primeData = server_Prime.getBytes();
                    wopEx.Encrypt(primeData, 0, primeData.Length);
                    ResponseData = primeData;

                    this.Username = _userName;

                    Step++;
                }
                else
                {
                    SysLogger.Log("[MazeHandShake][Server] No user key found in database", SysLogType.Debug);
                    ResponseData = GetFailResponseData();
                    return(MazeErrorCode.UserKeyNotFound);
                }
                break;
            }

            case 3:
            {
                //response back from client with his prime number
                wopEx.Decrypt(Data, 0, Data.Length);

                this.client_Prime = new BigInteger(Data);
                if (this.client_Prime.isProbablePrime())
                {
                    //verify the prime from the client
                    BigInteger client_Prime_test = BigInteger.genPseudoPrime(256, 50, new Random(this.server_Prime.IntValue()));

                    if (this.client_Prime != client_Prime_test)
                    {
                        //Attacker detected ?
                        SysLogger.Log("[MazeHandShake][Server] Man-In-The-Middle detected", SysLogType.Debug);
                        return(MazeErrorCode.Error);
                    }

                    BigInteger key = base.ModKey(server_Prime, client_Prime);
                    //apply key to encryption
                    ApplyKey(wopEx, key);
                    return(MazeErrorCode.Finished);
                }
                else
                {
                    SysLogger.Log("[MazeHandShake][Server] Invalid response", SysLogType.Debug);
                    return(MazeErrorCode.Error);
                }
            }
            }
            return(MazeErrorCode.Success);
        }
Beispiel #14
0
 internal void ApplyKey(WopEx wopEx, BigInteger prime)
 {
     PatchKey(ref prime);
     byte[] primeKey = prime.getBytes();
     ApplyKey(wopEx, primeKey);
 }
        public Connection(SSPClient client)
        {
            this.Client = client;

            this.Connected     = true;
            this.Headers       = new HeaderList(this);
            this.HandshakeSync = new SyncObject(this);
            this.InitSync      = new SyncObject(this);
            this.RegisteredOperationalSockets = new SortedList <ulong, Type>();
            this.Requests             = new SortedList <int, SyncObject>();
            this.OperationalSockets   = new SortedList <ushort, OperationalSocket>();
            this.EncryptionAlgorithm  = client.Server != null ? client.Server.serverProperties.EncryptionAlgorithm : client.Properties.EncryptionAlgorithm;
            this.CompressionAlgorithm = client.Server != null ? client.Server.serverProperties.CompressionAlgorithm : client.Properties.CompressionAlgorithm;

            //generate the header encryption
            byte[] privKey = client.Server != null ? client.Server.serverProperties.NetworkKey : client.Properties.NetworkKey;

            PrivateSeed = privKey.Length >= 4 ? BitConverter.ToInt32(privKey, 0) : 0xBEEF;

            for (int i = 0; i < privKey.Length; i++)
            {
                PrivateSeed += privKey[i];
            }

            byte[] SaltKey = new byte[privKey.Length];
            Array.Copy(privKey, SaltKey, SaltKey.Length);

            for (int i = 0; i < SaltKey.Length; i++)
            {
                SaltKey[i] += (byte)PrivateSeed;
            }


            uint CipherRounds = client.Server != null ? client.Server.serverProperties.Cipher_Rounds : client.Properties.Cipher_Rounds;

            byte[] encCode = new byte[0];
            byte[] decCode = new byte[0];
            WopEx.GenerateCryptoCode(PrivateSeed, 5, ref encCode, ref decCode);
            this.HeaderEncryption = new WopEx(privKey, SaltKey, InitialVector, encCode, decCode, WopEncMode.GenerateNewAlgorithm, CipherRounds, false);

            WopEx.GenerateCryptoCode(PrivateSeed << 3, 15, ref encCode, ref decCode);
            this.PayloadEncryption = new WopEx(privKey, SaltKey, InitialVector, encCode, decCode, WopEncMode.Simple, CipherRounds, true);

            this.EncAES = new HwAes(this, privKey, 256, System.Security.Cryptography.CipherMode.CBC, System.Security.Cryptography.PaddingMode.PKCS7);

            this.headerConfuser = new DataConfuser(PrivateSeed, Connection.HEADER_SIZE);

            this.QuickLZ = new UnsafeQuickLZ();

            this.messageHandler = new MessageHandler((uint)PrivateSeed + 0x0FA453FB);
            this.messageHandler.AddMessage(typeof(MsgHandshake), "MAZE_HAND_SHAKE");
            this.messageHandler.AddMessage(typeof(MsgCreateConnection), "CREATE_CONNECTION");
            this.messageHandler.AddMessage(typeof(MsgCreateConnectionResponse), "CREATE_CONNECTION_RESPONSE");

            this.messageHandler.AddMessage(typeof(MsgInitOk), "INIT_OK");
            this.messageHandler.AddMessage(typeof(MsgGetNextId), "GET_NEXT_NUMBER");
            this.messageHandler.AddMessage(typeof(MsgGetNextIdResponse), "GET_NEXT_NUMBER_RESPONSE");

            this.messageHandler.AddMessage(typeof(MsgOpDisconnect), "OP_DISCONNECT");
            this.messageHandler.AddMessage(typeof(MsgOpDisconnectResponse), "OP_DISCONNECT_RESPONSE");

            this.messageHandler.AddMessage(typeof(MsgKeepAlive), "KEEP_ALIVE");

            Headers.RegisterHeader(typeof(SystemHeader));
            Headers.RegisterHeader(typeof(ConnectionHeader));
            Headers.RegisterHeader(typeof(RequestHeader));
        }