views:

99

answers:

2

I have a program in C++ that uses the cryptopp library to decrypt/encrypt messages.

It offers two interface methods encrypt & decrypt that receive a string and operate on it through cryptopp methods.

Is there some way to use both methods in Python without manually wrapping all the cryptopp & files included?

Example:

import cppEncryptDecrypt

string foo="testing"
result = encrypt(foo)
print "Encrypted string:",result
+6  A: 

If you can make a DLL from that C++ code, exposing those two methods (ideally as "extern C", that makes all interfacing tasks so much simpler), ctypes can be the answer, not requiring any third party tool or extension. Otherwise, it's your choice between cython, good old SWIG, SIP, Boost, ... -- many, many such 3rd party tools will let your Python code call those two C++ entry points without any need for wrapping anything else but them.

Alex Martelli
Ragnagard
yeah, after some hours trying in this new dll thing, i reached the goal. thanks Alex.
Ragnagard
@Ragnagard, you're welcome, always glad to help!
Alex Martelli
+4  A: 

As Alex suggested you can make a dll, export the function you want to access from python and use ctypes(http://docs.python.org/library/ctypes.html) module to access e.g.

>>> libc = cdll.LoadLibrary("libc.so.6")
>>> printf = libc.printf
>>> printf("Hello, %s\n", "World!")
Hello, World

or there is alternate simpler approach, which many people do not consider but is equally useful in many cases i.e. directly call the program from command line. You said you have already working program, so I assume it does both encrypt/decrypt from commandline? if yes why don't you just call the program from os.system, or subprocess module, instead of delving into code and changing it and maintaining it.

I would say go the second way unless it can't fulfill your requirements.

Anurag Uniyal
+1 for cmndline interface
Program works as a server (sockets) encrypting and decrypting data based on that, no cmd line interface then :(.Apart from that, i dont know if calling to a cmd program on each packet would be same in performance as if called from dll...
Ragnagard
no you shouldn't even think of starting/closing a process on each packet, so second solution doesn't apply to you
Anurag Uniyal