python - 我该如何解决“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
怎么了?
解决方案
此脚本中有两个错误:
- 您想调用
urlsafe_b64encode
而不是调用urlsafe_b64decode
scrypt 派生的输出。Fernet 需要一个 urlsafe base64 编码的密钥。 - 解密时,您忘记将 passwordParam 传递给您的密钥派生函数(并且您的参数类型错误)
推荐阅读
- python - Cython realloc 错误:释放对象的校验和不正确
- java - Kotlin 中的 CountDownTimer 解释
- powershell - 加入两个哈希表来制作一个
- ionic-framework - ionic - 覆盖组件的 SASS 变量不适用于 iOS
- javascript - 如何将重叠矩形的轮廓转换为顶点?
- c++ - 将 SDL_Cursor 与 unique_ptr 一起使用:不允许错误不完整类型
- html - 通过“发布”发布包含图像文件相对路径的报告
- asp.net-core - 从 Angular 5 注销 MVC 应用程序
- java - 多模块java app项目结构
- mercurial - 仅查看来自特定分支的提交