如何使用自定义字母集对base64进行编码? [英] How can I base64 encode using a custom letter set?

查看:84
本文介绍了如何使用自定义字母集对base64进行编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python3中的自定义字符集对base64进行编码.我在SO中看到的大多数示例都与Python 2有关,因此我不得不对代码进行一些小的调整.我面临的问题是我要用 _ 替换字符/,但是它仍在使用/打印.我的代码是:这仅是一个示例,我不尝试仅使用urlsafe字符作为base64. custom 可以是长度正确的任何东西.

I am trying to base64 encode using a custom character set in python3. Most of the examples I have seen in SO are related to Python 2, so I had to make some minor adjustments to the code. The issue that I am facing is that I am replacing the character / with _, but it is still printing with /. My code is: This is just an example, i am not trying to only base64 with urlsafe chars. custom could be anything with the correct length.

import base64

data = 'some random? data'
print(base64.b64encode(data.encode()))

std_base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
custom = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"

data = data.translate(str.maketrans(custom, std_base64chars)).encode()

print(base64.b64encode(data))

# Both prints
b'c29tZSByYW5kb20/IGRhdGE='
b'c29tZSByYW5kb20/IGRhdGE='

如何使翻译生效,以便将/的出现正确地替换为 _ ?

How can I get the translation to work so that occurrences of / are replaced correctly with _?

我应该明确指出,我并不是在尝试仅执行一种类型的base64编码(如urlsafe),而是尝试任何可能的字符集.这是用户可以传递自己的字符集的功能.我正在通过字符映射而不是字符串切片来寻找字符.

I should make it clear that I am not trying to do only one type of base64 encoding here like urlsafe, but any possible character set. This will be a function were a user can pass their own charset. I am looking for a character by character mapping, not string slicing.

由于我对问题的清楚程度感到困惑,因此我尝试添加更多详细信息.

Because there is some confusion around the clarity of my question, I am try to add more details.

我正在尝试编写一个函数,该函数可以从用户那里获取任意字符集,然后在进行base64编码之前分别映射它们.大多数答案都围绕着操纵 altchars 或字符串切片和替换,但这并不能解决所有需求.

I am trying to write a function that can take an arbitrary charset from a user, and then map them individually before base64 encoding. Most of the answers have been around manipulating altchars or string slice and replace, but that doesnt solve all the needs.

例如,itoa64字符集为:./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz = 或unix加密格式为./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .答案虽然正确,但不能解决这些情况.

So for example, the itoa64 charset is: ./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz= or unix crypt format is ./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz. The answers although correct, does not address these situations.

推荐答案

如果要切换的唯一字符是 + \ ,则可以使用 base64.urlsafe_b64encode 替换为-和分别是 _ .

If the only characters you want to switch are + and \, you can use base64.urlsafe_b64encode to replace with - and _ respectively.

>>> base64.urlsafe_b64encode(data.encode())
b'c29tZSByYW5kb20_IGRhdGE='

或者,您可以使用 base64.b64编码:

>>> base64.b64encode(data.encode(), '*&'.encode())
b'c29tZSByYW5kb20&IGRhdGE='

如果您需要使用全新的字母,可以这样做

If you need to use an entirely new alphabet, you can do

import base64

data = 'some random? data'
print(base64.b64encode(data.encode()))

std_base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
custom = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"


x = base64.b64encode(data.encode())
print(bytes(str(x)[2:-1].translate(str(x)[2:-1].maketrans(std_base64chars, custom)), 'utf-8'))

哪个输出:

b'c29tZSByYW5kb20/IGRhdGE='
b'C29TzsbYyw5KB20_igrHDge='

这篇关于如何使用自定义字母集对base64进行编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