I think it's just ging to be something stupid that I've missed since the program itself runs, though doesn't solve the decrypted text. I've included the text from the decrypted file below the code and any help would be great.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Frequency_Analysis
{
    class Program
    {
        static void Main(string[] args)
        {
            //array to hold the english language letters
            char[] aLetter = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 
                                 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };

            //variable that holds the amount of words in the text
            int wCount = 0;
            //Read the task_2 text file into lines array
            string[] lines = System.IO.File.ReadAllLines(@"task_2.txt");

            foreach (string line in lines)
            {
                //creates a new string builder for the string 'line'
                StringBuilder b = new StringBuilder(line);  

                int i = 0;
                //Prints the encrypted text to console screen
                Console.WriteLine("Encrypted text");
                Console.Write("\n" + line);
                Console.WriteLine("\n");

                //initializes a new instance of the KeyValuePair structure with the specified key and value
                foreach (KeyValuePair<char, int> letter in GetLetterCounts(line).OrderByDescending(letter => letter.Value))
                
                {
                    //Character checker 
                    if (char.IsLetterOrDigit(letter.Key))   
                    {
                        //prints out the letter and how many times its occured
                        Console.WriteLine("Letter " + letter.Key + " : " + letter.Value);
                        //replaces the current letter with the english letter equivalent
                        b.Replace(letter.Key, aLetter[i]);
 
                        i++;
                    }

                    //checks for spacing
                    else if (letter.Key == ' ')
                    {
                        //puts the values of the letter into word count
                        wCount = letter.Value;   
                    }
                }

                //prints out the amount of words to the console
                Console.Write("\nTotal Words: " + wCount);    
                Console.WriteLine("\n");      
                //prints the string builders to console
                Console.WriteLine(b + "\n");

            }

            //keep window open til user is finished
            Console.WriteLine("Press any key to finish");
            System.Console.ReadKey();
        }

        //create dictionary function
        static Dictionary<char, int> GetLetterCounts(string myString)   
        {
            //Contains char and intager
            Dictionary<char, int> letter = new Dictionary<char, int>(); 
            //loop to check each character inside the string
            foreach (char c in myString)

                //checks to see if character is within
                if (letter.ContainsKey(c))
                {
                    //increment myDict character by one
                    letter[c]++;    
                }

                else
                {
                    //makes the characters have a count of one, if its sent to 0 it undercounts the letters and words
                    letter[c] = 1;  
                }

            //Returns the value
            return letter;
        }
    }
}

BERMN GWNDN BUUBANOG WZNARTIVUWZHD PBD B QZTJAN RQ NMZKNOGIV UZHGRAZBI ZOGNOG, GWRJTW ZGD ZLUANDDZROZDGZH NSNHJGZRO QRAEBKN B MNAV HINBA ZKNB RQ ZGD OBGJAN. ZG DNNLNK GR EN B DRAG RQ LRODGNA, RA DVLERI ANUANDNOGZOT B LRODGNA, RQ B
QRAL PWZHW ROIV B KZDNBDNK QBOHV HRJIK HROHNZMN. ZQ Z DBV GWBG LV DRLNPWBG NSGABMBTBOG ZLBTZOBGZRO VZNIKNK DZLJIGBONRJD UZHGJAND RQ BO RHGRUJD, B KABTRO, BOK B WJLBO HBAZHBGJAN, Z DWBII ORG EN JOQBZGWQJI GR GWN DUZAZG RQ GWN GWZOT. B UJIUV, GNOGBHINK WNBK DJALRJOGNK B TARGNDXJN BOK DHBIV ERKV PZGW AJKZLNOGBAV PZOTD, EJG ZG PBD GWN
TNONABI RJGIZON RQ GWN PWRIN PWZHW LBKN ZG LRDG DWRHFZOTIV QAZTWGQJI.

Hey,

You have not included the frequency analysis itself - Just the alphabet array.

For example:

char[] aLetter = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 
                                 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };

Needs to have the frequencies (1.4, 3.2, 2.2) etc and then ordered by the most frequent.

Hope this helps

To what are you comparing it to decrypt it?

To what are you comparing it to decrypt it?

how do you mean?

Do you mean reorganise the letter array into the most frequent? I've re done it to that now of:

char[] aLetter ={ 'e', 't', 'a', 'i', 'o', 'n', 's', 'r', 'h', 'u', 'l', 'c',
  'd', 'f', 'm', 'y', 'g', 'p', 'w', 'b', 'v', 'x', 'q', 'k', 'j', 'z' };

Do you mean reorganise the letter array into the most frequent? I've re done it to that now of:

char[] aLetter ={ 'e', 't', 'a', 'i', 'o', 'n', 's', 'r', 'h', 'u', 'l', 'c',
  'd', 'f', 'm', 'y', 'g', 'p', 'w', 'b', 'v', 'x', 'q', 'k', 'j', 'z' };

In essence, the way you've done it is "Hardcoded" and they should be sorted by means of like LINQ and not just a pre-defined array (This will make you pass your assignment, however, is not really a good way to program)

Your array should contain something like:

A = 8.2, B = 2.3 etc.. And then sorted that way.

Doing the method you've done, have you tried to match the the two values together? For example:

Alphabet: E T A I O N S R H U I C D F M Y G P W B V X Q K J Z
Ciphertext: C Y X S M P E T U V Q L I H K W J A D O R Z B G N

?


Your array should contain something like:

A = 8.2, B = 2.3 etc.. And then sorted that way.

Doing the method you've done, have you tried to match the the two values together? For example:

Alphabet: E T A I O N S R H U I C D F M Y G P W B V X Q K J Z
Ciphertext: C Y X S M P E T U V Q L I H K W J A D O R Z B G N

?

Thats what was going for, what would be your suggestion then? Going with the frequency the letter appears in the document or in the english alphabet for the array.

I would personally go with the one you've gone for.. I.e:

char[] aLetter ={ 'e', 't', 'a', 'i', 'o', 'n', 's', 'r', 'h', 'u', 'l', 'c', 'd', 'f', 'm', 'y', 'g', 'p', 'w', 'b', 'v', 'x', 'q', 'k', 'j', 'z' };

Do the text analysis on the text file.

I think Thines01 can implement a way of switching the letters =)

@phorce is Right.

Also, it might be easier if your first action on the data is to read it as an array (or List) of char rather than strings, since the decode will be done one char at a time.

Also, in your loop, you check to see if a character is a letter or digit or space. What if it is punctuation? Do you skip it or print it?

punctuation is printed to console screen, I've managed to get it working now by editing the text file into one line instead of multiple, though I would still like to learn. will try changing the string load to char

Does the text completely decrypt now?

This page has a decrypter program on it you can use for testing. It won't help with the code, but it's interesting to see.

yeah the text has completely decrypted and readable. And thanks for all the help!

No problem :) If this thread is solved, please mark it and give reputation points to those who helped!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.