我已经从正在研究的挑战二进制文件中反转了以下算法:

def encrypt(plain):
    l = len(plain)
    a = 10
    cipher = ""

    for i in range(0, l):
        if i + a < l - 1:
            cipher += chr( xor(plain[i], plain[i+a]) )
        else:
            cipher += chr( xor(plain[i], plain[a]) )

        if ord(plain[i]) % 2 == 0: a += 1 # even
        else: a -= 1 # odd

    return cipher

from binascii import hexlify
print hexlify(encrypt("this is a test string"))

实际上,它将每个字符与字符串中的另一个字符进行异或,偏移量为aa初始值是10,当函数在字符串中的字符上迭代时,如果字符的值是偶数,则为a +=1;如果字符的值是奇数,则为a -= 1
我已经在脑子里想好了如何反转这个密码并检索纯文本,这需要使用递归函数来找出原始字符串中哪些字符偏移量是偶数/奇数。IE:考虑到XOR%2的属性,我们现在发现如果cipher[0]是奇数,那么plain[0]plain[10]都是奇数,但不是两者都是类似地,如果cipher[0]是偶数,那么plain[0]plain[10]都是偶数,或者都是奇数从那里一个递归算法应该可以工作的其余部分。
一旦我们知道明文中的哪些字符是偶数/奇数,就可以轻松地反转其余字符。我花了几个小时来解决这个问题,但现在我却不知所措。
我以前用过基本的递归算法,但从来没有用“分支”来解决类似的问题。
给定由该函数产生的cipher字符串,我们如何使用递归算法来确定原始纯字符串中每个字符的奇偶性?
编辑:很抱歉,我只是想澄清一下,并且在回答一个评论时,在几个小时的挠头之后,我认为上面概述的递归策略将是解决这个问题的唯一方法。如果没有,我愿意接受任何提示/帮助来解决标题问题。

最佳答案

你可以用recursive backtracking来解决这个问题做一个假设,然后沿着这条路走,直到你解密了字符串或者你遇到了矛盾当遇到矛盾时,返回failure,调用函数将尝试下一种可能性。如果返回success,则将success返回给调用方。
很抱歉,但我忍不住要解决这个问题。我想到的是:

# Define constants for even/odd/notset so we can use them in a list of
# assumptions about parity.
even = 0
odd = 1
notset = 2

# Define success and failure so that success and failure can be passed
# as a result.
success = 1
failure = 0

def tryParity(i, cipher, a, parities, parityToSet):
    newParities = list(parities)
    for j, p in parityToSet:
        try:

            if parities[j] == notset:
                newParities[j] = p
            elif parities[j] != p:
                # Failure due to contradiction.
                return failure, []

        except IndexError:
            # If we get an IndexError then this can't be a valid set of values for the parity.
            # Error caused by a bad value for "a".
            return failure, []

    # Update "a" based on parity of i
    new_a = a+1 if newParities[i] == even else a-1

    return findParities(i+1,cipher,new_a,newParities)


def findParities(i, cipher, a, parities):
    # Start returning when you've reached the end of the cipher text.
    # This is when success start bubbling back up through the call stack.
    if i >= len(cipher):
        return success, [parities] # list of parities

    # o stands for the index of the other char that would have been XORed.
    # "o" for "other"
    o = i+a if i + a < len(cipher)-1 else a

    result = None
    resultParities = []
    toTry = []

    # Determine if cipher[index] is even or odd
    if ord(cipher[i]) % 2 == 0:
        # Try both even and both odd
        toTry = (((i,even),(o,even)),
                 ((i,odd),(o,odd)))

    else:
        # Try one or the other even, one or the other odd
        toTry = (((i,odd),(o,even)),
                 ((i,even),(o,odd)))


    # Try first possiblity, if success add parities it came up with to result
    resultA, resultParA = tryParity(i, cipher, a, parities, toTry[0])

    if resultA == success:
        result = success
        resultParities.extend(resultParA)

    # Try second possiblity, if success add parities it came up with to result
    resultB, resultParB = tryParity(i, cipher, a, parities, toTry[1])

    if resultB == success:
        result = success
        resultParities.extend(resultParB)

    return result, resultParities

def decrypt(cipher):
    a = 10
    parities = list([notset for _ in range(len(cipher))])

    # When done, possible parities will contain a list of lists,
    # where the inner lists have the parity of each character in the cipher.
    # Comes back with mutiple results because each
    result, possibleParities = findParities(0,cipher,a,parities)

    # A print for me to check that the parities that come back match the real parities
    print(possibleParities)
    print(list(map(lambda x: 0 if ord(x) % 2 == 0 else 1, "this is a test string")))

    # Finally, armed with the parities, decrypt the cipher. I'll leave that to you.
    # Maybe more recursion is needed

# test call
decrypt(encrypt("this is a test string"))

这似乎很有效,但我没有尝试任何其他输入。
这个解决方案只给你一半,我把字符的解密留给你了。他们也许可以一起做,但我想集中精力回答你提出的问题。我使用python 3是因为它是我安装的。
我也是这方面的初学者我建议你读一本彼得·诺维格的书谢谢你提出这个棘手的问题。

10-06 16:08
查看更多