예제 #1
0
        public void Decode_MessageWithTableDimensions_ReturnsMessageDecodedForVerticalLinesAlgorithm(
            string message, int rows, int columns, string encodedMessage)
        {
            var routeCipher = new RouteCipher(new VerticalLinesAlgorithm());

            var encoded = routeCipher.Decode(message, rows, columns);

            Assert.Equal(encodedMessage, encoded);
        }
예제 #2
0
        public IActionResult RouteDecrypt([FromBody] RouteEncryptViewModel viewModel)
        {
            RouteCipher cipher = new RouteCipher(viewModel.Key);

            string decrypted = "";

            try
            {
                decrypted = cipher.Decrypt(viewModel.Message);
            }
            catch (Exception e)
            {
                return(BadRequest(new { Result = false, Message = e.Message }));
            }

            return(Json(decrypted));
        }
예제 #3
0
        public IActionResult RouteVisualization([FromBody] RouteEncryptViewModel viewModel)
        {
            RouteCipher cipher = new RouteCipher(viewModel.Key);

            string encrypted = "";


            string[] results = new string[3] {
                "output", "input", "route"
            };
            List <int> RouteList = new List <int>();
            string     input     = viewModel.Message;

            input      = StringHelper.ReplaceWhitespace(input, "");
            input      = input.ToUpper();
            results[1] = input;
            try
            {
                encrypted  = cipher.Encrypt(viewModel.Message);
                results[0] = encrypted;


                int encryptedCounter = 0;
                int direction        = 0;//0-down, 1-left, 2-up, 3-right

                int columnRangeMin = 0;
                int columnRangeMax = cipher.MessageMatrix.GetLength(1) - 1;

                int rowRangeMin = 0;
                int rowRangeMax = cipher.MessageMatrix.GetLength(0) - 1;

                int steps = 0;

                while (steps < input.Length)
                {
                    if (direction == 0)
                    {
                        if (encryptedCounter >= cipher.MessageMatrix.Length)
                        {
                            break;
                        }
                        for (int i = rowRangeMin; i <= rowRangeMax; i++)
                        {
                            RouteList.Add(i);
                            RouteList.Add(columnRangeMax);
                            encryptedCounter++;
                        }

                        direction = 1;
                        columnRangeMax--;
                    }

                    if (direction == 1)
                    {
                        if (encryptedCounter >= cipher.MessageMatrix.Length)
                        {
                            break;
                        }
                        for (int i = columnRangeMax; i >= columnRangeMin; i--)
                        {
                            RouteList.Add(rowRangeMax);
                            RouteList.Add(i);
                            encryptedCounter++;
                        }


                        direction = 2;
                        rowRangeMax--;
                    }

                    if (direction == 2)
                    {
                        if (encryptedCounter >= cipher.MessageMatrix.Length)
                        {
                            break;
                        }
                        for (int i = rowRangeMax; i >= columnRangeMin; i--)
                        {
                            RouteList.Add(i);
                            RouteList.Add(columnRangeMin);
                            encryptedCounter++;
                        }


                        direction = 3;
                        columnRangeMin++;
                    }
                    if (direction == 3)
                    {
                        if (encryptedCounter >= cipher.MessageMatrix.Length)
                        {
                            break;
                        }
                        for (int i = columnRangeMin; i <= columnRangeMax; i++)
                        {
                            RouteList.Add(rowRangeMin);
                            RouteList.Add(i);
                            encryptedCounter++;
                        }

                        direction = 0;
                        rowRangeMin++;
                    }
                }
                var stringRoute = new StringBuilder();
                for (int i = 0; i < RouteList.Count; i++)
                {
                    stringRoute.Append(RouteList[i]);
                    stringRoute.Append(',');
                }
                results[2] = stringRoute.ToString();
            }
            catch (Exception e)
            {
                return(BadRequest(new { Result = false, Message = e.Message }));
            }

            return(Json(results));
        }