簡體   English   中英

C ++未處理的異常:std :: bad_alloc

[英]C++ Unhandled Exception: std::bad_alloc

我一直在花時間來學習有關加密的更多知識,以及一般而言的密碼。 我開始在控制台應用程序上工作,以允許我加密或解密RSA類型密碼。

到目前為止,除了我要加密的字符串中包含字符“ n”時,其他所有內容似乎都運行良好。 由於某些原因,只有字母“ n”會強制應用程序中止。 本身是否在其他字符串中是否為“ n”並不重要。 它只是不喜歡“ n”。

我仍然是編碼方面的學生,這只是我在C ++中的第二個應用程序。

錯誤:

RSA Cipher.exe中0x7743C42D的未處理異常:Microsoft C ++異常:內存位置0x0020DE98的std :: bad_alloc。

我當前的代碼:

#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
}

所以問題就像@molbdnilo提到的...

當encrNum%encrKey [1]為零時,會發生此問題,只要encrNum == encrKey [1]就會發生。 看起來您在基於零的索引和基於一的索引之間存在沖突。

幸運的是,當處理一個真正的密鑰而不是一個臨時的簡單密鑰時,不會發生這種沖突,因為我們將處理2048位或4096位密鑰。 然而,有趣的是,當模返回0作為余數時,會出現問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM