I am trying to finding the remaining part of the key by brute-forcing the remaining 4 characters. Then we decrypt using each key and check if the decrypted message has "Salam" at the starting.
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)
I can't read from partial_key i get this error:
TypeError: can't concat str to bytes