python - PyCrypto:decrypt() 方法返回的函数

标签 python pycrypto pkcs#1

我想在我的 python 网络中使用 RSA,并且我使用了 pycrypto。但我有一些疑问。 这是我的代码:

from Crypto import Random
from Crypto.Cipher import PKCS1_v1_5
from Crypto.PublicKey import RSA
import base64

random_generator = Random.new().read


def decrypt_rsa(secret):
    with open('/home/ubuntu/project/all/all_test/all_python/util/private_rsa.pem') as file:
        private_key = RSA.importKey(file.read())
        cipher = PKCS1_v1_5.new(private_key)
        text = cipher.decrypt(base64.b64decode(secret), random_generator)
        print(str(text))
        return text.decode('utf-8')

但是当我运行应用程序时,我会遇到如下错误:

Traceback (most recent call last):
File "/home/ubuntu/project/all/venv/lib/python3.5/site-packages/flask/app.py", line1982, in wsgi_app
response = self.full_dispatch_request()
File "/home/ubuntu/project/all/venv/lib/python3.5/site-packages/flask/app.py", line1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/ubuntu/project/all/venv/lib/python3.5/site-packages/flask/app.py", line1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/ubuntu/project/all/venv/lib/python3.5/sitepackages/flask/app.py", line1612, in full_dispatch_request
rv = self.dispatch_request()
File "/home/ubuntu/project/all/venv/lib/python3.5/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/ubuntu/project/all/all_test/all_python/v3/v3_main.py", line 192, in v3_set_article_love
if check_secret(secret):
File "/home/ubuntu/project/all/all_test/all_python/v3/v3_main.py", line 18, in check_secret
t = int(secret_util.decrypt_rsa(secret))
File "/home/ubuntu/project/all/all_test/all_python/util/secret_util.py", line14, in decrypt_rsa
return text.decode('utf-8')
AttributeError: 'function' object has no attribute 'decode'

我的服务器操作系统是ubuntu 16.04,使用python3.*,secret是通过base64编码的,就像FRw3kfdpHKD4Ms2O3hmaEoD4Gqp5onwF/OUkkp5zSVDsYnJ9Bej8f6kRewoZ/WBZuJIKy8qAUmhi 6qLwK063quaA4JKE96Ne6bACIcmkj+boWLoE5TsE3zus4Y8vdwhzsT3Hi9qBjjvmE7IV/LILNQizH ELp8fdbpGBwwRNlncAQ= 我非常感谢您的帮助。

最佳答案

简短回答(TL;DR):

decrypt() 方法正在返回您的 random_generator 函数。

详细信息

如果您输入

help(PKCS1_v1_5.PKCS115_Cipher.decrypt)

您可以从官方文档中阅读:

decrypt(self, ct, sentinel) method of 
Crypto.Cipher.PKCS1_v1_5.PKCS115_Cipher instance
Decrypt a PKCS#1 v1.5 ciphertext.

[...]    

:Parameters:
[...]
 sentinel : any type
        The object to return to indicate that an error was detected  
        during decryption.

正如您从上面所读到的,如果解密期间出现错误,则返回 sentinel 对象而不是字节字符串,然后在您的代码中 decrypt() 方法将返回 random_generator 函数。

这就是为什么您的 text 变量是一个函数,而您不能将其视为字节字符串。

进一步说明

我认为没有必要将随机生成器作为哨兵对象传递给decrypt(),因为它只是一个用于检测错误的哨兵对象。请参阅PyCrypto documentation了解有关此库的更多信息(还请阅读 thisthis 并考虑使用 PyCryptodome 代替)。

关于python - PyCrypto:decrypt() 方法返回的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47173762/

相关文章:

python - scipy 与 matlab 中的 dblquad 给出不同的结果

python - 正则表达式错误 : raise error, v # 无效表达式

python - 为什么我可以使用一个 DES key 加密数据并使用另一个成功解密?

jakarta-ee - 如何以PEM格式生成PKCS#1 RSA key ?

encryption - BouncycaSTLe 加密私钥 PEM 输出 : RSA PRIVATE KEY vs PRIVATE KEY

c# - 如何从 PKCS#7 中提取 PKCS#1 签名

python - PyQt : Put several QTreeWidgetItem into columns of QTreeWidget

python - 在 python 中识别 'x:...y:...z:...' 的正则表达式

python - 如何使用 RSA 加密和解密 AES key 并将其存储在文本文件中

c# - AES 加密。从 Python (pyCrypto) 到 .NET