I am a relative newbie to C++. I recently finished a command line program which cracks Vigenere encoded messages from a file by testing each word in the dictionary.
I have never had a tutor/teacher for C++, so I wanted to make sure that I don't develop any bad habits.
Main.cpp:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include "Alphanumeric.h" //turns characters into numerical equivalents
#include "Vigenere.h" //Vigenere Functions
#include <list>
#ifdef __APPLE__
#define DICTPATH "/usr/share/dict/words"
#endif
int main(int argc, char* argv[])
{
std::cout << "*************VIGENERE BREAKER*************" << std::endl;
std::ifstream input(argv[1]);
std::ifstream dictionary(DICTPATH);
std::string process = argv[2];
std::string key = argv[3];
std::string plain;
std::string str;
while (std::getline(input, str))
{
plain += str;
}
if (process == "-e")
{
std::cout << "*************ENCODE*************" << std::endl;
std::string result = Vigenere::EncryptVigenere(plain, key);
std::cout << result << std::endl;
}
if (process =="-d")
{
std::cout << "*************DECODE*************" << std::endl;
std::string result = Vigenere::DecryptVigenere(plain, key);
std::cout << result << std::endl;
}
if (process == "-dicf")
{
const float englishfreq[] = {8.2,1.5,2.8,4.3,12.7,2.2,2,6.1,7,0.2,0.8,4,2.4,6.7,7.5,1.9,0.1,6,6.3,9.1,2.7,1,2.4,0.15,1.9,0.074};
const int threshold = atoi(argv[3]);
std::cout << "*************DICTIONARY_ATTACK*************" << std::endl;
std::list<std::string> results;
while (std::getline(dictionary, str))
{
float total = 0;
float frequency[26] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
std::cout << str << std::endl;
std::string result = Vigenere::DecryptVigenere(plain, str);
for (char c : result)
{
frequency[Alphanumeric::chartoint(c)]++;
}
int size = result.size();
int index = 0;
for (float freq : frequency)
{
freq = (freq / size)*100;
float difference = freq - englishfreq[index];
if (difference < 0)
{
difference *= -1;
}
total += difference;
index++;
}
if (total <= threshold) {
std::cout << "RESULT FOUND" << std::endl;
results.push_back("Using " + str);
results.push_back(result);
}
}
std::cout << "*************RESULTS*************" << std::endl;
for (std::string s : results)
{
std::cout << s << std::endl;
}
std::cout << "dictionary attack complete!!!" << std::endl;
}
if (process == "-dic")
{
std::cout << "*************DICTIONARY_ATTACK*************" << std::endl;
std::list<std::string> results;
std::string keyword = argv[3];
while (std::getline(dictionary, str))
{
std::cout << "key:";
std::cout << str << std::endl;
std::string result = Vigenere::DecryptVigenere(plain, str);
if (result.find(keyword) != std::string::npos) {
std::cout << "RESULT FOUND" << std::endl;
results.push_back("Using " + str);
results.push_back(result);
}
}
std::cout << "*************RESULTS*************" << std::endl;
for (std::string s : results)
{
std::cout << s << std::endl;
}
std::cout << "dictionary attack complete!!!" << std::endl;
}
return 0;
}
Vigenere.h:
#ifndef VIGENERE_H_
#define VIGENERE_H_
namespace Vigenere
{
std::string DecryptVigenere(std::string ciphertext, std::string key);
std::string EncryptVigenere(std::string plaintext, std::string key);
}
#endif /* VIGENERE_H_ */
Vigenere.cpp:
#include <string>
#include "Vigenere.h"
#include "Alphanumeric.h"
std::string Vigenere::DecryptVigenere(std::string ciphertext, std::string key)
{
std::string answer;
for (int i = 0; i < ciphertext.length(); i++)
{
int numeric = Alphanumeric::chartoint(ciphertext[i]);
int keyval = Alphanumeric::chartoint(key[i%key.length()]);
numeric -= keyval;
if (numeric < 0)
{
numeric += 26;
}
numeric %= 26;
answer += Alphanumeric::inttochar(numeric);
}
return answer;
}
std::string Vigenere::EncryptVigenere(std::string plaintext, std::string key)
{
std::string answer;
for (int i = 0; i < plaintext.length(); i++)
{
int numeric = Alphanumeric::chartoint(plaintext[i]);
int keyval = Alphanumeric::chartoint(key[i % key.length()]);
numeric += keyval;
numeric %= 26;
answer += Alphanumeric::inttochar(numeric);
}
return answer;
}