【发布时间】:2020-11-16 11:19:14
【问题描述】:
我试图通过暴力破解剩余的 4 个字符来找到密钥的剩余部分。然后我们使用每个密钥解密,并检查解密后的消息是否在开头有“Salam”。
from Cryptodome.Cipher import AES
def revpad(s):
"""
This function is to remove padding.
parameters:
s:str
string to be reverse padded
"""
k=ord(s[-1])
temp=0 #temporary variable to check padding
for i in range (1,k): #for loop to check padding
if(s[-i]!=s[-1]): #comparision of bytes with the last Byte
temp=1
if(temp==0):
return (s[:-k]) #Reverse padded string
else:
return ("Wrong padding")
def decrypt(ct,key):
"""
This function is for decrypting ciphertext in ECB mode
parameters:
ct: str
ciphertext to be decrypted
key: str
key for ECB mode encryption whose lenght is always a multiple of 16 in this case
"""
plaintxt=AES.new(key,AES.MODE_ECB)
pt=plaintxt.decrypt(ct)
return revpad(pt) #After decrypting padding should be removed
if __name__ == "__main__":
ciphertext = open("ciphertext2.dat" , "rb").read()
partial_key = open("partial-key.dat" , "rb").read()
for i in range(256):
for j in range(256):
for k in range(256):
for l in range(256):
remaining_key = chr(i)+chr(j)+chr(k)+chr(l)
# print(type(remaining_key))
key = partial_key + remaining_key
decrypted_text = decrypt(ciphertext , key)
if decrypted_text[:5] == "Salam":
print(decrypted_text)
我无法从 partial_key 读取我收到此错误:
TypeError: can't concat str to bytes
【问题讨论】:
-
请阅读有关提供问题的最小示例。这可以在两行代码中复制! range 在它的第一个参数中不接受字符串。
标签: python python-3.x pycrypto