Base64

Encode Decode and Base64 Steg

Posted by JBNRZ on 2022-12-02
Estimated Reading Time 1 Minutes
Words 313 In Total
Viewed Times

过程

word: test

character bin
A-Za-z0-9+/ 0-63
each character ascii bin 8 length
t 116 1110100 001110100
e 101 1100101 001110100
s 115 1110011 001110011
t 116 1110100 001110100

将每个字符 8 位 2进制 ASCII code 拼接在一起

001110100001110100001110011001110100

每六位切割,替换为 base64 table 字符
当不足6位时,补零

6 length character
001110 d
100001 G
110100 V
001110 z
011001 d
110100 A

dGVzdA

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
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

如果您喜欢此博客或发现它对您有用,则欢迎对此发表评论。 也欢迎您共享此博客,以便更多人可以参与。 如果博客中使用的图像侵犯了您的版权,请与作者联系以将其删除。 谢谢 !