-2

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
  • 1
    Please read about providing a minimum example of your issue. this could have been replicated in two lines of code! range does not accept a string in it's first argument. – JeffUK Nov 16 '20 at 11:25

1 Answers1

2

It looks like you are using the range function incorrectly. This function returns you a generator with the values between two integers, with an optional argument to change the step size

Examples

range(10)  # numbers between 0 and 9
range(1, 10)  # numbers between 1 and 9
range(1, 10, 2) # numbers between 1 and 9, but skipping every second number [1, 3, 5, 7, 9]
range(10, 0, -1) # numbers between 10 and 1, counting backwards

What you are trying to do is looping through a string, and for that you dont need the range function. To repeat a string, just multiply it with the number of repetitions

for i in '0123456789abcdef'*2:
   <insert code here>
oskros
  • 3,101
  • 2
  • 9
  • 28
  • now code is running but no output. – Tauqeer Abbas Nov 16 '20 at 11:42
  • well I dont know what your input file looks like, perhaps the statement `decrypted_text [:5] == 'Salam'` is never true – oskros Nov 16 '20 at 11:45
  • Salam is encrypted word in the ciphertext file.Ciphertext2 file contains a statement And this statement start with salam – Tauqeer Abbas Nov 16 '20 at 12:09
  • sorry I dont know, but it's not related to your use of the range function. You need to be more specific in what issue you have, if you want further help – oskros Nov 16 '20 at 12:12