简单的 n00b 问题:我试图在 PHP 中复制openssl_private_decrypt函数的行为来解密供应商发送给我的文件,该文件通过openssl_public_encrypt函数加密。我使用的是 python 3.4,因此我能看到的唯一可用的库是pyopenssl,但它的级别足够低,我无法轻松找出如何做我想做的事情。这可能很简单,但有没有人有我想做的例子?
感谢@mnistic,它开始工作,但做了一些修改。这是最终的工作代码(您必须记住openssl_private_decrypt的默认值):
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import padding
# It's critical that the file be opened in mode "rb"!
with open("private.key", 'rb') as key_file:
private_key = serialization.load_pem_private_key(key_file.read(), password=None, backend=default_backend())
with open('encrypted_file', 'rb') as encrypted_file:
ciphertext = encrypted_file.read()
plaintext = private_key.decrypt(ciphertext, padding.PKCS1v15())
请注意,ciphertext
需要小于密钥的最大块大小(对于 RSA,密钥中的位数除以 8)。希望对未来的 Google 员工有所帮助!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句