首页 > 解决方案 > 我该如何解决“binascii.Error:不正确的填充”

问题描述

我有以下代码:

import base64
from cryptography.hazmat.primitives.kdf.scrypt import Scrypt
from cryptography.hazmat.backends import default_backend
from cryptography.fernet import Fernet


def DeriveKey(passwordParam):
    if type(passwordParam) == str:
        passwordParam = passwordParam.encode("utf-8")
    keyDerivationFunction = Scrypt(
        salt=b'ABCDEFGHIJKLMNOP',
        length=32,
        n=2**14,
        r=8,
        p=1,
        backend=default_backend()
    )
    deriveKEY = keyDerivationFunction.derive(passwordParam)
    key = base64.urlsafe_b64decode(deriveKEY)
    return key

def Encrypt(chunkParam, passwordParam: str):
    key = DeriveKey(passwordParam)
    fernet = Fernet(key)
    encryptedChunk = fernet.encrypt(chunkParam)
    return encryptedChunk


def Decrypt(chunkParam, passwordParam: int):
    key = DeriveKey()
    fernet = Fernet(key)
    decryptedChunk = fernet.decrypt((chunkParam))
    return decryptedChunk



def EncryptFile(fileNameParam: str, passwordParam: str) -> None:
    with open(fileNameParam, "rb") as fileObject:
        fileContent = fileObject.read()
        encryptedFileContent = Encrypt(fileContent, passwordParam)
    with open(f"{fileNameParam}.enc", "wb") as fileObject:
        fileObject.write(encryptedFileContent)


def DecryptFile(fileNameParam: str, passwordParam: str) -> None:
    with open(fileNameParam, "rb") as fileObject:
        fileContent = fileObject.read()
        decryptedFileContent = Decrypt(fileContent, passwordParam)
    with open(f"{fileNameParam}.dec", "wb") as fileObject:
        fileObject.write(decryptedFileContent)

command = input("(E)ncrypt or (D)ecrypt ?").upper()
password=(input("Type password: "))
fileName = input("type filename: ")

if command == "E":
    EncryptFile(fileName, password)
elif command ==  "D":
    DecryptFile(fileName, password)

我收到以下错误消息:

Traceback (most recent call last):
  File "C:/Users/mucah/PycharmProjects/FileEncrytDecrypt/main.py", line 57, in <module>
    EncryptFile(fileName, password)
  File "C:/Users/mucah/PycharmProjects/FileEncrytDecrypt/main.py", line 40, in EncryptFile
    encryptedFileContent = Encrypt(fileContent, passwordParam)
  File "C:/Users/mucah/PycharmProjects/FileEncrytDecrypt/main.py", line 24, in Encrypt
    fernet = Fernet(key)
  File "C:\Users\mucah\PycharmProjects\FileEncrytDecrypt\venv\lib\site-packages\cryptography\fernet.py", line 34, in __init__
    key = base64.urlsafe_b64decode(key)
  File "C:\Users\mucah\AppData\Local\Programs\Python\Python38-32\lib\base64.py", line 133, in urlsafe_b64decode
    return b64decode(s)
  File "C:\Users\mucah\AppData\Local\Programs\Python\Python38-32\lib\base64.py", line 87, in b64decode
    return binascii.a2b_base64(s)
binascii.Error: Incorrect padding

怎么了?

标签: pythonpython-3.xbase64

解决方案


此脚本中有两个错误:

  1. 您想调用urlsafe_b64encode而不是调用urlsafe_b64decodescrypt 派生的输出。Fernet 需要一个 urlsafe base64 编码的密钥。
  2. 解密时,您忘记将 passwordParam 传递给您的密钥派生函数(并且您的参数类型错误)

推荐阅读