Openssl Qt c++ Encryption









up vote
-1
down vote

favorite












I wrote a small program in Qt using OpenSSL following the wiki of OpenSSL the first part of the program which is the encryption part runs fine but the decryption part of the program throws a runtime error and the debug message says "Invalid parameter passed to C runtime function.", I can't figure out what I am doing wrong and I can't test the code to make sure that the encryption part is right.
Can you please take a look at the code and tell me what I am doing wrong :



void Encryption::Encrypt(const QByteArray Key, const QByteArray IV, const QString &PlainText, QString &CipherText)

EVP_CIPHER_CTX_ptr ctx(EVP_CIPHER_CTX_new(), ::EVP_CIPHER_CTX_free);
int rc = EVP_EncryptInit_ex(ctx.get(), EVP_aes_256_cbc(), NULL, (const unsigned char*)Key.data(), (const unsigned char*)IV.data());
if (rc != 1)
throw std::runtime_error("EVP_EncryptInit_ex failed");

// Cipher text expands up to BLOCK_SIZE
CipherText.resize(PlainText.length()+BLOCK_SIZE);
int out_len1 = CipherText.length();


rc = EVP_EncryptUpdate(ctx.get(), (unsigned char*)&CipherText.data()[0], &out_len1, (unsigned char*)&PlainText.data()[0], PlainText.size());
if (rc != 1)
throw std::runtime_error("EVP_EncryptUpdate failed");

int out_len2 = (int)CipherText.size() - out_len1;
rc = EVP_EncryptFinal_ex(ctx.get(), (unsigned char*)&CipherText.data()[0]+out_len1, &out_len2);
if (rc != 1)
throw std::runtime_error("EVP_EncryptFinal_ex failed");

// Set cipher text size now that we know it
CipherText.resize(out_len1 + out_len2);




void Encryption::Decrypt(const QByteArray Key, const QByteArray IV, QString &PlainText, QString &CipherText)

EVP_CIPHER_CTX_ptr ctx(EVP_CIPHER_CTX_new(), ::EVP_CIPHER_CTX_free);



/* Initialise the decryption operation. IMPORTANT - ensure you use a key
* and IV size appropriate for your cipher
* In this example we are using 256 bit AES (i.e. a 256 bit key). The
* IV size for *most* modes is the same as the block size. For AES this
* is 128 bits */
int rc= EVP_DecryptInit_ex(ctx.get(), EVP_aes_256_cbc(), NULL, (const unsigned char*)Key.data(), (const unsigned char*)IV.data());
if (rc != 1)
throw std::runtime_error("EVP_DecryptInit_ex failed");

int out_len1 = (int)CipherText.size();

/* Provide the message to be decrypted, and obtain the plaintext output.
* EVP_DecryptUpdate can be called multiple times if necessary
*/
rc= EVP_DecryptUpdate(ctx.get(), (unsigned char*)&PlainText.data()[0], &out_len1, (unsigned char*)&CipherText.data()[0], PlainText.size());
if (rc != 1)
throw std::runtime_error("EVP_DecryptUpdate failed");



/* Finalise the decryption. Further plaintext bytes may be written at
* this stage.
*/
qDebug()<< "here";
rc= EVP_DecryptFinal_ex(ctx.get(), (unsigned char*)&PlainText.data()[0]+out_len1, &out_len1);
if (rc != 1)
throw std::runtime_error("EVP_EncryptFinal_ex failed");
qDebug()<< "here";

// Set cipher text size now that we know it
PlainText.resize(out_len1 + CipherText.length());













