Esempio n. 1
0
        static void Main(string[] args)
        {
            // Variables
            string hexEncodedStr = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736";

            ScoredMessage[] decodedTopFiveResults = new ScoredMessage[5];

            // Populate decodedTopFiveResults
            for (int i = 0; i < decodedTopFiveResults.Length; i++)
            {
                decodedTopFiveResults[i] = new ScoredMessage();
            }

            // Pass hexEncodedStr to DiscoverSingleCharacterKey object for decoding
            DiscoverSingleCharacterKey key = new DiscoverSingleCharacterKey(hexEncodedStr);

            // Get top five decoded results
            decodedTopFiveResults = key.getTopFive();

            // Display top five decoded results
            for (int i = 0; i < decodedTopFiveResults.Length; i++)
            {
                Console.WriteLine(decodedTopFiveResults[i].ToString());
            }
        }
        /// <summary>
        /// Orders decoded messages by their scores and selects
        /// top five most likely candidates based on character
        /// frequency.
        /// </summary>
        /// <returns>array of top 5 scored messages</returns>
        public ScoredMessage[] getTopFive()
        {
            // Initialize ScoredMessage object topFive
            ScoredMessage[] topFive  = new ScoredMessage[5];
            ArrayList       msgArray = new ArrayList();
            int             index    = 25;

            // Add objects from scoredMessages to msgArray
            foreach (ScoredMessage value in scoredMessages)
            {
                msgArray.Add(value);
            }

            // Sort message array.
            msgArray.Sort();

            // Populate top five ScoredMessage array.
            for (int i = 0; i < topFive.Length; i++)
            {
                topFive[i] = (ScoredMessage)msgArray[index];
                index--;
            }

            return(topFive);
        }
Esempio n. 3
0
 public int CompareTo(object obj)
 {
     if (obj is ScoredMessage)
     {
         ScoredMessage sMsg = (ScoredMessage)obj;
         return(Score.CompareTo(sMsg.Score));
     }
     else
     {
         throw new NotImplementedException();
     }
 }
        /// <summary>
        /// Default constructor for DiscoverSingleCharacterKey
        /// class. Takes in message as argument.
        /// </summary>
        /// <param name="message"></param>
        public DiscoverSingleCharacterKey(string message)
        {
            this.message = message;

            //assign values to the array
            for (int i = 0; i < scoredMessages.Length; i++)
            {
                scoredMessages[i] = new ScoredMessage();
            }

            twoDigitHexValueSeperation = separateHexValueIntoPairs(message);
            decodeXORMessage();
        }
        /// <summary>
        /// Decodes the XOR'd hex string against each letter in the
        /// alphabet.
        /// </summary>
        /// <param name="letterRawByte">Raw byte representation of letter</param>
        /// <param name="key">char representation of letter</param>
        /// <returns>Scored Message object with decoded data</returns>
        private ScoredMessage decodeXORCalc(string letterRawByte, char key)
        {
            //Local variables
            string[]      decodedByte    = new string[rawByteHexPair.Length];
            string        decodedMessage = "";
            ScoredMessage sMsg           = new ScoredMessage();

            // loop through each hexPair and decode message with single
            // char from alphabet.
            for (int i = 0; i < rawByteHexPair.Length; i++)
            {
                int decodedDecimal = 0;

                for (int j = 0; j < 8; j++)
                {
                    // If the encoded bit is 1 the decoded bit is the opposite
                    // of the letter bit.
                    if (rawByteHexPair[i].ElementAt(j) == '1')
                    {
                        if (letterRawByte.ElementAt(j) == '1')
                        {
                            decodedByte[i] += "0";
                        }
                        else
                        {
                            decodedByte[i] += "1";
                        }
                    }
                    // If the encoded bit is 0 the decoded bit is the same as
                    // the letter bit.
                    else
                    {
                        decodedByte[i] += letterRawByte.ElementAt(j);
                    }
                }

                // Convert decoded byte to string and add to decoded message.
                decodedDecimal  = Convert.ToInt32((decodedByte[i]), 2);
                decodedMessage += Convert.ToChar(decodedDecimal);
            }

            // Populate ScoredMessage obj
            sMsg.Message = decodedMessage;
            sMsg.Score   = score.scoreMessage(decodedMessage);
            sMsg.Key     = key;

            return(sMsg);
        }