逆向常用python程式碼

noahze發表於2024-03-26

字串轉陣列列表

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)

十六進位制字串轉換為十進位制整數

hex_string = "1A3F"  # 十六進位制字串

# 將十六進位制字串轉換為十進位制整數
decimal_number = int(hex_string, 16)

# 將十進位制整數轉換為十六進位制字串
hex_result = hex(decimal_number)

print("轉換後的十進位制數:", decimal_number)
print("轉換後的十六進位制字串:", hex_result)

相關文章