Exemplo n.º 1
0
        public IActionResult CaesarVisualization([FromBody] CaesarCipherViewModel viewModel)
        {
            CaesarCipher cipher = new CaesarCipher(viewModel.Key)
            {
                Alphabet = Alphabets.GetAlphabet((Alphabets.AlphabetType)viewModel.AlphabetType)
            };

            cipher.Alphabet = Alphabets.GetAlphabet((Alphabets.AlphabetType)viewModel.AlphabetType);

            string[] results = new string[4] {
                "alphabet", "newalphabet", "output", "input"
            };
            string encrypted = "";
            string input     = viewModel.Message;

            input = StringHelper.ReplaceWhitespace(input, "");
            input = input.ToUpper();
            int    alphabetLength = cipher.Alphabet.Length;
            int    newKey         = (viewModel.Key) % alphabetLength;
            string cipherAlphabet = "";

            for (int i = 0; i < alphabetLength; i++)
            {
                if ((i + newKey) >= alphabetLength)
                {
                    cipherAlphabet += cipher.Alphabet[i + newKey - alphabetLength];
                }
                else
                {
                    cipherAlphabet += cipher.Alphabet[i + newKey];
                }
            }
            results[0] = cipher.Alphabet;
            results[1] = cipherAlphabet;
            results[3] = input;
            try
            {
                encrypted  = cipher.Encrypt(viewModel.Message);
                results[2] = encrypted;
            }
            catch (NullReferenceException)
            {
            }
            catch (Exception)
            {
                return(BadRequest(new { Result = false, Message = Text.InvalidCharacter }));
            }
            return(Json(results));
        }
Exemplo n.º 2
0
        public IActionResult CaesarDecrypt([FromBody] CaesarCipherViewModel viewModel)
        {
            CaesarCipher cipher = new CaesarCipher(viewModel.Key)
            {
                Alphabet = Alphabets.GetAlphabet((Alphabets.AlphabetType)viewModel.AlphabetType)
            };

            string decrypted = "";

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