Hello,

i have a problem with the Blowfish algorithm. When i encrypt a long
message i get the following error when i try to decrypt the message:

4156:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad
decrypt:.\crypto\evp\evp_enc.c:454:

The message length is form 10 to 450 (or more) characters. The funny thing is
even though i hash the key with MD5 the length of the message i can
decrypt seems to depend on the key i choose to hash and further more
it looks like it depend on the content of the message itself as well.

The message contains only characters.

I use OpenSSL 0.9.8b on Windows XP with Visual Studio 2005.

Any help will be appreciated.

And here is the code i use for encryption and decryption:

#include <iostream>
#include <string>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/bio.h>
#include <openssl/applink.c>
#pragma comment(lib, "libeay32.lib")

using namespace std;

void hashtheKey (char* key, char* output)
{
        OpenSSL_add_all_digests();
        BIO *bio_err = BIO_new_fp(stdout, BIO_NOCLOSE);
        EVP_MD_CTX mdctx;
        const EVP_MD *md;
        unsigned char md_value[EVP_MAX_MD_SIZE];
        int md_len;
        unsigned char iv[] = {0,1,2,3,4,5,6,7};

        //Hash the Key
        md = EVP_get_digestbyname("md5");
        if(!md) {
        cout << "Unknown message digest" << endl;
        exit(1);
        }

        string hash;
        EVP_MD_CTX_init(&mdctx);
        EVP_DigestInit_ex(&mdctx, md, NULL);
        EVP_DigestUpdate(&mdctx, key, strlen(key));
        EVP_DigestFinal_ex(&mdctx, md_value, (unsigned int *)&md_len);
        EVP_MD_CTX_cleanup(&mdctx);
        
        for (int i = 0; i < md_len; i++)
                hash += md_value[i];
        strcpy(output, hash.c_str());
}
int blowencrypt (char* key, char* input, char* output)
{
        ERR_load_crypto_strings();
        OpenSSL_add_all_algorithms();
        BIO *bio_err = BIO_new_fp(stdout, BIO_NOCLOSE);
        
        int outlen = 0, tmplen = 0;
        unsigned char iv[] = {0,1,2,3,4,5,6,7};

        char* hash = new char[EVP_MAX_MD_SIZE];
        hashtheKey(key, hash);

        cout << "Hashed key: " << hash << endl;

        //Encrypt the message
        char *encodebuffer = new char[strlen(input) + EVP_MAX_BLOCK_LENGTH - 1];
        EVP_CIPHER_CTX ctx;

        EVP_CIPHER_CTX_init(&ctx);
        EVP_EncryptInit_ex(&ctx, EVP_bf_cbc(), NULL, (const unsigned 
char*)hash, iv);

        if(!EVP_EncryptUpdate(&ctx, (unsigned char*)encodebuffer, &outlen, 
(const unsigned char*)input, strlen(input))) 
        {
                ERR_print_errors(bio_err);
                BIO_free_all(bio_err);  
        }
        tmplen += outlen;

        if(!EVP_EncryptFinal(&ctx, (unsigned char*)encodebuffer + tmplen, 
&outlen))
        {
                ERR_print_errors(bio_err);
                BIO_free_all(bio_err);          
        }

        tmplen += outlen;
        string chipher;
        for (int i = 0; i < tmplen; i++)
                        chipher += encodebuffer[i];

        EVP_CIPHER_CTX_cleanup(&ctx);
        strcpy(output, chipher.c_str());
        return tmplen;
}
void blowdecrypt (char* key, char* input, int len, char* output)
{
        ERR_load_crypto_strings();
        OpenSSL_add_all_algorithms();
        BIO *bio_err = BIO_new_fp(stdout, BIO_NOCLOSE);
        EVP_CIPHER_CTX ctx;

        int outlen = 0, tmplen = 0;
        unsigned char iv[] = {0,1,2,3,4,5,6,7};

        char *hash = new char[EVP_MAX_MD_SIZE];
        hashtheKey(key, hash);
        cout << "Hashed key: " << hash << endl;

        char *decodebuffer = new char[len + EVP_MAX_BLOCK_LENGTH];
        EVP_CIPHER_CTX_init(&ctx);
        EVP_DecryptInit_ex(&ctx, EVP_bf_cbc(), NULL, (const unsigned 
char*)hash, iv);
        if(!EVP_DecryptUpdate(&ctx, (unsigned char*)decodebuffer, &outlen, 
(const unsigned char*)input, len))
        {
                ERR_print_errors(bio_err);
                BIO_free_all(bio_err);          
        }
        tmplen = outlen;        
        
        if(!EVP_DecryptFinal_ex(&ctx, (unsigned char*)decodebuffer , &outlen))  
        {
                ERR_print_errors(bio_err);
                BIO_free_all(bio_err);          
        }       
        tmplen += outlen;
        EVP_CIPHER_CTX_cleanup(&ctx);
        
        string plain;
        for (int i = 0; i < tmplen; i++)
                plain += decodebuffer[i];
        strcpy(output, plain.c_str());
}

int main()
{

        //char *message = "This is a secret.This is a secret.This is a 
secret.This is a secret.This is a secret."\
                          "This is a secret.This is a secret.This is a 
secret.This is a secret.This is a secret."\
                          "This is a secret.This is a secret.This is a 
secret.This is a secret.This is a secret."\
                          "This is a secret.This is a secret.This is a 
secret.This is a secret.This is a secret."\
                          "This is a secret.This is a secret.This is a 
secret.This is a secret.This is a secret."\
                          "This is a secret.This is a secret.This is a 
secret.This is a secret.This is a secret."\
                          "This is a secret.This is a secret.This is a 
secret.This is a secret.This is a secret."\
                          "This is a secret.This is a secret.This is a 
secret.This is a secret.This is a secret."\
                          "This is a secret.This is a secret.This is a 
secret.This is a secret.This is a secret."\
                          "This is a secret.This is a secret.This is a 
secret.This is a secret.";
        char *message = "01234567890123456789012345678901234567890123456789"\
                        "01234567890123456789012345678901234567890123456789"\
                        "01234567890123456789012345678901234567890123456789"\
                        "01234567890123456789012345678901234567890123456789"\
                        "01234567890123456789012345678901234567890123456789"\
                        "01234567890123456789012345678901234567890123456789";


        cout << "Testing Blowfish encryption..." << endl << endl;
        char *key = "This is a secret key";
        int mylen = 0;
        cout << strlen(message) << endl;

        char *chipherbuffer = new char[strlen(message) * 2];
        char* plainbuffer = new char[strlen(message) * 2];
        mylen = blowencrypt(key, message, chipherbuffer);
        cout << chipherbuffer << endl;

        blowdecrypt(key, chipherbuffer, mylen, plainbuffer);
        cout << plainbuffer << endl << endl;

        cout << "Finished." << endl;

        string temp = "";
        getline(cin, temp);
        return (0);
}        
-- 
Mit freundlichen Grüßen
Andreas von Linden
mailto:[EMAIL PROTECTED]

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to