2015-12-21 163 views
0

我一直在花時間學習更多關於加密和一般的密碼。我開始研究一個控制檯應用程序,以允許我加密或解密RSA類型的密碼。C++未處理的異常:std :: bad_alloc

到目前爲止,除了當我在要加密的字符串中包含字符「n」時,所有內容似乎都運行良好。出於某種原因,只有字母「n」強制應用程序中止。如果它本身是一串其他字符中的「n」,則無關緊要。它只是不喜歡「n」。

我仍然是一名學生,當談到編碼時,這只是我在C++中的第二個應用程序。

的錯誤:

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

我當前的代碼:

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

可能不在您發佈的代碼中。如果你想幫助解決你的代碼問題,你真的需要發佈一個可以被「任何人」編譯和運行的完整示例。 –

+0

順便說一句,'bad_alloc'意味着你的系統不能滿足'new'的內存分配。要麼因爲你要求瘋狂的內存量,要麼永遠分配新的內存。 –

+0

'encrKey [1]'是14和''n''是第14個字母之間有一些聯繫。這可能會導致'letterArray [encrNum - 1]'的超出範圍的訪問。 –

回答

0

所以被作爲@molbdnilo提到的問題...

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.

幸運的是,一個真正的交易時鍵而不是臨時簡單的鍵,這種衝突不會發生,因爲我們將要處理2048位或4096位密鑰。但是,有趣的是,當模數返回0作爲餘數時會出現問題。