凯撒密码解密者

时间:2013-03-11 06:05:27

标签: c++ encryption

好吧所以我一直在“任务”编写程序来解密简单的凯撒密码。

我的一切工作正常,只有我的输入停止了大约29个字符。它是从一个非常大的文件读取,文本停在:“独立声明”中句。有什么想法会导致这种情况吗?我假设我在这里滥用和滥用某些东西。请随意扔石头。我确信我可以使用更多的“学习”

编辑:经过进一步调查,我认为我的问题与我的循环和sizeof(myarray)有关

编辑2:我已经编辑了代码,现在因为“访问冲突读取位置0x002A4000”而中断了代码:

 for (int i = 0; i < myArray.length(); i++)
    {
        // pritns each occurance cout << char(i + 'a') << " has " << count_Array[i] <<" occuarnces"  <<endl;
        if (count_Array[i] > tester)
        {
            //finds largest 
            tester = count_Array[i];
            max_array_value = i;
        }
    }
    // print

Thanks for any tips :] 

code: 

   #include <iostream>
#include <string>
#include <cctype>
#include <fstream>
using namespace std;

int decipher(string myArray, string outputFileName);

int main ()
{ 
    string outputFileName;
    string inputFileName;
    ifstream inputFile;
    string reply;
    char myArray;
    string all_text = "";

    //getting input and output files
    cout << "Please input filename: ";
    getline(cin,inputFileName);
    cout <<"please enter output filename:";
    getline(cin,outputFileName);
    //opens file
    inputFile.open(inputFileName);

    if (!inputFile.is_open())
    {
        //file failed to open
        cout <<"unable to open input file." <<endl << "Press enter to continue...";
        getline(cin,reply);
        exit(1);
    }
    //read file into all_text
    while(inputFile.peek() != EOF)
    {
        inputFile.get(myArray);
            all_text+=myArray;

    }
    // prints out file cout <<all_text;
            inputFile.close();
            //send to decipher
            decipher(all_text, outputFileName);
    cout << "press enter to continue";
    getline(cin,reply);
    return 0;
}



int decipher(string myArray, string outputFileName)
{
    char default_alp[26] = {'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' };
    int count_Array[26] = { 0 };
    int tester = count_Array[0];
    int max_array_value = 0;
    string my_Message;
    char temp;
    ofstream outputFile;
    //gets count of occurances
    for (int i = 0; i < myArray.length(); i++)
    {
        count_Array[tolower(myArray[i]) - 'a']++;
    }

    for (int i = 0; i < myArray.length(); i++)
    {
        // pritns each occurance cout << char(i + 'a') << " has " << count_Array[i] <<" occuarnces"  <<endl;
        if (count_Array[i] > tester)
        {
            //finds largest 
            tester = count_Array[i];
            max_array_value = i;

        }
    }
    // prints useful information cout << "Largest number of occurances " << default_alp[max_array_value] <<endl << "shift amount is: " << default_alp[max_array_value]-'e' <<endl;
    for (int i = 0; i < myArray.length(); i++)
    {
//prints out each letter    cout << myArray[i];
    }

    for (int i = 0; i < myArray.length; i++)
    {
        cout <<myArray.length();
        //shifts the text based on value in dec
        int shift = default_alp[max_array_value]-'e';
        temp = myArray[i];
        if (isalpha(temp))
        {
            if(tolower(myArray[i])-shift >= 97)
            {
                my_Message += tolower(myArray[i]) - shift;
            }
            else
            {
                my_Message += tolower(myArray[i]) + 26-shift;
            }
        }
        else
        {
                my_Message +=myArray[i];
        }
    }
    outputFile.open(outputFileName);
    outputFile << my_Message;
    return 0;
}

2 个答案:

答案 0 :(得分:1)

您使用的是什么编译器。对我来说,它不能编译,因为

//打开文件     inputFile.open(查找inputfilename);

需要 //打开文件     inputFile.open(inputFileName.c_str());

http://www.cplusplus.com/reference/fstream/ifstream/open/

但是当它编译时,事情似乎正常。读取整个文件。

答案 1 :(得分:1)

decipher中,sizeof(myArray)为您提供string个实例的大小。 string实例可能包含字符串的实际长度,一些非常小的字符串空间和指向实际数据的指针,但它不包含数据,即字符串本身。

而不是sizeof(myArray),请使用myArray.length()

相关问题