【问题标题】:So I am kinda lost in my code in Crib Drag Attack(For Educational Purposes)所以我有点迷失在婴儿床拖动攻击的代码中(出于教育目的)
【发布时间】:2022-10-05 02:04:26
【问题描述】:

所以我不知道如何将十六进制转换为 ascii 代码。我这样做是出于教育目的。总的来说,我仍在学习密码学和 python。我不知道将十六进制解密为 ASCII 的代码。我转换为十六进制字符串会出错,并且不会从用户那里得到输入。

def crib_drag_attack(guess, cp1, cp2):

xor_ciphers = ""
for idx in range(len(cp1)):
    ic1 = ord(cp1[idx])
    ic2 = ord(cp2[idx])
    ic_xor = ic1 ^ ic2
    xor_ciphers += chr(ic_xor)

for idx in range(len(xor_ciphers) - len(guess)+1):
    slide = xor_ciphers[idx: idx + len(guess)]
    results = ""
    for i in range(len(guess)):
        ig = ord(guess[i])
        id = ord(slide[i])
        ir = ig ^ id
        results += chr(ir)
    print(results)


def encrypt(key, plaintext):
idx = 0  # Declare index (idx) variable
ciphertext = ""  # Declare ciphertext variable
for p in plaintext:  # Take one character at a time in message
    ip = ord(p)  # Convert to Decimal value code
    k = key[idx]  # Take byte value of the key at idx
    ik = ord(k)  # Convert to Decimal value code
    inew = ip ^ ik  # XOR bit-by-bit
    ciphertext += chr(inew)  # Convert to character code and Update ciphertext
    print(p, hex(ip), k, hex(ik), hex(inew))  # print every result
    idx += 1  # Increment idx by 1

hexstring = ciphertext.encode("ascii").hex()
print("\n{} --> {}\n".format(ciphertext, hexstring))

trial = bytes.fromhex(hexstring).decode("ascii")
print("Trial: {}".format(trial))

return ciphertext


def decrypt(key, ciphertext):
idx = 0  # Declare index (idx) variable
plaintext = ""  # Declare plaintext variable
for c in ciphertext:  # Take one character at a time in message
    ic = ord(c)  # Convert to Decimal value code
    k = key[idx]  # Take byte value of the key at idx
    ik = ord(k)  # Convert to Decimal value code
    inew = ic ^ ik  # XOR bit-by-bit
    plaintext += chr(inew)  # Convert to character code and Update ciphertext
    print(c, hex(ic), k, hex(ik), hex(inew))  # print every result
    idx += 1  # Increment idx by 1

print("\n{} --> {}\n".format(plaintext, plaintext.encode("ascii").hex()))
return plaintext


if __name__ == '__main__':

# ciphertext1 = encrypt(key, message1)
# plaintext1 = decrypt(key, ciphertext1)
# #
# ciphertext2 = encrypt(key, message2)
# plaintext1 = decrypt(key, ciphertext2)

# place the given ciphertext 1 and 2 below

#

# Insert the hex string below
ciphertextHex1 = ""
ciphertextHex2 = ""

# Convert the hex string to ascii string
quote_h  = "300d04014"
quote = binascii.a2b_hex("%s" % 
(quote_h.strip())).decode("ASCII").replace(';', '\n- ')
print(quote)

guess = input("Guess a word: ")
crib_drag_attack(guess, ciphertext1, ciphertext2)

【问题讨论】:

    标签: python cryptography


    【解决方案1】:
    猜你喜欢
    • 2010-10-11
    • 2019-05-02
    • 2019-04-29
    • 2015-09-06
    • 2012-09-11
    • 1970-01-01
    • 2018-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多