【问题标题】:How can I decode a base64 string using a custom letter set?如何使用自定义字母集解码 base64 字符串?
【发布时间】:2021-03-04 18:47:52
【问题描述】:

我知道问题here 告诉我如何使用答案中提供的方法进行编码。

但是我知道如何在运行文件以编码一些文本后解码编码的内容。

这是我要加密的代码。

import base64

print("What would you like to encode? ")
data = input()

std_base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/="
custom = "abzdefghkjolsniprqmtuvwyxcDBCAEGFHJKILMNOPTRSQUVXWZY0629851743-&"

x = base64.b64encode(data.encode())
print('Here is your code!: ' + str(x)[2:-1].translate(str(x)[2:-1].maketrans(std_base64chars, custom)))

我将如何解码上述代码行的输出/加密文本?

输出示例。

What would you like to encode? 
Hello World!
Here is your code!: mgvSBg4Fv23ZBgrH  <-- How do I decode that?

【问题讨论】:

  • 所以你去数据->标准base64编码->字符集翻译->编码数据,那么明显的反向操作不会是编码数据->反向翻译->标准base64解码->数据?
  • @ilkkachu 所以是这样的吗? print('这是你的代码!:' + str(x)[2:-1].maketrans(str(x)[2:-1].translate(custom, std_base64chars)))
  • @ilkkachu 交换这两个给我以下。你想解码什么? mgvSBg4Fv23ZBgrHkdPe 这是您的代码!:\Y3A\Y0B\YD2\Y01\Y0E\Y05\YBFS\YD3\Y01\N\YZ7\Y36\YD9\YDE 这看起来不对。同时改变定义和它们在打印代码中出现的顺序,给出了同样的事情。

标签: python base64 decode


【解决方案1】:

你只需要反转你的步骤。

转换正在交换您的字符集,因此请将它们交换回来。
现在您有了一个 base64 编码的字符串,可以将其解码为人类可读的内容。

data = input()
x = str(data).translate(str(data).maketrans(custom, std_base64chars))
print(base64.b64decode(x).decode())

【讨论】:

  • 谢谢,这有效。我不知道为什么我没有想到这一点,但现在我知道了,以备后用。
  • 你好,如果我不知道自定义字母集,有没有解码编码字符串的方法?
猜你喜欢
  • 1970-01-01
  • 2013-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-03
相关资源
最近更新 更多