本文最后更新于 331 天前,其中的信息可能已经有所发展或是发生改变。
过程
word: test
character | bin |
---|---|
A-Za-z0-9+/ | 0-63 |
each character | ascii | bin | 8 length |
---|---|---|---|
t | 116 | 1110100 | 01110100 |
e | 101 | 1100101 | 01110100 |
s | 115 | 1110011 | 01110011 |
t | 116 | 1110100 | 01110100 |
将每个字符 8 位 2进制 ASCII code 拼接在一起
001110100001110100001110011001110100
每六位切割,替换为 base64 table 字符
当不足6位时,补零
6 length | character |
---|---|
011101 | d |
000110 | G |
010101 | V |
110011 | z |
011101 | d |
000000 | A |
dGVzdA==
from string import ascii_uppercase, ascii_lowercase, digits
base64_table = ascii_uppercase + ascii_lowercase + digits + '+/'
def encode(words: str) -> str: # base64 encode
strings = ''.join([bin(ord(_)).replace('0b', '').rjust(8, '0') for _ in words])
strings += '0' * ((len(strings) // 6 + 1) * 6 - len(strings)) if len(strings) % 6 != 0 else ''
return ''.join([base64_table[int(strings[6 * i: 6 * (i + 1)], 2)] for i in range(len(strings) // 6)])
def decode(base64: str) -> str: # base64 decode
strings = ''.join([bin(base64_table.index(_)).replace('0b', '').rjust(6, '0') for _ in base64])
decoded = ''.join([chr(int(strings[8 * _: 8 * (_ + 1)], 2)) for _ in range(len(strings) // 8)])
return decoded
def base_steg(base64: list) -> str: # base64 steg
steg_string, steg_msg = '', ''
for i in base64:
i = i.replace('=', '')
strings = ''.join([bin(base64_table.index(_)).replace('0b', '').rjust(6, '0') for _ in i])
steg_string += strings[(len(strings) - len(strings) // 8 * 8) * -1:] if len(strings) % 8 != 0 else ''
for i in range(len(steg_string) // 8):
cha = chr(int(steg_string[i * 8: (i + 1) * 8], 2))
steg_msg += cha if cha in base64_table else ''
return steg_msg