通过Flask UnicodedeCodede加密RSA

2024-09-26 18:20:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在构建一个简单的flask应用程序,它允许用户发布数据,我们返回一个RSA公钥加密的base64编码字符串。在

Flask应用程序路径如下所示

def index(eyaml_output=""):
if request.method == 'GET':
    return render_template('index.html.j2', eyaml_output=eyaml_output)
elif request.method == 'POST':
    input = request.form['eyaml_input']
    output = helpers.encrypt(input)
    eyaml_output = output

encrypt函数如下所示

^{pr2}$

这一切从命令行都能很好地工作。但是当在flask中调用时,我得到以下unicodedecoderror

UnicodeDecodeError: 'ascii' codec can't decode byte 0xda in position 0: ordinal not in range(128)

我尝试过切换到utf-8,但得到了类似的错误

编辑: 我正在用python2.7virtualenv进行测试。输出本身似乎也不重要。即使在执行中间步骤时也会失败,并出现相同的Unicode错误。问题似乎发生在加密部分,而不是base64编码。下面是完整堆栈跟踪

Traceback (most recent call last):
  File "/Users/bchen/Git/Stoneridge/ansible/eyaml-ansible-
filter/flask/.venv/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/bchen/Git/Stoneridge/ansible/eyaml-ansible-
filter/flask/.venv/lib/python2.7/site-packages/flask/app.py", line 
1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/bchen/Git/Stoneridge/ansible/eyaml-ansible-
filter/flask/.venv/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/bchen/Git/Stoneridge/ansible/eyaml-ansible-
filter/flask/.venv/lib/python2.7/site-packages/flask/app.py", line 
1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/bchen/Git/Stoneridge/ansible/eyaml-ansible-
filter/flask/.venv/lib/python2.7/site-packages/flask/app.py", line 
1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "app.py", line 13, in index
    output = helpers.encrypt(input)
  File "/Users/bchen/Git/Stoneridge/ansible/eyaml-ansible-
filter/flask/helpers.py", line 12, in encrypt
    fake_output = base64.b64encode(cipher.encrypt(arg))
  File "/Users/bchen/Git/Stoneridge/ansible/eyaml-ansible-
filter/flask/.venv/lib/python2.7/site-
packages/Crypto/Cipher/PKCS1_OAEP.py", line 150, in encrypt
    db = lHash + ps + bchr(0x01) + message
UnicodeDecodeError: 'ascii' codec can't decode byte 0xda in position 0: 
ordinal not in range(128)


Tags: inpygitappflaskoutputrequestline

热门问题