title: Recursive Decryption of String with AES-256-cbc
body: <p>I am trying to decrypt an AES CBC string for the number of times it was decrypted.
  I was successful in decrypting the first time.</p>
  <pre><code>from Crypto.Cipher import AES
  
  k = '57067125438768260656188878670043'
  key = bytes(k, 'ascii')
  i = '5706712543876826'
  iv = bytes(i, 'ascii') 
  dec = AES.new(key, AES.MODE_CBC, iv)
  n = 2
  cipher = 'dd3364461dbca39ddb5eb32e9f11b81f000acac9ce8b91369f8bf7e4a88787785a8cc498c85ea20370e68f0e7014e92a2b5aedd4c670ec172d7adb45dfa5a770b582e8ed255bb857d94afdfd6e579525f24890070f984b8862133eda9cbb118ba7880db125c32dea7e7c54bc77abfc02'
  
  
  def unpad(s):
      return s[:-ord(s[len(s)-1:])]
  
  
  def decrypt(cipherId):
      cipher = bytes.fromhex(cipherId)
      id = dec.decrypt(cipher)
      node_dec = unpad(id.decode('utf-8'))
      print (node_dec)
      return node_dec
  
  </code></pre>
  <p>that is the first stage of encryption, but I don't know how to set a loop to run the function again based on <code>n</code>. this cipher for example was encrypted twice, it might be three or four or more.</p>
  <p>I created another function to decrypt the output of the decrypt function because the output gives a different codec.</p>
  <pre><code>def decrypt_again(cipherId):
      cipher = bytes.fromhex(cipherId)
      id =   dec.decrypt(cipher)
      node_dec = unpad(id.decode('ISO-8859-1'))
      print (node_dec)
      return node_dec
  </code></pre>
  <p>so I tried writing this loop</p>
  <pre><code>x = decrypt(cipher)
  for i in range (n):
      y = decrypt_again(x)
  </code></pre>
  <p>but it only works for n = 1, if n is more than 1, it just keeps repeating y instead of parsing it again.</p>
  <p>how can I go about it, please?</p>
  <p>also if I re-run the function twice something happens to the string, I get <code>ó³*Ä'»g)X»#¾ú84-8089-be57330fcd45</code> instead of this <code>a214868d-f40b-4184-8089-be57330fcd45</code>, there seem to be a break in the codec</p>
  <p>any reason why this is so?</p>