Archived Forum Post

Index of archived forum posts

Question:

Python Byte Data

Oct 11 '15 at 18:34

I am trying to use this API from Python 2.7, but I am having trouble. The documentation shows:

# data is a CkByteData
# outData is a CkByteData (output)
status = crypt2.EncryptBytes(data, outData);
but Python does not allow parameters (outdata) to receive results.

I notice that some of the API's have lower-case equivalents (i.e., encryptBytes) that return the output instead of the status. If I try that, I get an exception: "AttributeError: encryptBytes"

Ideally, I'd like to pass bytes (the Python type) in, and receive bytes back so as to eliminate conversions, as throughput is an issue. Ditto for decrypt.


Answer

Methods that return binary bytes do so by depositing the bytes in the output-only CkByteData object that is passed in the last argument. This is an object that would first be instantiated by the Python script, and then passed to the method. The empty CkByteData is passed in the last argument, and it is filled with the result bytes. The success/failure of any method returning bytes in this way is the boolean value returned by the method: True(success) / False(failure).

For example:

import chilkat

crypt = chilkat.CkCrypt2()

...

# Load something from a file. # Create a CkByteData object to hold the bytes. inByteObj = chilkat.CkByteData() success = inByteObj.loadFile("hamlet.xml")

# Encrypt bytes # Create a CkByteData object to hold the encrypted bytes. encByteObj = chilkat.CkByteData() success = crypt.EncryptBytes(inByteObj, encByteObj) # save the encrypted bytes to a file encByteObj.saveFile("hamletEncrypted.enc")

# grab a pointer to the bytes for Python to access. # this is not copying the bytes -- it's just returning a pointer to the bytes. p = encByteObj.getBytes() f = open('h.enc', 'wb') f.write(p) f.close()

# Create a CkByteData object to hold the decrypted bytes: decByteObj = chilkat.CkByteData()

success = crypt.DecryptBytes(encByteObj,decByteObj) # save the decrypted bytes to a file. decByteObj.saveFile("hamletDecrypted.xml")