[英]Incorrect Padding error while decoding base64 encoding
我試圖解碼作為BLOB存儲的PDF,並將其保存到擴展名為.pdf的文件中。 results [0] [1]具有從數據庫查詢中提取的BLOB數據。
blob_val=results[0][1]
if len(blob_val) % 4 != 0:
while len(blob_val) % 4 != 0:
blob_val = blob_val + b"="
decod_text = base64.b64decode(blob_val)
else:
decod_text = base64.b64decode(blob_val)
即使我在末尾添加了=來糾正填充錯誤,它仍然顯示不正確的填充錯誤。 為什么即使我們通過“ =”進行了更正,它仍然顯示此錯誤?
每個base64字符都編碼六個位。 為此,總字節數應被三整除,而不是四整。
這應該可以工作(並簡化一下):
blob_val = results[0][1]
# If the length is divisible by 3, the 'while' will never
# be entered, so no point in doing the additional 'if' above.
while len(blob_val) % 3 != 0:
blob_val += b"="
decod_text = base64.b64decode(blob_val)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.