使用QT、Openssl加密任务崩溃怎么办?

Encryption task crashed using QT, Openssl what should i do?

下面的函数在 Qt Creator 上运行时出现以下错误

/home/zero/build-EncryptionTask2-Desktop_Qt_5_7_0_GCC_64bit-Debug/EncryptionTask2 crashed.

我认为它来自转换 to/from QByteArray 数据类型

QByteArray encryotionProgress(QByteArray d_input)
{
//QByteArray d_output;
params->key = key;
params->iv  = iv;
/* Indicate that we want to encrypt */
   params->encrypt = 1;
/* Set the cipher type you want for encryption-decryption */
  params->cipher_type = EVP_aes_256_cbc();

 /* Encrypt the given file */
   unsigned char *enc_out;

   AES_set_encrypt_key( params->key, 256, &enc_key);
   AES_encrypt((unsigned char*)(d_input.data()), enc_out, &enc_key);
   QByteArray d_output((char*)enc_out);//convert back to qbyte array

  return d_output;
 }

在传递给函数 AES_encrypt 之前,必须将内存分配给 enc_out

首先,获取输出缓冲区的大小:

const int n = sizeof(unsigned char) * ((d_input.length() + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;

然后使用以下任一方式为enc_out分配内存:

C++ 风格的动态内存分配:

unsigned char* enc_out = new unsigned char[n];

C 风格动态内存分配:

unsigned char* enc_out = (unsigned char *) malloc (n);

注意:使用后不要忘记freedelete[]内存分配给enc_out

堆栈内存分配:

unsigned char  enc_out[n];

希望对您有所帮助。