share|improve this question

























    up vote
    -1
    down vote

    favorite












    I wrote a small program in Qt using OpenSSL following the wiki of OpenSSL the first part of the program which is the encryption part runs fine but the decryption part of the program throws a runtime error and the debug message says "Invalid parameter passed to C runtime function.", I can't figure out what I am doing wrong and I can't test the code to make sure that the encryption part is right.
    Can you please take a look at the code and tell me what I am doing wrong :



    void Encryption::Encrypt(const QByteArray Key, const QByteArray IV, const QString &PlainText, QString &CipherText)

    EVP_CIPHER_CTX_ptr ctx(EVP_CIPHER_CTX_new(), ::EVP_CIPHER_CTX_free);
    int rc = EVP_EncryptInit_ex(ctx.get(), EVP_aes_256_cbc(), NULL, (const unsigned char*)Key.data(), (const unsigned char*)IV.data());
    if (rc != 1)
    throw std::runtime_error("EVP_EncryptInit_ex failed");

    // Cipher text expands up to BLOCK_SIZE
    CipherText.resize(PlainText.length()+BLOCK_SIZE);
    int out_len1 = CipherText.length();


    rc = EVP_EncryptUpdate(ctx.get(), (unsigned char*)&CipherText.data()[0], &out_len1, (unsigned char*)&PlainText.data()[0], PlainText.size());
    if (rc != 1)
    throw std::runtime_error("EVP_EncryptUpdate failed");

    int out_len2 = (int)CipherText.size() - out_len1;
    rc = EVP_EncryptFinal_ex(ctx.get(), (unsigned char*)&CipherText.data()[0]+out_len1, &out_len2);
    if (rc != 1)
    throw std::runtime_error("EVP_EncryptFinal_ex failed");

    // Set cipher text size now that we know it
    CipherText.resize(out_len1 + out_len2);




    void Encryption::Decrypt(const QByteArray Key, const QByteArray IV, QString &PlainText, QString &CipherText)

    EVP_CIPHER_CTX_ptr ctx(EVP_CIPHER_CTX_new(), ::EVP_CIPHER_CTX_free);



    /* Initialise the decryption operation. IMPORTANT - ensure you use a key
    * and IV size appropriate for your cipher
    * In this example we are using 256 bit AES (i.e. a 256 bit key). The
    * IV size for *most* modes is the same as the block size. For AES this
    * is 128 bits */
    int rc= EVP_DecryptInit_ex(ctx.get(), EVP_aes_256_cbc(), NULL, (const unsigned char*)Key.data(), (const unsigned char*)IV.data());
    if (rc != 1)
    throw std::runtime_error("EVP_DecryptInit_ex failed");

    int out_len1 = (int)CipherText.size();

    /* Provide the message to be decrypted, and obtain the plaintext output.
    * EVP_DecryptUpdate can be called multiple times if necessary
    */
    rc= EVP_DecryptUpdate(ctx.get(), (unsigned char*)&PlainText.data()[0], &out_len1, (unsigned char*)&CipherText.data()[0], PlainText.size());
    if (rc != 1)
    throw std::runtime_error("EVP_DecryptUpdate failed");



    /* Finalise the decryption. Further plaintext bytes may be written at
    * this stage.
    */
    qDebug()<< "here";
    rc= EVP_DecryptFinal_ex(ctx.get(), (unsigned char*)&PlainText.data()[0]+out_len1, &out_len1);
    if (rc != 1)
    throw std::runtime_error("EVP_EncryptFinal_ex failed");
    qDebug()<< "here";

    // Set cipher text size now that we know it
    PlainText.resize(out_len1 + CipherText.length());













    share|improve this question























      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite











      I wrote a small program in Qt using OpenSSL following the wiki of OpenSSL the first part of the program which is the encryption part runs fine but the decryption part of the program throws a runtime error and the debug message says "Invalid parameter passed to C runtime function.", I can't figure out what I am doing wrong and I can't test the code to make sure that the encryption part is right.
      Can you please take a look at the code and tell me what I am doing wrong :



      void Encryption::Encrypt(const QByteArray Key, const QByteArray IV, const QString &PlainText, QString &CipherText)

      EVP_CIPHER_CTX_ptr ctx(EVP_CIPHER_CTX_new(), ::EVP_CIPHER_CTX_free);
      int rc = EVP_EncryptInit_ex(ctx.get(), EVP_aes_256_cbc(), NULL, (const unsigned char*)Key.data(), (const unsigned char*)IV.data());
      if (rc != 1)
      throw std::runtime_error("EVP_EncryptInit_ex failed");

      // Cipher text expands up to BLOCK_SIZE
      CipherText.resize(PlainText.length()+BLOCK_SIZE);
      int out_len1 = CipherText.length();


      rc = EVP_EncryptUpdate(ctx.get(), (unsigned char*)&CipherText.data()[0], &out_len1, (unsigned char*)&PlainText.data()[0], PlainText.size());
      if (rc != 1)
      throw std::runtime_error("EVP_EncryptUpdate failed");

      int out_len2 = (int)CipherText.size() - out_len1;
      rc = EVP_EncryptFinal_ex(ctx.get(), (unsigned char*)&CipherText.data()[0]+out_len1, &out_len2);
      if (rc != 1)
      throw std::runtime_error("EVP_EncryptFinal_ex failed");

      // Set cipher text size now that we know it
      CipherText.resize(out_len1 + out_len2);




      void Encryption::Decrypt(const QByteArray Key, const QByteArray IV, QString &PlainText, QString &CipherText)

      EVP_CIPHER_CTX_ptr ctx(EVP_CIPHER_CTX_new(), ::EVP_CIPHER_CTX_free);



      /* Initialise the decryption operation. IMPORTANT - ensure you use a key
      * and IV size appropriate for your cipher
      * In this example we are using 256 bit AES (i.e. a 256 bit key). The
      * IV size for *most* modes is the same as the block size. For AES this
      * is 128 bits */
      int rc= EVP_DecryptInit_ex(ctx.get(), EVP_aes_256_cbc(), NULL, (const unsigned char*)Key.data(), (const unsigned char*)IV.data());
      if (rc != 1)
      throw std::runtime_error("EVP_DecryptInit_ex failed");

      int out_len1 = (int)CipherText.size();

      /* Provide the message to be decrypted, and obtain the plaintext output.
      * EVP_DecryptUpdate can be called multiple times if necessary
      */
      rc= EVP_DecryptUpdate(ctx.get(), (unsigned char*)&PlainText.data()[0], &out_len1, (unsigned char*)&CipherText.data()[0], PlainText.size());
      if (rc != 1)
      throw std::runtime_error("EVP_DecryptUpdate failed");



      /* Finalise the decryption. Further plaintext bytes may be written at
      * this stage.
      */
      qDebug()<< "here";
      rc= EVP_DecryptFinal_ex(ctx.get(), (unsigned char*)&PlainText.data()[0]+out_len1, &out_len1);
      if (rc != 1)
      throw std::runtime_error("EVP_EncryptFinal_ex failed");
      qDebug()<< "here";

      // Set cipher text size now that we know it
      PlainText.resize(out_len1 + CipherText.length());













      share|improve this question













      I wrote a small program in Qt using OpenSSL following the wiki of OpenSSL the first part of the program which is the encryption part runs fine but the decryption part of the program throws a runtime error and the debug message says "Invalid parameter passed to C runtime function.", I can't figure out what I am doing wrong and I can't test the code to make sure that the encryption part is right.
      Can you please take a look at the code and tell me what I am doing wrong :



      void Encryption::Encrypt(const QByteArray Key, const QByteArray IV, const QString &PlainText, QString &CipherText)

      EVP_CIPHER_CTX_ptr ctx(EVP_CIPHER_CTX_new(), ::EVP_CIPHER_CTX_free);
      int rc = EVP_EncryptInit_ex(ctx.get(), EVP_aes_256_cbc(), NULL, (const unsigned char*)Key.data(), (const unsigned char*)IV.data());
      if (rc != 1)
      throw std::runtime_error("EVP_EncryptInit_ex failed");

      // Cipher text expands up to BLOCK_SIZE
      CipherText.resize(PlainText.length()+BLOCK_SIZE);
      int out_len1 = CipherText.length();


      rc = EVP_EncryptUpdate(ctx.get(), (unsigned char*)&CipherText.data()[0], &out_len1, (unsigned char*)&PlainText.data()[0], PlainText.size());
      if (rc != 1)
      throw std::runtime_error("EVP_EncryptUpdate failed");

      int out_len2 = (int)CipherText.size() - out_len1;
      rc = EVP_EncryptFinal_ex(ctx.get(), (unsigned char*)&CipherText.data()[0]+out_len1, &out_len2);
      if (rc != 1)
      throw std::runtime_error("EVP_EncryptFinal_ex failed");

      // Set cipher text size now that we know it
      CipherText.resize(out_len1 + out_len2);




      void Encryption::Decrypt(const QByteArray Key, const QByteArray IV, QString &PlainText, QString &CipherText)

      EVP_CIPHER_CTX_ptr ctx(EVP_CIPHER_CTX_new(), ::EVP_CIPHER_CTX_free);



      /* Initialise the decryption operation. IMPORTANT - ensure you use a key
      * and IV size appropriate for your cipher
      * In this example we are using 256 bit AES (i.e. a 256 bit key). The
      * IV size for *most* modes is the same as the block size. For AES this
      * is 128 bits */
      int rc= EVP_DecryptInit_ex(ctx.get(), EVP_aes_256_cbc(), NULL, (const unsigned char*)Key.data(), (const unsigned char*)IV.data());
      if (rc != 1)
      throw std::runtime_error("EVP_DecryptInit_ex failed");

      int out_len1 = (int)CipherText.size();

      /* Provide the message to be decrypted, and obtain the plaintext output.
      * EVP_DecryptUpdate can be called multiple times if necessary
      */
      rc= EVP_DecryptUpdate(ctx.get(), (unsigned char*)&PlainText.data()[0], &out_len1, (unsigned char*)&CipherText.data()[0], PlainText.size());
      if (rc != 1)
      throw std::runtime_error("EVP_DecryptUpdate failed");



      /* Finalise the decryption. Further plaintext bytes may be written at
      * this stage.
      */
      qDebug()<< "here";
      rc= EVP_DecryptFinal_ex(ctx.get(), (unsigned char*)&PlainText.data()[0]+out_len1, &out_len1);
      if (rc != 1)
      throw std::runtime_error("EVP_EncryptFinal_ex failed");
      qDebug()<< "here";

      // Set cipher text size now that we know it
      PlainText.resize(out_len1 + CipherText.length());










      c++ qt openssl






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 17:06









      user3602386

      32




      32



























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241340%2fopenssl-qt-c-encryption%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241340%2fopenssl-qt-c-encryption%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          ReactJS Fetched API data displays live - need Data displayed static

          政党