Неправильное шифрование (QT c ++ OpenSSL AES 256 CBC)

Я написал небольшую программу для шифрования и дешифрования данных с использованием OpenSSL AES 256 CBC, но после расшифровки зашифрованных данных у меня закончились ненужные данные, поэтому в коде должно быть что-то не так, но я не смог понять, можете ли вы взглянуть на код и скажи мне, что я сделал не так:

Основная функция :

  /* Message to be encrypted */
unsigned char* plaintext =
(unsigned char*)"The quick brown fox jumps over the lazy dog";

EVP_CIPHER_CTX *en, *de;
en = EVP_CIPHER_CTX_new();
de = EVP_CIPHER_CTX_new();

/* 8 bytes to salt the key_data during key generation. This is an example of
compiled in salt. We just read the bit pattern created by these two 4 byte
integers on the stack as 64 bits of contigous salt material -
ofcourse this only works if sizeof(int) >= 4 */
unsigned int salt[] = {12345, 54321};
unsigned char *key_data;
int key_data_len, i;

key_data = (unsigned char*)"01234567890123456789012345678901";
key_data_len = strlen((const char*)key_data);

Encryption AES;

/* gen key and iv. init the cipher ctx object */
if (AES.Init(key_data, key_data_len, (unsigned char *)&salt, en, de)) {
qDebug() << "Couldn't initialize AES cipher ";
}

unsigned char *ciphertext;
int olen, len;

/* The enc/dec functions deal with binary data and not C strings. strlen() will
return length of the string without counting the '\0' string marker. We always
pass in the marker byte to the encrypt/decrypt functions so that after decryption
we end up with a legal C string */
olen = len = strlen((const char*)plaintext)+1;ciphertext = AES.Encrypt(en, plaintext, &len);
plaintext = (unsigned char*)"";
plaintext = AES.Decrypt(de, ciphertext, &len);

QString s;
QString result = "";
int rev = strlen((const char*)ciphertext);

for ( int i = 0; i<rev; i++)
{
s = QString("%1").arg(ciphertext[i],0,16);

if(s == "0"){
s="00";
}
result.append(s);

}

ui->label_7->setText(result);

rev = len;
result = "";
for ( int i = 0; i<rev; i++)
{
s = QString("%1").arg(plaintext[i],0,16);

if(s == "0"){
s="00";
}
result.append(s);

}

ui->label_8->setText(result);

free(ciphertext);
free(plaintext);

EVP_CIPHER_CTX_free(en);
EVP_CIPHER_CTX_free(de);

Функция инициализации:

Функция инициализации

Функция шифрования:

Функция шифрования

Функция расшифровки:

Функция расшифровки

Извините, если я загрузил изображения кода, потому что я не мог добавить еще один блок кода здесь, я не знаю, почему я попытался вставить другой блок кода, и это выглядело как обычный текст, не знаю почему ?!

Любая помощь будет принята с благодарностью.
Заранее спасибо.

-4

Решение

Наконец, я понял, в чём была проблема при отображении зашифрованных и дешифрованных строк, когда я попробовал result = QString :: fromLocal8Bit ((char *) ciphertext);
он работал нормально и показал оригинальный текст.

0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector