bystartw_1

Base64 編碼 & 解碼程序

Aug 24th, 2017
1,170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.41 KB | None | 0 0
  1. import base64 # 匯入 base64 模組
  2. import time
  3. import os
  4. if os.name == "nt": # 如果系統是 Windows
  5.     os.system ("title Base64Tool") # cmd 設定標題
  6.     os.system ("cls") # Windows 底下,清除螢幕的指令為 cls
  7. else:
  8.     os.system ("clear") # Mac & Linux 底下,清除螢幕的指令為 clear
  9. print ("\n\n\n") # Menu
  10. print ("1. 編碼字串至 Base64")
  11. print ("2. 解碼字串至 Base64")
  12. print ("3. 離開")
  13. option = input ("請輸入你想要的操作: ")
  14. if option == "1": # 如果使用者選擇 '1'
  15.     b64encode = input ("請輸入欲編碼字串: ")
  16.     encode_result = base64.b64encode(bytes(b64encode, 'utf-8')) # 先將使用者輸入之字串轉換為 bytes (以 utf-8 編碼)
  17.     encode_result = str(encode_result) # 將編碼結果重新轉換為 str.
  18.     print (b64encode + " 已編碼為 \nBase64: ", end="")
  19.     print(str(encode_result.lstrip('b'))) # 將原結果前面的 'b' 去除
  20. elif option == "2": # 如果使用者選擇 '2'
  21.     b64decode = input ("請輸入欲解碼字串 (base64): ")
  22.     decode_result = base64.b64decode(bytes(b64decode, 'utf-8')) # 先將使用者輸入之字串轉換為 bytes (以 utf-8 編碼)
  23.     decode_result = str(decode_result, 'utf-8') # 將解碼結果重新轉換為 str.
  24.     print (b64decode + " 已解碼成 \n" + decode_result)
  25. else: # 如果使用者輸入 3 or other.
  26.     print ("你沒有任何動作.")
  27.  
  28. # creator: byStarTW
Advertisement
Add Comment
Please, Sign In to add comment