Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.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中解密密码_Python_Flask - Fatal编程技术网

使用密码术在Python中解密密码

使用密码术在Python中解密密码,python,flask,Python,Flask,尝试解密存储在my MongoDB中的密码,但它显示了一个加密.fernet.InvalidToken 这是我的密码 import base64 import os from cryptography.fernet import Fernet from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import hashes from cryptography.haz

尝试解密存储在my MongoDB中的密码,但它显示了一个加密.fernet.InvalidToken
这是我的密码

import base64
import os
from cryptography.fernet import Fernet
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC

sec_key = b"My_message"  # Password Encode Secret Key Before Password Encrypt
salt = os.urandom(32)
kdf = PBKDF2HMAC(
            algorithm=hashes.SHA256(),
            length=32, salt=salt, iterations=100000,
            backend=default_backend()
        )
key = base64.urlsafe_b64encode(kdf.derive(sec_key))
f = Fernet(key)

password = "x7zBCHki"
token = f.encrypt(bytes(password, encoding='utf-8'))
print("Token:", token)
print(type(token))
tek = token.decode()
使用此代码,我将加密密码存储到我的MongoDB中,并知道尝试解密该密码,但显示无效的令牌化。 这是我的加密密钥…


password=“x7zBCHki
加密= GAAAABEDVAINWUIZ9OYT Ze-eSBdtVHWNmDbq\u liREZtpNiPBlKHXIXmjeRe2kHQwHmgBs8tvFqtSVRUJSKxig-mV6qMe78DA==


此加密代码为字符串格式


  • 解密代码



错误消息:


Admin@DESKTOP-48C6LC5 MINGW64/p/WSSP/Blockchain/yourapplication(主应用程序)
$python test.py
回溯(最近一次呼叫最后一次):
文件“P:\WSSP\Blockchain\Blockchain\lib\site packages\cryptography\fernet.py”,第104行,在\u verify\u签名中
h、 验证(数据[-32:])
文件“P:\WSSP\Blockchain\Blockchain\lib\site packages\cryptography\hazmat\primitives\hmac.py”,第66行,在verify中
ctx.验证(签名)
文件“P:\WSSP\Blockchain\Blockchain\lib\site packages\cryptography\hazmat\backends\openssl\hmac.py”,第74行,在verify中
提出无效签名(“签名与摘要不匹配”)
cryptography.exceptions.InvalidSignature:签名与摘要不匹配。
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“test.py”,第30行,在
dec=f.decrypt(字节(tek,encoding='utf-8')).decode()
文件“P:\WSSP\Blockchain\Blockchain\lib\site packages\cryptography\fernet.py”,第75行,解密
返回self.\u解密\u数据(数据、时间戳、ttl)
文件“P:\WSSP\Blockchain\Blockchain\lib\site packages\cryptography\fernet.py”,第117行,在解密数据中
自我验证签名(数据)
文件“P:\WSSP\Blockchain\Blockchain\lib\site packages\cryptography\fernet.py”,第106行,在\u verify\u签名中
提出无效代币
cryptography.fernet.InvalidToken
请帮助我解决此解密方法

注意:当我使用简单消息并加密然后解密时,它运行平稳,但当我从MongoDB获取并解密它时,它会显示这样一个错误。

您的问题是,您使用不同的盐进行加密和解密。 由于您使用的是固定密钥,因此也可以使用一些固定密钥:

import base64
import os
from cryptography.fernet import Fernet
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC

SECRET_KEY = b"My_message"  # Password Encode Secret Key Before Password Encrypt
SALT = b"sAlT"*8

def get_fenec():
    kdf = PBKDF2HMAC(
        algorithm=hashes.SHA256(),
        length=32, salt=SALT, iterations=100000,
        backend=default_backend()
    )
    key = base64.urlsafe_b64encode(kdf.derive(SECRET_KEY))
    return Fernet(key)

def encrypt_password(password):
    f = get_fenec()
    token = f.encrypt(bytes(password, encoding='utf-8'))
    return token.decode()

def decrypt_password(tek):
    f = get_fenec()
    return f.decrypt(bytes(tek, encoding='utf-8')).decode()

password = "x7zBCHki"
tek = encrypt_password(password)

tek = 'gAAAAABeDyduJrAmHBtfCPMAKWwBPYFAFJhCAtPf3EerNt9OVfZIADe_9Pap-DiGK8VF6EQ_w62hUIeqHP9kiHf5u58blIL6qQ=='
decrypted_password = decrypt_password(tek)
但是,由于每个人都可以读取代码中的密钥,因此您可以直接使用fenec密钥:

KEY = b'gcPZx4_UiXtw8Zl0dSCJnY02FKejaMQ-8a40RbLnO0M='

def get_fenec():
    return Fernet(KEY)

您的示例代码适用于我。你用的是同样的盐吗?我不确定,我完全理解你的问题。你说你可以用一些代码加密和解密密码。现在,如果您将加密的密码存储在MongoDB中并尝试解密它,那么它就不起作用了?你能展示你能加密和解密密码的最短代码吗?您是否还可以显示最短的代码段,表明如果使用MongoDB存储和检索密码,则无法对密码进行加密和解密?
import base64
import os
from cryptography.fernet import Fernet
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC

SECRET_KEY = b"My_message"  # Password Encode Secret Key Before Password Encrypt
SALT = b"sAlT"*8

def get_fenec():
    kdf = PBKDF2HMAC(
        algorithm=hashes.SHA256(),
        length=32, salt=SALT, iterations=100000,
        backend=default_backend()
    )
    key = base64.urlsafe_b64encode(kdf.derive(SECRET_KEY))
    return Fernet(key)

def encrypt_password(password):
    f = get_fenec()
    token = f.encrypt(bytes(password, encoding='utf-8'))
    return token.decode()

def decrypt_password(tek):
    f = get_fenec()
    return f.decrypt(bytes(tek, encoding='utf-8')).decode()

password = "x7zBCHki"
tek = encrypt_password(password)

tek = 'gAAAAABeDyduJrAmHBtfCPMAKWwBPYFAFJhCAtPf3EerNt9OVfZIADe_9Pap-DiGK8VF6EQ_w62hUIeqHP9kiHf5u58blIL6qQ=='
decrypted_password = decrypt_password(tek)
KEY = b'gcPZx4_UiXtw8Zl0dSCJnY02FKejaMQ-8a40RbLnO0M='

def get_fenec():
    return Fernet(KEY)