Blowfish and C++

I am recently returning to C++ from Java and Ruby so I apologize in advance for this question. I suspect I am overlooking something obvious.

I have some code that is trying to decrypt something encrypted using the Ruby crypt gem and Blowfish encryption. This is the Ruby code:

def self.encrypt(pass, key)
  blowfish = Crypt::Blowfish.new(key)
  encryptedBlock = blowfish.encrypt_block(pass)
  return encryptedBlock
end

The C++ code will decrypt stuff that I encrypted in C++ just fine, but is not correctly decrypting the stuff that was encrypted in Ruby. This is the C++ code:

unsigned char * decoded_msg;
decoded_msg = new unsigned char [msg->size() + 1];
bfdecrypt ( ( unsigned char * ) cipher_key->c_str(), cipher_key->size(),
( unsigned char * ) msg->c_str(), decoded_msg, msg->size() + 1 );
cout << decoded_msg << " " << msg->size() << endl;
void CryptHandler::bfdecrypt ( unsigned char *keydata, int keydatalen, unsigned char *in, unsigned char
                               *out, unsigned int inlen )
{
	BF_KEY key;
	unsigned char ivec[32];
	int num=0;

	// set up for decryption
	BF_set_key ( &key, keydatalen, keydata );
	memset ( ivec, '\0', 32 );
	BF_cfb64_encrypt ( in, out, inlen, &key, ivec, &num, BF_DECRYPT );
}

Any help/pointers would be greatly appreciated.

Hi,

I guess cipher_key and msg are std::string * types. From this assumption you want to assign the encrypted contents to msg indirect over the c_str part. But c_str returns a non modifiable C character array and hence this will not work.

As a possible workaround you can use out as a local variable and pass msg as reference to bfdecrypt and assign it in there (like msg = out).

Hope this helps