Example #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));
        }
Example #2
0
        public IActionResult VigenereVisualization([FromBody] VigenereCipherViewModel viewModel)
        {
            VigenereCipher cipher = new VigenereCipher
                                        (viewModel.Key, Alphabets.GetAlphabet((Alphabets.AlphabetType)viewModel.AlphabetType));

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

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

            input = StringHelper.ReplaceWhitespace(input, "");
            input = input.ToUpper();
            int alphabetLength = cipher.Alphabet.Length;

            results[0] = cipher.Alphabet;
            results[2] = input;
            try
            {
                encrypted  = cipher.Encrypt(viewModel.Message);
                results[1] = encrypted;
                var key      = new StringBuilder();
                int keyIndex = 0;
                for (int i = 0; i < input.Length; i++)
                {
                    key.Append(cipher.Key[keyIndex]);

                    keyIndex++;
                    if (keyIndex >= cipher.Key.Length)
                    {
                        keyIndex = 0;
                    }
                }
                results[3] = key.ToString();
            }
            catch (NullReferenceException)
            {
            }
            catch (Exception)
            {
                return(BadRequest(new { Result = false, Message = Text.InvalidCharacter }));
            }
            return(Json(results));
        }
Example #3
0
        public IActionResult VigenereDecrypt([FromBody] VigenereCipherViewModel viewModel)
        {
            VigenereCipher cipher = new VigenereCipher
                                        (viewModel.Key, Alphabets.GetAlphabet((Alphabets.AlphabetType)viewModel.AlphabetType));

            string decrypted = "";

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

            return(Json(decrypted));
        }
Example #4
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));
        }
Example #5
0
        public IActionResult AffineEncrypt([FromBody] AffineCipherViewModel viewModel)
        {
            AffineCipher cipher = new AffineCipher(viewModel.KeyA, viewModel.KeyB)
            {
                Alphabet = Alphabets.GetAlphabet((Alphabets.AlphabetType)viewModel.AlphabetType)
            };

            string encrypted = "";

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

            return(Json(encrypted));
        }
Example #6
0
        public IActionResult AffineVisualization([FromBody] AffineCipherViewModel viewModel)
        {
            AffineCipher cipher = new AffineCipher(viewModel.KeyA, viewModel.KeyB)
            {
                Alphabet = Alphabets.GetAlphabet((Alphabets.AlphabetType)viewModel.AlphabetType)
            };

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

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

            input = StringHelper.ReplaceWhitespace(input, "");
            input = input.ToUpper();
            int alphabetLength = cipher.Alphabet.Length;

            input      = StringHelper.ReplaceWhitespace(input, "");
            input      = input.ToUpper();
            results[0] = cipher.Alphabet;
            results[2] = input;
            try
            {
                encrypted  = cipher.Encrypt(viewModel.Message);
                results[1] = encrypted;
            }
            catch (NullReferenceException)
            {
            }
            catch (Exception)
            {
                return(BadRequest(new { Result = false, Message = Text.InvalidCharacter }));
            }
            return(Json(results));
        }