字符串转数组列表
def strTolist(str):
ans = list(str)
for i in range(len(ans)):
ans[i] = ord(ans[i])
return ans
str='Hello'
print(strTolist(str))
#输出:[72, 101, 108, 108, 111]
16进制字符串转字符串
def hexstrTostr(hexstr):
byte_str = bytes.fromhex(hexstr)
return byte_str.decode("utf-8")
hexstr = "48656c6c6f"
print(hexstrTostr(hexstr))
# 输出:Hello
# 0x48->H 0x65->e 0x6c->l 0x6c->l 0x6f->0
字符串中大写转小写
strs.lower()
字符串中大写转小写
strs.upper()
字符串大小写置换
def ulsub(old_strs):
new_strs = ''
for str in old_strs:
if str.isupper():
new_strs += chr(ord(str) + 32)
elif str.islower():
new_strs += chr(ord(str) - 32)
else:
new_strs += str
return new_strs
倒序输出
for i in range(10,1,-1):
print(i)
#输出:
#10
#9
#8
#7
#6
#5
#4
#3
#2
列表反转
data_list[::-1]
数组列表通过ASCII码转为字符串
def listTostr(table):
ans = ''
for item in table:
ans = ans + chr(item)
return ans
lt = [0x48,0x65,0x6c,0x6c,0x6f]
print(listTostr(lt))
#输出:Hello
base64换表
def base64_encode(message,base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"):
# 将字符串编码为字节串
message_bytes = message.encode('utf-8')
# 计算需要补齐的字节数
padding = 3 - (len(message_bytes) % 3)
if padding == 3:
padding = 0
# 补齐字节串
message_bytes += b'\x00' * padding
# 将每3个字节编码为4个Base64字符
encoded_chars = []
for i in range(0, len(message_bytes), 3):
# 3个字节转换为一个24位整数
value = (message_bytes[i] << 16) + (message_bytes[i + 1] << 8) + message_bytes[i + 2]
# 拆分24位整数为4组6位
for j in range(4):
index = (value >> ((3 - j) * 6)) & 63
encoded_chars.append(base64_chars[index])
# 根据补齐数添加 '=' 字符
for i in range(padding):
encoded_chars[-(i + 1)] = '='
# 将字符列表转换为字符串并返回
return ''.join(encoded_chars)
def base64_decode(encoded_message,base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"):
# 初始化解码字节串
decoded_bytes = bytearray()
# 解码每4个Base64字符为3个字节
for i in range(0, len(encoded_message), 4):
# 将4个Base64字符映射回6位整数
value = 0
for j in range(4):
if encoded_message[i + j] == '=':
value <<= 6
else:
value <<= 6
value += base64_chars.index(encoded_message[i + j])
# 将6位整数拆分为3个字节
for j in range(2, -1, -1):
decoded_bytes.append((value >> (j * 8)) & 255)
# 去除补齐字节
while decoded_bytes[-1] == 0:
decoded_bytes.pop()
# 将字节串解码为字符串并返回
ans = None
try:
ans = decoded_bytes.decode('utf-8')
except Exception as e:
print("[*]解码失败")
return ans
message = "Hello, World!"
encoded_message = base64_encode(message,base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
print("Encoded:", encoded_message)
decoded_message = base64_decode(encoded_message,base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
print("Decoded:", decoded_message)
16进制字符串转10进制整数
hexstr = "1A3F" # 十六进制字符串
decimal_number = int(hexstr, 16)
print(decimal_number)
#输出: 6719
#0x1A3F == 6719
10进制整数转16进制字符串
hex_result = hex(6719)
print(hex_result)
#输出: 0x1a3f
#0x1A3F == 6719
标签:逆向,python,代码,base64,bytes,str,ans,encoded,message
From: https://www.cnblogs.com/noahze/p/18096681