What's the most pythonic way of access C libraries - for example, OpenSSL?

I need to access the crypto functions of OpenSSL to encode Blowfish data in a CBC streams. I've googled and found some Blowfish libraries (hand written) and some OpenSSL wrappers (none of the seem complete.)

In the end, I need to access the certain OpenSSL functions, such as the full blowfish.h library of commands. What's the pythonic/right way of accessing them? Using something like SWIG to allow Python/C bindings, or is there a better way?

Thanks!


Asked by: Briony378 | Posted: 28-01-2022






Answer 1

There's lots of ways to interface with C (and C++) in Python. ctypes is pretty nice for quick little extensions, but it has a habit of turning would be compile time errors into runtime segfaults. If you're looking to write your own extension, SIP is very nice. SWIG is very general, but has a larger following. Of course, the first thing you should be doing is seeing if you really need to interface. Have you looked at PyCrypto?

Answered by: Jack849 | Posted: 01-03-2022



Answer 2

ctypes is the place to start. It lets you call into DLLs, using C-declared types, etc. I don't know if there are limitations that will keep you from doing everything you need, but it's very capable, and it's included in the standard library.

Answered by: Maddie487 | Posted: 01-03-2022



Answer 3

I was happy with M2Crypto (an OpenSSL wrapper) for blowfish.

import M2Crypto
from M2Crypto import EVP
import base64
import struct

key = '0' * 16 # security FTW
iv = '' # initialization vector FTW
dummy_block =  ' ' * 8

encrypt = EVP.Cipher('bf_cbc', key, iv, M2Crypto.encrypt)
decrypt = EVP.Cipher('bf_cbc', key, iv, M2Crypto.decrypt)

binary = struct.pack(">Q", 42)
ciphertext = encrypt.update(binary)

decrypt.update(ciphertext) # output is delayed by one block
i = struct.unpack(">Q", decrypt.update(dummy_block))

print i

Answered by: Julian642 | Posted: 01-03-2022



Answer 4

SWIG is pretty much the canonical method. Works good, too.

Answered by: Freddie321 | Posted: 01-03-2022



Answer 5

I've had good success with Cython, as well.

Answered by: Daryl787 | Posted: 01-03-2022



Answer 6

I would recommend M2Crypto as well, but if the code sample by joeforker looks a bit strange you might have an easier time understanding the M2Crypto cipher unit tests, which include Blowfish. Check out the CipherTestCase in test_evp.py.

Answered by: Rafael197 | Posted: 01-03-2022



Similar questions

xml - Are there any libraries that support MTOM encryption in Python

I am using Python Suds to connect to a webservice and I am wanting to use MTOM encrpytion to send an XML message. I have looked around the web and seen libraries for PHP etc. but not for Python. Can anyone recommend a good MTOM library to use preferably for use with SUDS. Thanks for any suggestions.


encryption - Can I encrypt email and decrypt it back using python default library set?

Of course similar questions have been asked in stackoverflow but I don't want to use any third party library like Crypto or something. So I need to generate a ciphertext from a user email and decrypt it back to plaintext. How can I do this in python?


security - What is the most secure python "password" encryption

I am making a little webgame that has tasks and solutions, the solutions are solved by entering a code given to user after completion of a task. To have some security (against cheating) i dont want to store the codes genereted by the game in plain text. But since i need to be able to give a player the code when he has accomplished the task i cant hash it since then i cant retrive it. So what is the most secure way ...


Encryption with Python

I'm making an encryption function in Python and I want to encrypt a random number using a public key. I wish to know that if I use Crypto package (Crypto.publicKey.pubkey) than how can I use the method like... def encrypt(self,plaintext,k) Here the k is itself a random number, is this mean the key. Can somebody help me with somewhat related?


Python: encryption as means to prevent data tampering

Many of my company's clients use our data acquisition software in a research basis. Due to the nature of research in general, some of the clients ask that data is encrypted to prevent tampering -- there could be serious ramifications if their data was shown to be falsified. Some of our binary software encrypts output files with a password stored in the source, that looks like random characters. At the so...


relevant query to what is the best python method for encryption

I tried to use the gnupg.py module encryption decryption function named " def test_encryption_and_decryption(self): " Could i use this function by passing the key or fingerprint retrieved from public key server. I am getting the key by this : retk = urllib.urlopen('http://pool.sks-keyservers.net:11371/pks/lookup op=get&search=hex format of key') pub_key = retk.read() ...


python - UDP packet encryption

This appears to be reasonably trivial if using the ssl module for TCP communication, but how would encrypted communication be done via UDP? Can the ssl module still be used? if so, what steps would need to be performed for the client and server to be in a position where data can be sent to-and-fro as normal?


python - How to add a padding to the data to make it acceptable for AES256 encryption algorithm in pycrypto library

Can someone tell me how to add a padding to the data to make it acceptable for AES256 encryption algorithm in pycrypto library (Python). Thanks a lot in advance.. :)


django, python and link encryption

I need to arrange some kind of encrpytion for generating user specific links. Users will be clicking this link and at some other view, related link with the crypted string will be decrypted and result will be returned. For this, I need some kind of encryption function that consumes a number(or a string) that is the primary key of my selected item that is bound to the user account, also consuming some kind of seed a...


What encryption algorithm would be best for data transfer between Python and Php?

I am writing a client / server based program, Server side is with Php and Client is with Python, and I need to make sure the transfer data is safe both way. So, My question is What encryption algorithm would be best for data transfer between Python and Php? I couldn't use Https Need to decrypt/encrypt with key on both Python and Php


python - AES encryption with PyCrypto and decryption with mcrypt

For some sensitive data I decided to store it AES-encrypted on disc. I've implemented the encryption using PyCrypto. Furthermore, the data is important, and the stored encrypted data will be my only copy of it (backups aside), so I looked for some means of retrieving the data without using PyCrypto to have a fallback given the possibility that PyCrypt...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top