访问 C 库的最 Python 方式是什么 - 例如 OpenSSL?

发布于 2024-07-11 00:09:49 字数 338 浏览 10 评论 0原文

我需要访问 OpenSSL 的加密函数来对 CBC 流中的 Blowfish 数据进行编码。 我在谷歌上搜索并找到了一些 Blowfish 库(手写)和一些 OpenSSL 包装器(似乎都不完整)。

最后,我需要访问某些 OpenSSL 函数,例如 命令。 访问它们的 pythonic/正确方法是什么? 使用 SWIG 之类的东西来允许 Python/C 绑定,或者有更好的方法吗?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

生死何惧 2024-07-18 00:09:49

在 Python 中与 C(和 C++)交互的方法有很多。 ctypes 对于快速的小扩展来说非常好,但它有将编译时错误转变为运行时段错误的习惯。 如果您想编写自己的扩展,SIP 非常好。 SWIG 非常通用,但拥有更多的追随者。 当然,您应该做的第一件事是看看您是否真的需要接口。 你看过 PyCrypto 吗?

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?

梦里寻她 2024-07-18 00:09:49

ctypes 是起点。 它允许您调用 DLL、使用 C 声明的类型等。我不知道是否存在限制使您无法执行所需的所有操作,但它非常强大,并且包含在标准库中。

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.

怕倦 2024-07-18 00:09:49

我对河豚的 M2Crypto(OpenSSL 包装器)感到满意。

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

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
浅唱々樱花落 2024-07-18 00:09:49

SWIG 几乎是规范的方法。 效果也很好。

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

梦一生花开无言 2024-07-18 00:09:49

我在 Cython 方面也取得了很好的成功。

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

权谋诡计 2024-07-18 00:09:49

我也会推荐 M2Crypto ,但是如果 joeforker 的代码示例看起来有点奇怪,您可能会遇到更轻松地理解 M2Crypto 密码单元测试,其中包括 Blowfish。 查看 test_evp.py 中的 CipherTestCase。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文