Esempio n. 1
0
        public string DecryptMessage(string cipherText, VigenereKey cipherKey)
        {
            // Nothing to decrypt.
            if (!DoesInputContainAlphabeticalCharacters(cipherText))
            {
                return(string.Empty);
            }

            // Valid Vigenere cipher text (at least within this implementation) will contain only upper case letters
            // and special characters. Therefore any lower case letters render message invalid. Do not continue.
            if (cipherText.Where(char.IsLetter).Any(char.IsLower))
            {
                throw new InvalidOperationException("Invalid cipher text.");
            }

            var sb = new StringBuilder(string.Empty);

            var cipherAlphabets = GetCipherAlphabets(cipherKey.MemorableKey);
            var keyIndex        = 0;

            foreach (var encryptedCharacter in cipherText)
            {
                // Append special characters.
                if (!IsAlphabeticalCharacter(encryptedCharacter))
                {
                    sb.Append(encryptedCharacter);
                    continue;
                }

                var multiAlphabetIndex = keyIndex % cipherKey.MemorableKey.Length;
                var currentAlphabet    = cipherAlphabets
                                         .First(a => a.First().Equals(cipherKey.MemorableKey[multiAlphabetIndex]));

                sb.Append(currentAlphabet[Alphabet.IndexOf(encryptedCharacter)]);

                // Prevent keyIndex from overflowing.
                keyIndex = GetUpdatedKeyIndex(keyIndex, cipherKey.MemorableKey);
            }

            return(sb.ToString().ToLower());
        }
Esempio n. 2
0
        public string EncryptMessage(string plainText, VigenereKey cipherKey)
        {
            // Nothing to encrypt.
            if (!DoesInputContainAlphabeticalCharacters(plainText))
            {
                return(string.Empty);
            }
            if (!DoesInputContainAlphabeticalCharacters(cipherKey.MemorableKey))
            {
                return(string.Empty);
            }

            var sb = new StringBuilder(string.Empty);

            var cipherAlphabets = GetCipherAlphabets(cipherKey.MemorableKey);
            var keyIndex        = 0;

            foreach (var plainTextCharacter in plainText.ToUpper())
            {
                // Append special characters
                if (!IsAlphabeticalCharacter(plainTextCharacter))
                {
                    sb.Append(plainTextCharacter);
                    continue;
                }

                var multiAlphabetIndex = keyIndex % cipherKey.MemorableKey.Length;
                var currentAlphabet    = cipherAlphabets
                                         .First(a => a.First().Equals(cipherKey.MemorableKey[multiAlphabetIndex]));

                sb.Append(currentAlphabet[Alphabet.IndexOf(plainTextCharacter)]);

                // Prevent keyIndex from overflowing.
                keyIndex = GetUpdatedKeyIndex(keyIndex, cipherKey.MemorableKey);
            }

            return(sb.ToString());
        }