title: Change Rijndael decryption logic from C# to Python
body: <p>I have some already encrypted data which needs to be decrypted using python. The decryption logic in C# looks as given below.</p>
  <pre class="lang-cs prettyprint-override"><code>using System.Security.Cryptography;
  private const string Url_ENCRYPTION_KEY = &quot;abcd123456&quot;;
  private readonly static byte[] URL_SALT = Encoding.ASCII.GetBytes(Url_ENCRYPTION_KEY.Length.ToString());
  public static string Decrypt(string inputText) {
      
      try {
          if (!string.IsNullOrEmpty(inputText)) {
              RijndaelManaged rijndaelCipher = new RijndaelManaged();
              byte[] encryptedData = Convert.FromBase64String(inputText.Replace(&quot; &quot;, &quot;+&quot;));
              PasswordDeriveBytes secretKey = new PasswordDeriveBytes(Url_ENCRYPTION_KEY, URL_SALT);
  
              using (ICryptoTransform decryptor =
                  rijndaelCipher.CreateDecryptor(secretKey.GetBytes(32), secretKey.GetBytes(16))) {
                  using (MemoryStream memoryStream = new MemoryStream(encryptedData)) {
                      using (CryptoStream cryptoStream =
                          new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) {
                          byte[] plainText = new byte[encryptedData.Length];
                          int decryptedCount = cryptoStream.Read(plainText, 0, plainText.Length);
                          return Encoding.Unicode.GetString(plainText, 0, decryptedCount);
                      }
                  }
              }
          }
      }
      catch(Exception ex) {
          //clsErrorHandler.LogError(ex);
      }
      return inputText;
  }
  </code></pre>
  <p>I have tried libs like pprp and python's cryptography but the solutions out there use PBKDF2, while the C# code here supplies the decryptor bytes of key and salt as key and IV values.
  From what I looked the PasswordDeriveBytes function basically work as a somewhat modified PBKDF1,
  but all of the solutions I tried fail with somekind of esoteric this size doesn't match with that size errors.
  Here is one implementation of PasswordDeriveBytes I found floating out there but I am at a loss on how to do something similar to <code>secretKey.GetBytes(32)</code> and creating a decryptor</p>
  <pre class="lang-py prettyprint-override"><code>import hashlib
  from base64 import b64decode
  
  def MS_PasswordDeriveBytes(pstring, salt, hashfunc, iterations, keylen):
      if iterations &gt; 0:
          lasthash = hashlib.sha1(pstring+salt).digest()
          iterations -= 1
      else:
          print(&quot;Iterations must be &gt; 0&quot;)
      #If iterations is 1 then basically the same thing happens as 2 based on my testing
      #if iterations == 0 and keylen &gt; len(lasthash):
          #print(&quot;Dunno what happens here&quot;)
          #return -1
      for i in range(iterations-1):
          lasthash = hashlib.sha1(lasthash)
      bytes = hashlib.sha1(lasthash).digest()
      ctrl = 1
      while len(bytes) &lt; keylen:
          bytes += hashlib.sha1(str(ctrl)+lasthash).digest()
          ctrl += 1
      return(bytes[:keylen])
  
  
  stpass = 'amp4Z0wpKzJ5Cg0GDT5sJD0sMw0IDAsaGQ1Afik6NwXr6rrSEQE='
  slt = 'aGQ1Afik6NampDT5sJEQE4Z0wpsMw0IDAD06rrSswXrKzJ5Cg0G='
  initv = '@1B2c3D4e5F6g7H8'
  enc_str = b64decode('B5YDTLEDBjd+8zy5lzEfjw==')
  derbytes = MS_PasswordDeriveBytes(stpass, slt, hashlib.sha1, iterations=2, keylen=32)
  </code></pre>