Hi....  i'm having problems with the function EVP_DecryptFinal_ex... i have
the next code:

#include <stdio.h>
#include <string.h>
#include <libgen.h>
#include <wchar.h>
#include <sys/time.h>
#include <openssl/pem.h>
#include <openssl/sha.h>
#include <openssl/hmac.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/err.h>

#define LEN_KEY 16
#define ENC_SIZE 512000
#define MSG_SIZE 512000
#define ENCRIPTA 1
#define DESENCRIPTA 0

void unbase64(unsigned char *input, char **ret, int *blen){
  BIO *b64, *bmem;
  char *buff,*tmp;
  int length;

  length = strlen(input)+1;
  tmp = malloc(length*sizeof(char));
  strcpy(tmp,input);
  strcat(tmp,"\n\0");

  buff = (char *)malloc(length);
  memset(buff, 0, length);

  b64 = BIO_new(BIO_f_base64());
  bmem = BIO_new_mem_buf(tmp, length);
  bmem = BIO_push(b64, bmem);

  *blen = BIO_read(bmem, buff, length);
  BIO_free_all(bmem);

  strcpy(*ret,buff);

  free(tmp);
  free(buff);
}

void base64(const unsigned char *input, int length, char **ret){
  BIO *bmem, *b64;
  BUF_MEM *bptr;
  char *buff;

  b64 = BIO_new(BIO_f_base64());
  bmem = BIO_new(BIO_s_mem());
  b64 = BIO_push(b64, bmem);

  BIO_write(b64, input, length);
  BIO_flush(b64);
  BIO_get_mem_ptr(b64, &bptr);

  buff = (char *)malloc(bptr->length);
  memcpy(buff, bptr->data, bptr->length-1);
  buff[bptr->length-1] = 0;

  BIO_free_all(b64);

  strcpy(*ret,buff);

}

void rpad(const char *to_pad, char **padded, char *pad_char , int len){
        int i,start;

        start = strlen(to_pad);
        strcpy(*padded,to_pad);
        for(i = start; i <= len; i++) {
                strcat(*padded,pad_char);
        }
}

void lpad(const char *to_pad, char **padded, char *pad_char , int len){
        int i,start;

        start = strlen(to_pad);
        strcpy(*padded,"");
        for(i = start; i <= len; i++) {
                strcat(*padded,pad_char);
        }
        strcat(*padded,to_pad);
}

void hextoascii(const char *hex, char **ascii){
        int i;
        char *tmp;

        tmp = malloc(2*sizeof(char ));
        strcpy(*ascii,"");


        for(i=0; i < strlen(hex) ; i=i+2){
                sprintf(tmp,"%c%c",*(hex+i),*(hex+i+1));
                sprintf(tmp,"%c",(unsigned int)strtol(tmp,NULL,16));
                strcat(*ascii,tmp);
        }

        free(tmp);
}

void asciitohex(const char *ascii, char **hex){
        int i;
        char *tmp;

        tmp = malloc(2*sizeof(char ));
        strcpy(*hex,"");

        for(i=0; i < strlen(ascii) ; i++){
                sprintf(tmp,"%02X",*(ascii+i)&255);
                strcat(*hex,tmp);
        }

        free(tmp);
}

void tripledes(char *llave1, char *llave2, char *text, char **ret, int
action) {
    EVP_CIPHER_CTX ctx;
    unsigned char iv[] = "OTRACLAV";
    char *key,*key1,*key2,*encrypted,*encryptedbase64,*plain;
    int olen, tlen, encrypted_len,flen;
    FILE *fdcypher;

    key = malloc(24*sizeof(char ));
    key1 = malloc(8*sizeof(char ));
    key2 = malloc(8*sizeof(char ));
    encryptedbase64 = malloc(ENC_SIZE*sizeof(char ));
    plain = malloc(MSG_SIZE*sizeof(char ));
    encrypted = malloc(ENC_SIZE*sizeof(char ));

    hextoascii(llave1,&key1);
    hextoascii(llave2,&key2);

    strcpy(key,key1);
    strcat(key,key2);
    strcat(key,key1);

    EVP_CIPHER_CTX_init(&ctx);

    switch(action) {
      case ENCRIPTA:
    bzero(encrypted, ENC_SIZE);
    EVP_EncryptInit_ex(&ctx, EVP_des_ede3_cbc(), NULL, key, iv);
    EVP_EncryptUpdate(&ctx, encrypted, &olen, text, strlen(text));
    EVP_EncryptFinal_ex(&ctx, encrypted + olen, &tlen);
    EVP_CIPHER_CTX_cleanup(&ctx);
    encrypted_len = olen + tlen;
    base64(encrypted,encrypted_len,&encryptedbase64);
    strcpy(*ret,encryptedbase64);
    break;
      case DESENCRIPTA:
    bzero(plain, ENC_SIZE);
    fdcypher = fopen(text,"r");
    fseek(fdcypher, 0L, SEEK_END);
    flen = ftell(fdcypher);
    rewind(fdcypher);
    fread(encryptedbase64,flen,1,fdcypher);

    unbase64(encryptedbase64, &encrypted, &encrypted_len);

    EVP_DecryptInit_ex(&ctx, EVP_des_ede3_cbc(), NULL, key, iv);
    if (ERR_peek_error()) {
        ERR_load_crypto_strings();
        ERR_print_errors_fp(stderr);
        ERR_free_strings();
    }

    fprintf(stderr,"%d - %d\n",strlen(encrypted),encrypted_len);
    EVP_DecryptUpdate(&ctx, plain, &olen, encrypted, encrypted_len);
    if (ERR_peek_error()) {
        ERR_load_crypto_strings();
        ERR_print_errors_fp(stderr);
        ERR_free_strings();
    }

    EVP_DecryptFinal_ex(&ctx, plain + olen, &tlen);
    if (ERR_peek_error()) {
        ERR_load_crypto_strings();
        ERR_print_errors_fp(stderr);
        ERR_free_strings();
    }

    EVP_CIPHER_CTX_cleanup(&ctx);
    encrypted_len = olen + tlen;
    strncpy(*ret,plain,encrypted_len);

    break;
    }


    free(key);
}


int main(int argc, char **argv ){
    char *llave1,*llave2,*messge,*res;
    int action;

    llave1 = malloc(LEN_KEY);
    llave2 = malloc(LEN_KEY);
    messge = malloc(MSG_SIZE);
    res = malloc(ENC_SIZE);

    action = *argv[1] - 48;
    strcpy(llave1,argv[2]);
    strcpy(llave2,argv[3]);
    strcpy(messge,argv[4]);

    tripledes(llave1,llave2,messge,&res, action);

    printf("%s",res);

    free(llave1);
    free(llave2);
    free(messge);
}

When i use to encrypt data, i have not problems.. when i decrypt the result
of this code, i have not problem... when i decrypt with this program, i have


13015:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad
decrypt:evp_enc.c:461:

The EVP_DecryptUpdate works ok, decrypt the info, but the rest off encypted
(the remaining encrypted data) is not decrypted, and the tlen = 0.

thanks...

Reply via email to