2011-01-13 68 views
0

我剛剛包裝了一個頭文件(.h)並創建了.so文件,將它用作python中的模塊。函數Encrypt使用char * bb,然後使用memcpy()填充它。如果我想從c代碼中調用這個函數(char * bb =(char *)new char [200]; Encrypt(...,...,...,bb);)。如何從python調用它? Python中的(char * bb =(char *)new char [200];)如何相等?python中的char *問題

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(); 
}; 

從C調用加密():

char* bbb = (char*) new char [400]; 
int sizec = Encrypt(key, iv, plaintext, bbb); 

我用create_string_buffer( 「」,400)在python,但它不工作。

+0

「如果我想從ac代碼中調用這個函數,我必須做`char * bb =(char *)new char [200];`...」你的意思是C++,而不是C。不必要。 – 2011-01-13 09:16:02

+0

這是什麼`char * bbb =(char *)new char [400];`?你不需要投。只需`char * bbb = new char [400];`就夠了! – Nawaz 2011-01-13 09:17:49

回答

0

我認爲你需要插入「\ 0」在「字符* BB」結束(終止空字符)。或者如果您的數據是字符串而不是二進制文件,則使用strcpy。 對不起,不是你的答案,但你可以得到內存錯誤。