简体   繁体   中英

C++ Unhandled Exception: std::bad_alloc

I've been taking my time to learn more about encryption and just ciphers in general. I started working on a console application to allow me to encrypt or decrypt RSA type ciphers.

So far everything seems to be working well, except for when I include the character "n" within the string that I want to encrypt. For some reason only the letter "n" forces the application to abort. It doesn't matter if it's "n" by itself of within a string of other characters. It just doesn't like "n".

I'm still a student when it comes to coding and this is only my second application within C++.

The Error:

Unhandled exception at 0x7743C42D in RSA Cipher.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0020DE98.

My current code:

#include <iostream>
#include <sstream>
#include <algorithm>
#include <math.h>

using namespace std;

int main()
{
    //GLOBAL_VARS
    string mainInput;
    string aboutInput;
    string encrInput;
    int encrKey[2] = {5, 14}; //Temp Key

    const int nValues = 26; //How many values within the array
    string letterArray[nValues] = { "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"}; //Array of letters
    string capsArray[nValues] = { "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"}; //Array of capital letters

    //MAIN_MENU
    while ((mainInput != "Exit") || (mainInput != "exit") || (mainInput != "4")) 
    {
        cout << "\n---RSA Cipher [Main Menu]---\n\n"
             << "1: About\n"
             << "2: Encrypt\n"
             << "3: Decrypt\n"
             << "4: Exit\n\n";

        cin >> mainInput;

        //ABOUT_MENU
        if ((mainInput == "about") || (mainInput == "About") || (mainInput == "1")) 
        {
            cout << "\n---RSA Cipher [About]---\n\n"
                 << "This applicaton was designed to encrypt or decrypt information using RSA encryption.\n\n"
                 << "1: Back\n\n";

            cin >> aboutInput;
        }
        else {
            //ENCRYPT_MENU
            int encrLength;
            int encrNum;
            string encrGet;
            string encrOutput;

            if ((mainInput == "Encrypt") || (mainInput == "encrypt") || (mainInput == "2")) 
            {
                cout << "\n---RSA Cipher [Encrypt]---\n\n"
                     << "Enter a string to encrypt.\n\n";

                cin >> encrInput;

                encrLength = encrInput.length(); //Sets the variable to equal the length of the users input so that both items being compared are signed.

                int iLength = 0;
                while (iLength < encrLength) 
                {
                    encrGet = encrInput[iLength]; //Grabs a value of the entered string in order

                    int iIndex = 0;
                    while (iIndex < nValues) 
                    {   
                        if ((encrGet == letterArray[iIndex]) || (encrGet == capsArray[iIndex]))
                        {
                            encrNum = pow(iIndex + 1, encrKey[0]); //Sets the variable to equal array index + 1 (the alphabet starts at 1 not 0) to the power of the first encrKey value
                            encrNum = encrNum % encrKey[1]; //Sets the variable to equal it'self mod the second encrKey value

                            encrOutput = encrOutput + letterArray[encrNum - 1]; //Adds the letters to the soon to be output -1, as we are now dealing with computer logic.

                        }

                        iIndex++;
                    }
                    iLength++;
                }
                cout << "Encrypted: " << encrOutput << endl;
            }
        }
    }
    //END-MAIN_MENU
}

So the problem was as @molbdnilo mentioned...

The problem occurs when encrNum % encrKey[1] is zero, which will happen whenever encrNum == encrKey[1]. It looks like you have a conflict between zero-based and one-based indexing.

Fortunately, when dealing with a real key and not a temporary simple one, this conflict will not occur as we would be dealing with 2048 bit or 4096 bit keys. However, it is interesting that a problem occurs when the modulo returns 0 as a remainder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM