Python 解码Base64 得到码流格式文本实例

时间:2022-10-11 20:08:00

我就废话不多说了,直接上代码吧!

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# coding:utf8
import base64
 
 
def BaseToFlow():
  while True:
    str = input("Please input src: ")
    flag = input("Please input Decode - 1 or Encode - 2: ")
    if str == "":
      str = "ApIAGBcEAAAEBO6x3nLykEEhjWMX1wHs"
    if flag == "":
      flag = "1"
    if flag == "1":
      print("Decoding ...")
      dst = base64.b64decode(str)
      # print(type(dst))
      # <class 'bytes'>
      # print(dst)
      # b'\x02\x92\x00\x18\x17\x04\x00\x00\x04\x04\xee\xb1\xder\xf2\x90A!\x8dc\x17\xd7\x01\xec'
      # print(dst.hex()) --去掉\0x前缀 得到一个字符串
      # 02920018170400000404eeb1de72f29041218d6317d701ec
      HexFormat(dst.hex())
    elif flag == "2":
      print("Encoding ...")
      dst = base64.b64encode(str)
      print(dst)
 
 
def HexFormat(str):
  """
  :param str: 16进制连续字符串
  :return: 码流格式的16进制串
  """
  i = 1
  str2 = ""
  while (i <= len(str)):
    str2 = str2 + str[i - 1] + str[i] + " "
    if (i + 1) %16 == 0 and (i + 1) % 32 != 0:
      str2 = str2 + " "
    elif (i + 1) %32 == 0:
      str2 = str2 + "\n"
    i = i + 2
 
 
if __name__ == '__main__':
  BaseToFlow()

结果:

?
1
2
3
4
5
Please input src:
Please input Decode - 1 or Encode - 2:
Decoding ...
02 92 00 18 17 04 00 00 04 04 ee b1 de 72 f2 90
41 21 8d 63 17 d7 01 ec

以上这篇Python 解码Base64 得到码流格式文本实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_27468251/article/details/82899143