Skip to content

Instantly share code, notes, and snippets.

@leveryd
Created January 25, 2024 08:48
Show Gist options
  • Save leveryd/fd0f1c53b2aa2341e31d206b5d542baa to your computer and use it in GitHub Desktop.
Save leveryd/fd0f1c53b2aa2341e31d206b5d542baa to your computer and use it in GitHub Desktop.
import re
import base64
class GenerateBase64(object):
@staticmethod
def split_string_by_length(text, length):
# 使用正则表达式匹配长度为指定值的子串
pattern = f'.{{{length}}}'
return [match.group(0) for match in re.finditer(pattern, text)]
def calc(self, s, start_num, end_num):
"""
计算字符串s的base64编码后,掐头去尾后获得字符串
:param s: 字符串
:param start_num:
:param end_num:
:return:
"""
s = base64.b64encode(s).decode("utf-8")
ss = self.split_string_by_length(s, 4)
start = ss[0]
end = ss[-1]
return start[-start_num:] + "".join(ss[1:-1]) + end[:end_num]
def convert(self, term):
"""
padding过程举例:
s = "123"
1. 左边padding零个字符,为了保证长度是三的倍数,右边padding零个字符,得到 "123"
2. 左边padding一个字符,为了保证长度是三的倍数,右边padding两个字符,得到 "X123XX"
3. 左边padding两个字符,为了保证长度是三的倍数,右边padding一个字符,得到 "XX123X"
s = "1234"
1. 左边padding零个字符,为了保证长度是三的倍数,右边padding两个字符,得到 "1234XX"
2. 左边padding一个字符,为了保证长度是三的倍数,右边padding一个字符,得到 "X1234X"
3. 左边padding两个字符,为了保证长度是三的倍数,右边padding零个字符,得到 "XX1234"
s = "12345"
1. 左边padding零个字符,为了保证长度是三的倍数,右边padding一个字符,得到 "12345X"
1. 左边padding一个字符,为了保证长度是三的倍数,右边padding零个字符,得到 "X12345"
2. 左边padding两个字符,为了保证长度是三的倍数,右边padding两字符,得到 "XX12345XX"
padding后计算base64编码的过程举例:
s = "123"
"123" -> "MTIz" calc("X123XX", 4, 4)
"X123XX" -> base64("X12")[-2:] + base64("3XX")[0:1] calc("X123XX", 2, 1)
"XX123X" -> base64("XX1")[-1:] + base64("23X")[0:2] calc("XX123X", 1, 2)
s = "1234"
"1234XX" -> base64("123") + base64("4XX")[0:1] calc("1234XX", 4, 1)
"X1234X" -> base64("X12")[-2:] + base64("34X")[0:2] calc("X1234X", 2, 2)
"XX1234" -> base64("XX1")[-1:] + base64("234") calc("XX1234", 1, 4)
s = "12345"
"12345X" -> base64("123") + base64("45X")[0:2] calc("12345X", 4, 2)
"X12345" -> base64("X12")[-2:] + base64("345") calc("X12345", 2, 4)
"XX12345XX" -> base64("XX1")[-1:] + base64("234") + base64("5XX")[0:1] calc("XX12345XX", 1, 1)
:param term:
:return:
"""
assert isinstance(term, bytes)
assert len(term) >= 3
if len(term) % 3 == 0:
first = self.calc(term, 4, 4)
second = self.calc(b"X" + term + b"XX", 2, 1)
third = self.calc(b"XX" + term + b"X", 1, 2)
return [first, second, third]
if len(term) % 3 == 1:
first = self.calc(term + b"XX", 4, 1)
second = self.calc(b"X" + term + b"X", 2, 2)
third = self.calc(b"XX" + term, 1, 4)
return [first, second, third]
if len(term) % 3 == 2:
first = self.calc(term + b"X", 4, 2)
second = self.calc(b"X" + term, 2, 4)
third = self.calc(b"XX" + term + b"XX", 1, 1)
return [first, second, third]
def convert2(self, term):
"""
:param term:
:return:
"""
return "(" + "|".join(self.convert(term)) + ")"
def convert(term):
"""
:param term:
:return:
"""
ret = []
for i in term.split(b"|"):
ret.append(GenerateBase64().convert2(i))
return "(?i:" + "|".join(ret) + ")"
print(convert(b"\xac\xed\x00\x05"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment