Преобразуйте шестнадцатеричный код в base64, а затем исправьте XOR

Я видел на сайте вопросы, где слишком простые задачи можно сгруппировать в одну.

Вот вызовы 1 и 2 от Crypto Challenge Set 1 в Python. Какие-либо предложения?

the_hex = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"


base64_table = {
    'A': '000000',
    'B': '000001',
    'C': '000010',
    'D': '000011',
    'E': '000100',
    'F': '000101',
    'G': '000110',
    'H': '000111',
    'I': '001000',
    'J': '001001',
    'K': '001010',
    'L': '001011',
    'M': '001100',
    'N': '001101',
    'O': '001110',
    'P': '001111',
    'Q': '010000',
    'R': '010001',
    'S': '010010',
    'T': '010011',
    'U': '010100',
    'V': '010101',
    'W': '010110',
    'X': '010111',
    'Y': '011000',
    'Z': '011001',
    'a': '011010',
    'b': '011011',
    'c': '011100',
    'd': '011101',
    'e': '011110',
    'f': '011111',
    'g': '100000',
    'h': '100001',
    'i': '100010',
    'j': '100011',
    'k': '100100',
    'l': '100101',
    'm': '100110',
    'n': '100111',
    'o': '101000',
    'p': '101001',
    'q': '101010',
    'r': '101011',
    's': '101100',
    't': '101101',
    'u': '101110',
    'v': '101111',
    'w': '110000',
    'x': '110001',
    'y': '110010',
    'z': '110011',
    '0': '110100',
    '1': '110101',
    '2': '110110',
    '3': '110111',
    '4': '111000',
    '5': '111001',
    '6': '111010',
    '7': '111011',
    '8': '111100',
    '9': '111101',
    '+': '111110',
    "https://codereview.stackexchange.com/": '111111'
}

hex2bin_table = {
    '0': '0000',
    '1': '0001',
    '2': '0010',
    '3': '0011',
    '4': '0100',
    '5': '0101',
    '6': '0110',
    '7': '0111',
    '8': '1000',
    '9': '1001',
    'a': '1010',
    'b': '1011',
    'c': '1100',
    'd': '1101',
    'e': '1110',
    'f': '1111'
}


def hex_to_base64(the_hex):
    hex_in_bin = ''

    for c in the_hex:
        # to_ascii = ord(c)  # if it's text to base64
        # print(c, to_ascii, bin(to_ascii)[2:].zfill(8), hex2bin_table[c])
        # hex_in_bin += bin(to_ascii)[2:].zfill(8)
        hex_in_bin += hex2bin_table[c]

    # switch keys and values only for unique values
    reversed_table = dict(zip(base64_table.values(), base64_table.keys()))

    final = ""
    for c in range(0, len(hex_in_bin), 6):
        x = hex_in_bin[c:c + 6]

        if len(x) == 6:
            final += reversed_table[x]
        else:
            # pad with 0s to 6
            final += reversed_table[x.ljust(6, '0')]

    len_to_fill_final = 0
    if len(the_hex) % 3 != 0:
        # see Output padding from wiki, base64
        if len(final) % 4 != 0:
            len_to_fill_final = len(final)
            while len_to_fill_final % 4 != 0:
                len_to_fill_final += 1

    return final.ljust(len_to_fill_final, "=")


print(hex_to_base64(the_hex))
print("SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t")



hex1 = '1c0111001f010100061a024b53535009181c'
hex2 = '686974207468652062756c6c277320657965'
expected_result="746865206b696420646f6e277420706c6179"


# hex to bin
def hex_2_bin(hex_string):
    hex2bin_temp = ""
    for c in hex_string:
        hex2bin_temp += hex2bin_table[c]

    return hex2bin_temp


hex2bin_temp1 = hex_2_bin(hex1)
hex2bin_temp2 = hex_2_bin(hex2)

len_hex1 = len(hex2bin_temp1)
len_hex2 = len(hex2bin_temp2)

xored_binary = ""
for index in range(len_hex1):
    a = int(hex2bin_temp1[index])
    b = int(hex2bin_temp2[index])
    c = a ^ b
    xored_binary = "".join((xored_binary, str(c)))


# again convert keys to values for hex to bin table
reversed_bin_table = dict(zip(hex2bin_table.values(), hex2bin_table.keys()))
finale = ""
for x in range(0, len(xored_binary), 4):
    h = reversed_bin_table[xored_binary[x:x + 4]]
    finale = "".join((finale, h))


print(finale)
print(expected_result)

0

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *