【问题标题】:Split encrypted string and xor it with a shorter string at every point拆分加密字符串并在每个点用较短的字符串对其进行异或
【发布时间】:2014-02-10 22:20:00
【问题描述】:

我是 Python 新手,但有相当多的编程经验(虽然我也不知道如何用另一种语言来做到这一点)。

我有一个很长的加密十六进制字符串,我想做的是在每个位置用the 对那个字符串进行异或,这样我在每个点都从加密字符串中异或一个相等长度的字符串。

例如,如果我将字符串 12104c06134e5709the 设为 74484520,我想这样做:

  • 12104c06异或74484520
  • 2104c061异或74484520
  • 104c0613 异或74484520

(等等...)

此时我并不太关心结果是如何存储的,我想一个数组就可以了(我认为是 Python 中的一个列表)。

这可能吗?感谢所有帮助。

【问题讨论】:

  • 我确实知道如何做异或部分;我应该提到这一点。我需要帮助的主要是分裂,但谢谢。
  • 另一个问题:对于您的示例,是否应该计算 9 个 XOR?我正在为你解答。
  • 是的,没错。谢谢!

标签: python encryption cryptography xor


【解决方案1】:
def iterXOR(hexString, otherString):

    #Convert otherString to a hex (the 16 is for base 16)
    otherNum = int(otherString, 16)

    #Iterate over the hexString
    for i in range(0, len(hexString) - len(otherString) + 1):

        #Grab the substring of length N beginning at index i
        #Where N is the length of otherString and i is the index
        subString = hexString[i:i+len(otherString)]

        #Convert the substring to a hex number
        hexNum = int(subString, 16)

        #Print the product of the XOR (or do whatever you want here)
        print hex(hexNum ^ otherNum)


#Usage
iterXOR("12104c06134e5709", "74484520")

输出:

>>> 
0x66580926
0x554c8541
0x64044333
0x70882414
0x384e566e
0xb42971c5
0x725b0b77
0x157ca050
0x67061229

【讨论】:

  • 这绝对有效,但我试图在函数结束时将它解码回 ascii 并且它抛出一个错误......知道吗?我基本上在最后添加了这个:xor = hex(hexNum ^ otherNum) // print xor.decode("hex")
  • 您使用的是 Python 3 吗?我知道 Python 3 中没有 .decode("hex")
  • 我实际上是在 2.7.6
猜你喜欢
  • 2021-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-12
  • 1970-01-01
相关资源
最近更新 更多