Title: Message

Hi, I have the following code to do encryption and decryption (blowfish). But I have problem of encrypting/decrypting the string with certain length in my testing. Every thing seems working fine for the string length less than 48. if the length is greater than or equal to 48 and less than 56, the decryption of the encrypted string will return error and some times core dumps. it works for length equal to 56, not working for > 56 and working again for some value > 56. It seems weird to me. Does any body know what's wrong?

class Cipher

{

public:

Cipher(unsigned char*,unsigned char*);

~Cipher();

int encrypt(unsigned char*,unsigned char**, int);

int decrypt(unsigned char*,unsigned char**, int);

protected:

unsigned char* mKey;

unsigned char* mIv;

};

Cipher::Cipher(unsigned char* aKey, unsigned char* aIv)

{

int lKeylen = strlen((char*)aKey);

int lIvlen = strlen((char*)aIv);

mKey=(unsigned char*)malloc(lKeylen);

mIv =(unsigned char*)malloc(lIvlen);

memcpy(mKey,aKey,lKeylen);

memcpy(mIv,aIv,lIvlen);

}

Cipher::~Cipher()

{

free(mKey);

free(mIv);

}

int Cipher::encrypt(unsigned char* aIn,unsigned char** aOut, int aInLen)

{

return Symmetric_Encrypt(aIn, aOut, aInLen, mKey, mIv);

}

int Cipher::decrypt(unsigned char* aIn,unsigned char** aOut, int aInLen)

{

return Symmetric_Decrypt(aIn, aOut, aInLen, mKey, mIv);

}

int Symmetric_Encrypt(unsigned char *inbuf, unsigned char **outbuf, int inlen, unsigned char *key,

unsigned char *iv)

{

BIO *bio, *mbio, *cbio;

unsigned char *dst;

int outlen;

mbio = BIO_new(BIO_s_mem());

cbio = BIO_new(BIO_f_cipher());

BIO_set_cipher(cbio, EVP_bf_ecb(), key, iv, 1);

bio = BIO_push(cbio, mbio);

BIO_write(bio, inbuf, inlen);

BIO_flush(bio);

outlen = BIO_get_mem_data(mbio, (unsigned char *) &dst);

*outbuf=(unsigned char*)malloc(outlen);

memcpy(*outbuf, dst, outlen);

BIO_free_all(bio);

return outlen;

}

int Symmetric_Decrypt(unsigned char *inbuf, unsigned char **outbuf,

int inlen, unsigned char *key, unsigned char *iv)

{

BIO *bio, *mbio, *cbio;

unsigned char *dst;

int outlen;

mbio = BIO_new(BIO_s_mem());

cbio = BIO_new(BIO_f_cipher());

BIO_set_cipher(cbio, EVP_bf_ecb(), key, iv, 0);

bio = BIO_push(cbio, mbio);

BIO_write(bio, inbuf, inlen);

BIO_flush(bio);

outlen = BIO_get_mem_data(mbio, (unsigned char *) &dst);

*outbuf=(unsigned char*)malloc(outlen);

memcpy(*outbuf, dst, outlen);

BIO_free_all(bio);

return outlen;

}

 

 

Thanks for your help.

 

Louis

Reply via email to