Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过DeDecodeError进行RSA加密_Python_Encryption_Flask - Fatal编程技术网

Python 通过DeDecodeError进行RSA加密

Python 通过DeDecodeError进行RSA加密,python,encryption,flask,Python,Encryption,Flask,我正在构建一个简单的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

我正在构建一个简单的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
from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA
import base64

def encrypt(arg):
    with open('eyaml_public.key') as public_key_file:
        public_key = public_key_file.read()

    pubkey = RSA.importKey(public_key)
    cipher = PKCS1_OAEP.new(pubkey)

    output = base64.b64encode(cipher.encrypt(arg))

    return output
encrypt函数如下所示

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
from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA
import base64

def encrypt(arg):
    with open('eyaml_public.key') as public_key_file:
        public_key = public_key_file.read()

    pubkey = RSA.importKey(public_key)
    cipher = PKCS1_OAEP.new(pubkey)

    output = base64.b64encode(cipher.encrypt(arg))

    return output
这一切都可以在命令行中正常工作。但是当在flask中调用时,我得到以下UnicodeDecodeError

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)


如果您在
arg
参数上添加对的调用,它应该可以工作。
encrypt
方法需要一个字节序列,但得到的却是一个
str

如果在
arg
参数上添加对的调用,它应该可以工作。
encrypt
方法需要一个字节序列,但得到的是一个
str

您可能在控制台中使用了不同版本的Python-3.x和Flask,2.x,因为后者的
base64.b64encode
生成了一个字符串而不是字节,所以您不必解码任何内容。为了安全起见,请始终对base64输出进行解码,例如,
返回输出。decode(“拉丁语-1”)
您可能正在使用不同版本的Python—3.x和Flask,2.x在控制台中—因为后者的
base64.b64encode
生成字符串而不是字节,所以您不必解码任何内容。为了安全起见,请始终对base64输出进行解码,例如返回输出。解码(“拉丁语-1”)