【发布时间】:2019-03-20 09:52:09
【问题描述】:
免责声明:这是大学作业的一部分
我收到了以下 AES-128-CBC 密钥,并告诉我密钥中最多 3 位已更改/损坏。
d9124e6bbc124029572d42937573bab4
提供原始密钥的 SHA-1 哈希;
439090331bd3fad8dc398a417264efe28dba1b60
我必须通过尝试最多 3 位翻转的所有组合来找到原始密钥。
据推测,这在 349633 次猜测中是可能的,但我不知道这个数字是从哪里来的;我会假设它会更接近 128*127*126,这将超过 2M 组合,这就是我的第一个问题所在。
其次,我在下面创建了包含三重嵌套循环的 python 脚本(我知道,远非最好的代码......)来迭代所有 2M 的可能性,但是,在一个小时后完成后,它没有找到我真正的匹配不明白。
希望有人能至少指出我正确的方向,干杯
#!/usr/bin/python2
import sys
import commands
global binary
def inverseBit(index):
global binary
if binary[index] == "0":
return "1"
return "0"
if __name__ == '__main__':
if len(sys.argv) != 3:
print "Usage: bitflip.py <hex> <sha-1>"
sys.exit()
global binary
binary = ""
sha = str(sys.argv[2])
binary = str(bin(int(sys.argv[1], 16)))
binary = binary[2:]
print binary
b2 = binary
tries = 0
file = open("shas", "w")
for x in range(-2, 128):
for y in range(-1,128):
for z in range(0,128):
if x >= 0:
b2 = b2[:x] + inverseBit(x) + b2[x+1:]
if y >= 0:
b2 = b2[:y] + inverseBit(y) + b2[y+1:]
b2 = b2[:z] + inverseBit(z) + b2[z+1:]
#print b2
hexOut = hex(int(b2,2))
command = "echo -n \"" + hexOut + "\" | openssl sha1"
cmdOut = str(commands.getstatusoutput(command))
cmdOut = cmdOut[cmdOut.index('=')+2:]
cmdOut = cmdOut[:cmdOut.index('\'')]
file.write(str(hexOut) + " | " + str(cmdOut) + "\n")
if len(cmdOut) != 40:
print cmdOut
if cmdOut == sha:
print "Found bit reversals in " + str(tries) + " tries. Corrected key:"
print hexOut
sys.exit()
b2 = binary
tries = tries + 1
if tries % 10000 == 0:
print tries
编辑:
将 for 循环更改为
for x in range(-2, 128):
for y in range(x+1,128):
for z in range(y+1,128):
在(我认为?)仍然覆盖整个空间的同时,大大减少了猜测的数量。仍然得到一些重复,但仍然没有运气找到匹配项..
【问题讨论】:
标签: python encryption aes sha