'How do I implement OpenSSL's AES in C through EVP?

void aes_encrypt( unsigned char *source, unsigned char *iv, unsigned char *key, unsigned char *target) {

        int in_len, out_len;

        in_len = strlen(source);

        EVP_CIPHER_CTX *ctx;
        ctx = EVP_CIPHER_CTX_new();

        if(ctx == NULL) {

            printf("Error creating ctx!");
        }

        printf("\nThis is the plaintext: %s", source);
        printf("\nThe length of this string is: %d", in_len);
        fflush(stdout);

        EVP_CIPHER_CTX_init(ctx);

        EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv);
        EVP_EncryptUpdate(ctx, target, &out_len, source, in_len);
        EVP_EncryptFinal(ctx, target, &out_len);

        printf("\nThis is the encrypted text: %s", target);
        printf("\nThe length of this string is %d", out_len);
        fflush(stdout);

         EVP_CIPHER_CTX_free(ctx);
    }

    void aes_decrypt( unsigned char *source, unsigned char *iv, unsigned char *key, unsigned char *target) {

        int in_len = strlen(source);
        int out_len;

        EVP_CIPHER_CTX *ctx;
        ctx = EVP_CIPHER_CTX_new();

        if(ctx == NULL) {

            printf("Error creating ctx!");
        }

        EVP_CIPHER_CTX_init(ctx);

        EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv);
        EVP_DecryptUpdate(ctx, source, &out_len, target, in_len);
        EVP_DecryptFinal_ex(ctx, target, &out_len);

        target[out_len] ='\0';

        printf("\nThe deciphered text is: %s", target);
        printf("\nThe length of this text is: %d", out_len);
        printf("\nWorked");
        fflush(stdout);

        EVP_CIPHER_CTX_free(ctx);

    }
int main() {

    unsigned char *strte, *strtg, *output;

    unsigned char *key;
    unsigned char *iv;

    printf("Give me something to encrypt: ");
    fflush(stdout);
    strte = scan();
    fflush(stdout);

    strtg = (unsigned char *) malloc(sizeof(strte));
    output = (unsigned char *) malloc(sizeof(strte));
    iv = (unsigned char *) malloc(16);

    key  = "PasswordPassword";

    RAND_poll();
    RAND_bytes(iv, 16);

    aes_encrypt(strte, iv, key, strtg);

    aes_decrypt(strtg, iv, key, output);

    return 0;
}

I have been trying to encrypt and decrypt some text using AES with EVP from OpenSSL. Encryption seems to work, but decryption doesn't output anything. What am I doing wrong? The reason I am doing this is because I am working on a password manager and am looking at ways to store user data locally in files ( I am a beginner, and this is a personal project ), but I'm not entirely sure if OpenSSL is the best for this purpose. Are there any other libraries I should take a look at?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source