Reputation: 181
I've just wrapped a header (.h) file and created .so to use it as a module in python. The function Encrypt takes char* bb, then fill it using memcpy(). If I want to call this function from a c code I have to do (char* bb = (char*) new char[200]; Encrypt (...,...,..., bb);). How to call it from python? How is the equivalent of (char* bb = (char*) new char[200];) in python?
int Encrypt (string key, string iv, string plaintext, char* bb)
{
std::string ciphertext;
CryptoPP::AES::Encryption aesEncryption((byte *)key.c_str(), CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, (byte *)iv.c_str() );
CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink( ciphertext ) );
stfEncryptor.Put( reinterpret_cast<const unsigned char*>( plaintext.c_str() ), plaintext.length() );
stfEncryptor.MessageEnd();
memcpy(bb,ciphertext.c_str(), ciphertext.size());
return ciphertext.size();
};
Calling Encrypt() from c:
char* bbb = (char*) new char [400];
int sizec = Encrypt(key, iv, plaintext, bbb);
I used create_string_buffer("", 400) in python but it does not work.
Upvotes: 0
Views: 338
Reputation: 2368
I think you need insert '\0' (terminating null character) at the end of "char *bb". Or use strcpy if your data is string and not binary. Sorry, not your answer, but you can get memory error.
Upvotes: 0
Reputation: 7147
IMHO you can not call C functions directly from python, but it is easy to write a python module, see http://docs.python.org/extending/extending.html
EDIT
ctype module allows you to call plain C functions directly.
Check this URL for an answer: http://docs.python.org/library/ctypes.html#passing-pointers-or-passing-parameters-by-reference
Upvotes: 2