Example #1
0
        private static char GetSquareChar(int charOneIndex, int charTwoIndex, PlayfairKey key,
                                          bool isDecryption = false, bool isSecondChar = false)
        {
            int row;

            if (isDecryption)
            {
                if (isSecondChar)
                {
                    row = GetCharRowIndex(charTwoIndex) * CipherGridDimension;
                    return(key.Value[row + GetCharColumnIndex(charOneIndex)]);
                }

                row = GetCharRowIndex(charOneIndex) * CipherGridDimension;
                return(key.Value[row + GetCharColumnIndex(charTwoIndex)]);
            }


            if (isSecondChar)
            {
                row = GetCharRowIndex(charTwoIndex) * CipherGridDimension;
                return(key.Value[row + GetCharColumnIndex(charOneIndex)]);
            }

            // Calculation inside Convert works out the row multiplier and multiplication provides the actual row index.
            row = GetCharRowIndex(charOneIndex) * CipherGridDimension;
            return(key.Value[row + GetCharColumnIndex(charTwoIndex)]);
        }
Example #2
0
        public string DecryptMessage(string cipherText, PlayfairKey cipherKey)
        {
            if (!_keyManagement.IsValidCipherKey(cipherKey))
            {
                throw new ArgumentException("Invalid cipher key.");
            }

            return(Decrypt(_digrathGenerator.GetCipherTextDigraphs(cipherText), cipherKey));
        }
Example #3
0
        public string EncryptMessage(string plainText, PlayfairKey cipherKey)
        {
            if (!_keyManagement.IsValidCipherKey(cipherKey))
            {
                throw new ArgumentException("Invalid cipher key.");
            }

            var sanitizedMessage = PlayfairUtil.GetSanitisedString(plainText);
            var digraths         = _digrathGenerator.GetMessageDigraths(sanitizedMessage);

            return(Encrypt(digraths, cipherKey));
        }
Example #4
0
        /// <summary>
        /// Based on the current index and whether attempting to encrypt/decrypt appropriate character is retrieved
        /// from the cipher key value. This method applies when both of the characters are on the same row.
        /// </summary>
        /// <param name="currentIndex">Current index of the character attempting to encrypt/decrypt.</param>
        /// <param name="key">Cipher key being used for encryption/decryption.</param>
        /// <param name="isDecryption">Flag determining whether to use decryption rule set. If not set to <c>true</c>,
        /// encryption mode is assumed.</param>
        /// <returns>Encrypted/Decrypted character corresponding to conditions specified.</returns>
        private static char GetCharacterSameRow(int currentIndex, PlayfairKey key, bool isDecryption = false)
        {
            // Decryption; Character immediately to the left.
            if (isDecryption)
            {
                // If left-most column: use end column character, otherwise use character to the left of current index.
                return(GetCharColumnIndex(currentIndex) == 0
                    ? key.Value[currentIndex + (CipherGridDimension - 1)]
                    : key.Value[currentIndex - 1]);
            }

            // Encryption; If character is right-most use first first character within the row, otherwise use
            // character immediately to the right.
            return(GetCharColumnIndex(currentIndex + 1) == 0
                ? key.Value[currentIndex - (CipherGridDimension - 1)]
                : key.Value[currentIndex + 1]);
        }
Example #5
0
        /// <summary>
        /// Based on the current index and whether attempting to encrypt/decrypt appropriate character is retrieved
        /// from the cipher key value. This method applies when both of the characters are in the same column.
        /// </summary>
        /// <param name="currentIndex">Current index of the character attempting to encrypt/decrypt.</param>
        /// <param name="key">Cipher key being used for encryption/decryption.</param>
        /// <param name="isDecryption">Flag determining whether to use decryption rule set. If not set to <c>true</c>,
        /// encryption mode is assumed.</param>
        /// <returns>Encrypted/Decrypted character corresponding to conditions specified.</returns>
        private static char GetCharacterSameColumn(int currentIndex, PlayfairKey key, bool isDecryption = false)
        {
            // Decryption; Character immediately above.
            if (isDecryption)
            {
                // If top-most row: use bottom row, otherwise use character immediately above. The 4 multiplier makes
                // it easy to transition from top-most to bottom row within the key.
                return(currentIndex < CipherGridDimension
                    ? key.Value[(CipherGridDimension * 4) + currentIndex]
                    : key.Value[currentIndex - CipherGridDimension]);
            }

            // Encryption; If character is on the bottom-most row use character from the top row of the same column,
            // otherwise use character from the row below.
            return(GetCharRowIndex(currentIndex) == (CipherGridDimension - 1)
                ? key.Value[currentIndex - (CipherGridDimension * 4)]
                : key.Value[currentIndex + CipherGridDimension]);
        }
Example #6
0
        private static string PerformGenericPlayfair(IEnumerable <Digraph> digraphs, PlayfairKey key,
                                                     bool isDecrypt = false)
        {
            var sb = new StringBuilder(string.Empty);

            foreach (var digraph in digraphs)
            {
                // Find digraph character index values.
                var charOneIndex = 0;
                var charTwoIndex = 0;
                for (var i = 0; i < key.Value.Length; i++)
                {
                    if (key.Value[i].Equals(digraph.CharacterOne))
                    {
                        charOneIndex = i;
                    }
                    else if (key.Value[i].Equals(digraph.CharacterTwo))
                    {
                        charTwoIndex = i;
                    }
                }

                if (AreOnTheSameRow(charOneIndex, charTwoIndex))
                {
                    sb.Append(GetCharacterSameRow(charOneIndex, key, isDecrypt));
                    sb.Append(GetCharacterSameRow(charTwoIndex, key, isDecrypt));
                }
                else if (AreInTheSameColumn(charOneIndex, charTwoIndex))
                {
                    sb.Append(GetCharacterSameColumn(charOneIndex, key, isDecrypt));
                    sb.Append(GetCharacterSameColumn(charTwoIndex, key, isDecrypt));
                }
                else
                {
                    sb.Append(GetSquareChar(charOneIndex, charTwoIndex, key, isDecrypt));
                    sb.Append(GetSquareChar(charOneIndex, charTwoIndex, key, isDecrypt, true));
                }
            }

            return(sb.ToString());
        }
Example #7
0
 /// <summary>
 /// Performs decryption.
 /// </summary>
 /// <param name="digraphs">Cipher text digraphs.</param>
 /// <param name="key">Encryption cipher key.</param>
 /// <returns>Decrypted cipher text.</returns>
 private static string Decrypt(IEnumerable <Digraph> digraphs, PlayfairKey key)
 {
     return(PerformGenericPlayfair(digraphs, key, true).ToLower());
 }
Example #8
0
 /// <summary>
 /// Performs the encryption.
 /// </summary>
 /// <param name="digraths">Plain text digraphs.</param>
 /// <param name="key">Encryption cipher key.</param>
 /// <returns>Encrypted cipher text.</returns>
 private static string Encrypt(IEnumerable <Digraph> digraths, PlayfairKey key)
 {
     return(PerformGenericPlayfair(digraths, key));
 }