OpenSSL AES_cfb128_encrypt C ++

I tried to implement a “very” simple example of encryption / decryption. I need this for a project where I would like to encrypt some user information. I can not encrypt the entire database, but only some fields in the table.

Working with the database and most of the rest of the project, with the exception of encryption: Here is a simplified version:

#include <openssl/aes.h>
#include <openssl/evp.h>
#include <iostream>
#include <string.h>

using namespace std;

int main()
{
    /* ckey and ivec are the two 128-bits keys necessary to
       en- and recrypt your data.  Note that ckey can be
       192 or 256 bits as well
     */

    unsigned char ckey[] =  "helloworldkey";
    unsigned char ivec[] = "goodbyworldkey";

    int bytes_read;
    unsigned char indata[AES_BLOCK_SIZE];
    unsigned char outdata[AES_BLOCK_SIZE];
    unsigned char decryptdata[AES_BLOCK_SIZE];

    /* data structure that contains the key itself */
    AES_KEY keyEn;

    /* set the encryption key */
    AES_set_encrypt_key(ckey, 128, &keyEn);

    /* set where on the 128 bit encrypted block to begin encryption*/
    int num = 0;

    strcpy( (char*)indata , "Hello World" );
    bytes_read = sizeof(indata);

    AES_cfb128_encrypt(indata, outdata, bytes_read, &keyEn, ivec, &num, AES_ENCRYPT);
    cout << "original data:\t" << indata << endl;
    cout << "encrypted data:\t" << outdata << endl;

    AES_cfb128_encrypt(outdata, decryptdata, bytes_read, &keyEn, ivec, &num, AES_DECRYPT);
    cout << "input data was:\t" << decryptdata << endl;
    return 0;
}

But the output of the "decrypted" data is some random characters, but they are the same after each code execution. outdatachanges every time ...

I tried to debug and look for a solution, but I could not find a solution for my problem.
Now my question is, what's going on here? Or am I completely misunderstanding the provided functions?

+4
1

, AES_cfb128_encrypt ivec ( ). , ivec AES_cfb128_encrypt :

const char ivecstr[AES_BLOCK_SIZE] = "goodbyworldkey\0";
unsigned char ivec[AES_BLOCK_SIZE];
memcpy( ivec , ivecstr, AES_BLOCK_SIZE);

memcpy AES_cfb128_encrypt.

1:. , \0 . , .

2: , , strcpy . .

+7

Source: https://habr.com/ru/post/1539029/


All Articles