コード例 #1
0
ファイル: FileSystem.cs プロジェクト: zDainy/Sea-Battleship
        private static string saveArrangement(bool[,] input)
        {
            bool[] field     = CryptSystem.Lining <bool>(input);
            bool[] scrambled = CryptSystem.Scramble(field, 4);
            byte[] bytes     = new byte[13];
            for (int i = 0; i < 13; i++)
            {
                bool[] tmp = new bool[8];
                for (int j = 0; j < 8; j++)
                {
                    tmp[j] = scrambled[8 * i + j];
                }
                bytes[i] = CryptSystem.BoolToByte(tmp);
            }
            byte[] result = new byte[34];
            byte[] hash   = CryptSystem.GetHash(bytes);
            byte[] keys   = new byte[13];
            random.NextBytes(keys);
            for (int i = 0; i < 13; i++)
            {
                result[2 * i]     = CryptSystem.Vigenere(bytes[i], keys[i]);
                result[2 * i + 1] = keys[i];
                bytes[i]          = (byte)(result[2 * i] ^ result[2 * i + 1]);
            }
            for (int i = 0; i < hash.Length; i++)
            {
                result[26 + i] = hash[i];
            }
            hash = CryptSystem.GetHash(bytes);
            for (int i = 0; i < hash.Length; i++)
            {
                result[26 + hash.Length + i] = hash[i];
            }
            string res = "";

            for (int i = 0; i < result.Length; i++)
            {
                res += CryptSystem.ByteToHex(result[i]);
            }
            return(res);
        }
コード例 #2
0
ファイル: FileSystem.cs プロジェクト: zDainy/Sea-Battleship
        private static string saveGame(Game g)
        {
            byte[,] result = new byte[10, 10];
            byte[,] map    = ArrangementsToByteArray(g.ServerShipArrangement, g.ClientShipArrangement);
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    result[j, i] = map[j, i];
                }
            }
            byte[] bytes = new byte[1];
            random.NextBytes(bytes);
            byte config = bytes[0];

            bool[] conf = CryptSystem.ByteToBool(config);
            conf[0] = g.GameConfig.IsOnline;
            if (g.GameConfig.IsOnline)  // Сохранение онлайн игры
            {
                byte[] rand = new byte[3];
                random.NextBytes(rand);
                string[] ip = g.GameConfig.Connection.Split(new char[2] {
                    '.', ':'
                });
                result[5, 0] = byte.Parse(ip[0]);
                result[5, 1] = byte.Parse(ip[1]);
                result[5, 2] = byte.Parse(ip[2]);
                result[5, 3] = byte.Parse(ip[3]);
                byte[] port = BitConverter.GetBytes(int.Parse(ip[4]));
                result[5, 4] = port[0];
                result[5, 5] = port[1];
                conf[0]      = g.TurnOwner == PlayerRole.Server;
                conf[3]      = (g.GameConfig.GameSpeed == GameSpeed.Slow) || (g.GameConfig.GameSpeed == GameSpeed.Turtle);
                conf[4]      = (g.GameConfig.GameSpeed == GameSpeed.Medium) || (g.GameConfig.GameSpeed == GameSpeed.Turtle);

                /*switch (g.GameConfig.GameSpeed)
                 * {
                 *  case GameSpeed.Fast:
                 *      conf[3] = false;
                 *      conf[4] = false;
                 *      break;
                 *  case GameSpeed.Medium:
                 *      conf[3] = false;
                 *      conf[4] = true;
                 *      break;
                 *  case GameSpeed.Slow:
                 *      conf[3] = true;
                 *      conf[4] = false;
                 *      break;
                 *  case GameSpeed.Turtle:
                 *      conf[3] = true;
                 *      conf[4] = true;
                 *      break;
                 * }*/
                config       = CryptSystem.BoolToByte(conf);
                result[5, 6] = config;
                result[5, 7] = rand[0];
                result[5, 8] = rand[1];
                result[5, 9] = rand[2];
            }
            else                        // Сохранениe оффлайн игры
            {
                byte[] rand = new byte[8];
                random.NextBytes(rand);
                result[5, 0] = rand[7];
                result[5, 1] = rand[0];
                result[5, 2] = rand[1];
                switch (g.GameConfig.BotLvl)
                {
                case BotLevels.Easy:
                    result[5, 3] = 1;
                    break;

                case BotLevels.Medium:
                    result[5, 3] = 2;
                    break;

                case BotLevels.Hard:
                    result[5, 3] = 3;
                    break;
                }
                result[5, 4] = rand[2];
                result[5, 5] = rand[3];
                conf[3]      = (g.GameConfig.GameSpeed == GameSpeed.Slow) || (g.GameConfig.GameSpeed == GameSpeed.Turtle);
                conf[4]      = (g.GameConfig.GameSpeed == GameSpeed.Medium) || (g.GameConfig.GameSpeed == GameSpeed.Turtle);

                /*switch (g.GameConfig.GameSpeed)
                 * {
                 *  case GameSpeed.Fast:
                 *      conf[3] = false;
                 *      conf[4] = false;
                 *      break;
                 *  case GameSpeed.Medium:
                 *      conf[3] = false;
                 *      conf[4] = true;
                 *      break;
                 *  case GameSpeed.Slow:
                 *      conf[3] = true;
                 *      conf[4] = false;
                 *      break;
                 *  case GameSpeed.Turtle:
                 *      conf[3] = true;
                 *      conf[4] = true;
                 *      break;
                 * }*/
                config       = CryptSystem.BoolToByte(conf);
                result[5, 6] = config;
                result[5, 7] = rand[4];
                result[5, 8] = rand[5];
                result[5, 9] = rand[6];
            }
            for (int i = 0; i < 10; i++)
            {
                byte[] tmp = new byte[5];
                for (int j = 0; j < 5; j++)
                {
                    tmp[j] = result[j, i];
                }
                byte[] hash = CryptSystem.GetHash(tmp);
                result[6, i] = hash[0];
                result[7, i] = hash[1];
                tmp          = CryptSystem.Vigenere(tmp, hash);
                for (int j = 0; j < 5; j++)
                {
                    result[j, i] = tmp[j];
                }
                tmp = new byte[6];
                for (int j = 0; j < 6; j++)
                {
                    tmp[j] = result[j, i];
                }
                hash         = CryptSystem.GetHash(tmp);
                result[8, i] = hash[0];
                result[9, i] = hash[1];
            }
            bytes = CryptSystem.Lining <byte>(result);
            string res = "";

            for (int i = 0; i < bytes.Length; i++)
            {
                res += CryptSystem.ByteToHex(bytes[i]);
            }
            return(res);
        }