Hi there. Read this letter further only if you have patience to read the attached C code to.

Thanks, so you have patience.
So, I'd like to know, if this code is ok, does it produce a correct ciphertext.  And if it does, can I write the decrypting function just by changing the functions.

EVP_Encrypt_Init to EVP_Decrypt_Init
EVP_Encrypt_Update to EVP_Decrypt_Update and
EVP_Encrypt_Final to EVP_Decrypt_Final ?

The copy string copies from source a substring (from the position begin to position end) into target.

void copy_string(unsigned char *source, int source_length, unsigned char** target, int begin, int end)

//Note:
//the position end is included only
{
        *target = NULL;

        if ((source!=NULL) && (begin>=0) && (end>begin))
        {
                if (end>source_length)
                {
                        end = source_length;
                }

                source = source + begin;
                *target = (unsigned char *) malloc (end-begin);
                for (int i = begin; i<end; i++)
                {
                        **target = *source;
                        (*target)++;
                        source++;
                }
                //**target = '\0';
                *target = *target - (end-begin);
            source = source - end;
        }
}
       

int EVP_DES3_encrypt(unsigned char *in, int in_length, unsigned char *password, int password_length, unsigned char** out)
{
        unsigned char *key   = NULL;
        unsigned char *iv    = NULL;
        unsigned char *OUT   = NULL;
        unsigned char *in_piece = NULL;

        int out_length       = 0;
        int out_piece_length = 0;
        int piece_length     = 512;
        int number_of_pieces = in_length / piece_length;
        int i                                 = 0;
       
        EVP_CIPHER_CTX ctx;
        EVP_CIPHER *cipher_type = EVP_des_ede3_cbc();
        EVP_MD *hash_type = EVP_md5();

        key = (unsigned char*) malloc (EVP_MAX_KEY_LENGTH);
        iv  = (unsigned char*) malloc (EVP_MAX_IV_LENGTH);

       
        OUT = (unsigned char*) malloc (in_length+2*piece_length);
       
        printf("Lefoglalva:%d\n",in_length+2*piece_length);
       
       
        if ((in_length % piece_length) !=0)
        {
                number_of_pieces++;
        }

        EVP_BytesToKey(cipher_type, hash_type, NULL, password, password_length, 1, key, iv);
        EVP_EncryptInit(&ctx, cipher_type, key, iv);

        while (i<number_of_pieces)
        {
                copy_string(in,in_length,&in_piece,i*piece_length,(i+1)*piece_length);
                EVP_EncryptUpdate(&ctx,OUT,&out_piece_length,in_piece,piece_length);
                out_length+=out_piece_length;
                i++;
                //free(in_piece);
        }
        EVP_EncryptFinal(&ctx,OUT,&out_piece_length);
        out_length+=out_piece_length;
       
        *out = OUT;        
       
//        free(OUT);
        free(key);
        free(iv);

//        printf("Szukseges:%d\n",out_length);

        return(out_length);
}

Thanks again.

andras

Reply via email to