0

Trying to encrypt and decrypt strings with evp functions of openssl. I tried the following source code but I got unexpected results (garbege output).

What I am missing?

#include <stdio.h>
#include <unistd.h>

#if 1

#include <openssl/evp.h>

char *se_evp_encrypt(char *ssid, char *data, int inl, char *ret, int *rb) 
{
    int i, tmp, ol;
    EVP_CIPHER_CTX  evpctx;
    char key[EVP_MAX_KEY_LENGTH] = {0};
    char iv[EVP_MAX_IV_LENGTH] = {0};

    *ret = '\0';

    strncpy(key, ssid, EVP_MAX_KEY_LENGTH);
    strncpy(iv, ssid, EVP_MAX_IV_LENGTH);

    EVP_EncryptInit(&evpctx, EVP_bf_cbc(), key, iv);
    EVP_EncryptUpdate(&evpctx, ret, &ol, data, inl);
    *rb = ol;
    EVP_EncryptFinal(&evpctx, ret, &ol);
    return ret;
}

char *se_evp_decrypt(char *ssid, char *ct, int inl, char *pt)
{
    int ol;
    EVP_CIPHER_CTX  evpctx;
    char key[EVP_MAX_KEY_LENGTH] = {0};
    char iv[EVP_MAX_IV_LENGTH] = {0};
    char final[EVP_MAX_BLOCK_LENGTH];

    *pt = '\0';

    strncpy(key, ssid, EVP_MAX_KEY_LENGTH);
    strncpy(iv, ssid, EVP_MAX_IV_LENGTH);

    EVP_DecryptInit(&evpctx, EVP_bf_cbc(), key, iv);

    EVP_DecryptUpdate(&evpctx, pt, &ol, ct, inl);
    if (!ol) /* there's no block to decrypt */
    {
        return "";
    }
    pt[ol] = 0;
    EVP_DecryptFinal(&evpctx, final, &inl);
    return pt;
}


int main(int argc,  char *argv[])
{
    char str[] = "abcdef123456789";
    char buf[256] = "", buf2[256] = "";
    int i;

    se_evp_encrypt("anyssid", str, strlen(str), buf, &i);


    printf("Ciphertext is %d bytes.    %d\n", i, strlen(str));

    se_evp_decrypt("anyssid", buf, i, buf2);
    printf("Decrypted: >>%s<<\n", buf2);

}
#endif

1 Answer 1

1

fixed the source code in this way

#include <stdio.h>
#include <unistd.h>

#if 1

#include <openssl/evp.h>

char *se_evp_encrypt(char *ssid, char *data, int inl, char *ret, int *rb) 
{
    int i, tmp, ol;
    EVP_CIPHER_CTX  evpctx = {0};
    char key[EVP_MAX_KEY_LENGTH] = {0};
    char iv[EVP_MAX_IV_LENGTH] = {0};

    *ret = '\0';

    strncpy(key, ssid, EVP_MAX_KEY_LENGTH);
    strncpy(iv, ssid, EVP_MAX_IV_LENGTH);

    EVP_EncryptInit(&evpctx, EVP_bf_cbc(), key, iv);
    EVP_EncryptUpdate(&evpctx, ret, &ol, data, inl);
    EVP_EncryptFinal(&evpctx, ret + ol, &tmp);
    *rb = ol + tmp;
    return ret;
}

char *se_evp_decrypt(char *ssid, char *ct, int inl, char *pt)
{
    int ol, tmp;
    EVP_CIPHER_CTX  evpctx;
    char key[EVP_MAX_KEY_LENGTH] = {0};
    char iv[EVP_MAX_IV_LENGTH] = {0};
    char final[EVP_MAX_BLOCK_LENGTH];

    *pt = '\0';

    strncpy(key, ssid, EVP_MAX_KEY_LENGTH);
    strncpy(iv, ssid, EVP_MAX_IV_LENGTH);

    EVP_DecryptInit(&evpctx, EVP_bf_cbc(), key, iv);

    EVP_DecryptUpdate(&evpctx, pt, &ol, ct, inl);
    EVP_DecryptFinal(&evpctx, pt+ol , &tmp);

    pt[ol+tmp] = 0;

    return pt;
}


int main(int argc,  char *argv[])
{
    char str[] = "abcdef123456789";
    char buf[256] = "", buf2[256] = "";
    int i;

    se_evp_encrypt("anyssid", str, strlen(str), buf, &i);


    printf("Ciphertext is %d bytes.    %d\n", i, strlen(str));

    se_evp_decrypt("anyssid", buf, i, buf2);
    printf("Decrypted: >>%s<<\n", buf2);

}
#endif
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.