Openssl Qt c++ Encryption

Multi tool use
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());
c++ qt openssl
add a comment |
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());
c++ qt openssl
add a comment |
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());
c++ qt openssl
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
c++ qt openssl
asked Nov 10 at 17:06
user3602386
32
32
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
EabfZULyiAHQ1yBY90k b6YfvauPyC