天天看點

Python爬蟲加密即拿即用【Base64加密、解密】

Python爬蟲加密即拿即用【Base64加密、解密】

"""
base64加密
# 被編碼的參數必須是二進制資料
Base64編碼是一種“防君子不防小人”的編碼方式。廣泛應用于MIME協定,作為電子郵件的傳輸編碼,生成的編碼可逆,後一兩位可能有“=”,生成的編碼都是ascii字元。
優點:速度快,ascii字元,肉眼不可了解
缺點:編碼比較長,非常容易被破解,僅适用于加密非關鍵資訊的場合
"""

import base64

s = 'what the help'
base64_s = 'd2hhdCB0aGUgaGVscA=='


def base64_encrypt(text):
    text_format = text.encode("utf-8")
    result = base64.b64encode(text_format).decode()
    return result


def base64_decrypt(base64_text):
    result = base64.b64decode(base64_text).decode("utf-8")
    return result


base64_test = base64_encrypt(s)
print(base64_test)  # d2hhdCB0aGUgaGVscA==
rebase64_test = base64_decrypt(base64_s)
print(rebase64_test)  # what the help