1. 程式人生 > >python3的base64編解碼

python3的base64編解碼

-s == base64編碼 des 都是 itl bit 實現 python3

使用python3的base64編解碼實現字符串的簡易加密解密

引言:

  在一些項目中,接口的報文是通過base64加密傳輸的,所以在進行接口自動化時,需要對所傳的參數進行base64編碼,對拿到的響應報文進行解碼;

Base64編碼是一種“防君子不防小人”的編碼方式。廣泛應用於MIME協議,作為電子郵件的傳輸編碼,生成的編碼可逆,後一兩位可能有“=”,生成的編碼都是ascii字符。
優點:速度快,ascii字符,肉眼不可理解
缺點:編碼比較長,非常容易被破解,僅適用於加密非關鍵信息的場合

import base64

copyright = Copyright (c) 2012 Doucube Inc. All rights reserved.
def main(): #轉成bytes string bytesString = copyright.encode(encoding="utf-8") print(bytesString) #base64 編碼 encodestr = base64.b64encode(bytesString) print(encodestr) print(encodestr.decode()) #解碼 decodestr = base64.b64decode(encodestr) print(decodestr.decode())
if __name__ == __main__: main()

運行結果:

*** Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on win32. ***
>>>
*** Remote Interpreter Reinitialized ***
>>>
b‘Copyright (c) 2012 Doucube Inc. All rights reserved.‘
b‘Q29weXJpZ2h0IChjKSAyMDEyIERvdWN1YmUgSW5jLiBBbGwgcmlnaHRzIHJlc2VydmVkLg==‘


Q29weXJpZ2h0IChjKSAyMDEyIERvdWN1YmUgSW5jLiBBbGwgcmlnaHRzIHJlc2VydmVkLg==
Copyright (c) 2012 Doucube Inc. All rights reserved.
>>>

精簡版

import base64

print(base64.b64decode(b‘Q29weXJpZ2h0IChjKSAyMDEyIERvdWN1YmUgSW5jLiBBbGwgcmlnaHRzIHJlc2VydmVkLg==‘).decode())

python3的base64編解碼