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()
{
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];
AES_KEY keyEn;
AES_set_encrypt_key(ckey, 128, &keyEn);
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?