簡體   English   中英

python + 腳本在 /usr/lib64/python2.7 下的密碼學失敗

[英]python + script failed regarding to cryptography under /usr/lib64/python2.7

我創建以下 python 腳本

這個 python 腳本將讀取文件/lpp/airflow/.sec/rmq_pass到 var - pass_hash

並將其解密為decrypted_pass

more security_test.py
import sys
import os
import base64
from cryptography.fernet import Fernet

key_file = "/lpp/airflow/.sec/key"
rmq_pass_file = "/lpp/airflow/.sec/rmq_pass"

key = open(key_file, 'r')
f = Fernet(key.read())

pass_hash  = open(rmq_pass_file, 'r')
#decrypting the password from "pass_file" file using the key from the "key_file".
decrypted_pass = f.decrypt(pass_hash.read())
ConnStr = "amqp://airflow:" + decrypted_pass  + "@localhost:5672//"

當我運行腳本時,它在 /usr/lib64/python2.7/site-packages/cryptography/fernet.py 或/usr/lib64/python2.7/site-packages/cryptography下的任何地方都失敗了

我們嘗試重新安裝 package 加密,但這沒有幫助

和想法可能是什么?

python  security_test.py
Traceback (most recent call last):
  File "security_test.py", line 14, in <module>
    decrypted_pass = f.decrypt(pass_hash.read())
  File "/usr/lib64/python2.7/site-packages/cryptography/fernet.py", line 75, in decrypt
    return self._decrypt_data(data, timestamp, ttl)
  File "/usr/lib64/python2.7/site-packages/cryptography/fernet.py", line 117, in _decrypt_data
    self._verify_signature(data)
  File "/usr/lib64/python2.7/site-packages/cryptography/fernet.py", line 101, in _verify_signature
    h = HMAC(self._signing_key, hashes.SHA256(), backend=self._backend)
  File "/usr/lib64/python2.7/site-packages/cryptography/hazmat/primitives/hmac.py", line 31, in __init__
    self._ctx = self._backend.create_hmac_ctx(key, self.algorithm)
  File "/usr/lib64/python2.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 207, in create_hmac_ctx
    return _HMACContext(self, key, algorithm)
  File "/usr/lib64/python2.7/site-packages/cryptography/hazmat/backends/openssl/hmac.py", line 34, in __init__
    key_ptr = self._backend._ffi.from_buffer(key)
TypeError: from_buffer() cannot return the address of the raw string within a str or unicode or bytearray object

重要提示 - 在其他機器上這個腳本工作正常

解決它的最佳方法是什么? 通過刪除所有模塊並重新安裝它們? 或通過重新安裝 python?

如果這是由pip安裝的,則此問題是由於cffi package 過時(我能夠通過在virtualenv中強制安裝cffi 1.5 來重現此問題)。 較新版本的cryptography需要cffi >= 1.8 ,但pip並不總是能正確解決這個問題(取決於各種其他場景)。 您可以pip install -U cffi看看是否可以解決它,但通常您應該強烈考慮在virtualenv內運行 Python 代碼,而不是將包安裝到全局 ZEFE90A8E604A7C840E88D03AZ87 空間中。 操作系統 package 管理器假定它擁有全局包,如果您將分發包與pip安裝混合和匹配,您可能會導致安裝出現許多問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM