Skip navigation.
Home

C++ code for Generating Frequencies in Cryptoanalysis

Often there are ciphers called "substitution" ciphers - where one substitutes one letter for another.  This a bit different from the Caesarian Cipher as those substitutions are marked distance from each other.  We are talking about an "a" matching a "m," a "z" matching for a "n," and so on.

The problem is, the structure used is still using the same letter distribution frequencies.  An "e," no matter the letter used, will still show up the most times - as it is the most used letter in English (or another letter for whatever language is being used.)

Thus, all one needs to do is make a frequency analysis chart of the cipher text and some wording - then start matching!  Now there is hardships, for example the cipher text is very small - but there are ways around that too.  Perhaps a future article!

The following code creates such a frequency analysis.  It can be run on the cipher code and on the plain text (I use a wikipedia article often for what I think it will be about).  Then start matching the most commonly occuring letter in the cipher text to the most commonly occuring letter in the sample plain text.

// Frequency Attack: Generate frequencies, C++ Code
// (C) Feb 17 2014 Scott Auge scott_auge@yahoo.com
// Amduus Information Works, Inc.
// OS - OS X 10.6.8
// Capitals are used!

#include <iostream>
#include <fstream>
#include <string>
#include <map>

using namespace std;

int main(int argc, char * const argv[]) {
    
    // Our first argument is the analysis file
    
    typedef map<char,int> DefFrequencyMap;
    char c;
    
    DefFrequencyMap FrequencyMap;
    
    cout << "Generate Frequency Analysis" << endl;
    
    ifstream TheFile(argv[1]);
    
    if (! TheFile) {
        cout << "Unable to open " << argv[1] << endl;
        exit(1);
    }
    
    while (TheFile.get(c)) {
        
        if (isspace(c) || ispunct(c)) continue;
        
        FrequencyMap[c]++;
        
    } // while
    
    TheFile.close();
    
    //
    // Output our results
    //
    
    ofstream OutFile ("Freq.dat");
    
    DefFrequencyMap::iterator Position;
    
    for (Position = FrequencyMap.begin(); Position != FrequencyMap.end(); ++Position) {
        
        OutFile << Position->first << " " << Position->second << endl;
        
    } // for
    
    OutFile.close();
    
} // main