title: Rijndael Padding in Python
body: <p>I know where the problem is but I don't know how to fix it. The problem is with the padding. I have absolutely no idea about it and how it works. I tried searching online but nothing seemed to help. I am trying to implement this function to work with my <a href="https://hackhex.com/" rel="nofollow noreferrer">website</a>. The python encrypts and sends the data and PHP decrypts it.</p>
  <p>Here's the actual code of my python:</p>
  <pre><code>from rijndael.cipher.crypt import new
  from rijndael.cipher.blockcipher import MODE_CBC
  import base64
  
  PADDING = b'.'
  
  def r_pad(payload, block_size=32):
      return payload + (block_size - len(payload) % block_size) * PADDING
  
  KEY = 'lkirwf897+22#bbtrm8814z5qq=498j5'
  IV = '741952hheeyy66#cs!9hjv887mxx7@8y'
  plain_text = &quot;A padded string to BLOCKSIZE length.&quot;
  
  rjn = new(KEY, MODE_CBC, IV, blocksize=32)
  encd = rjn.encrypt(r_pad(plain_text))
  data = base64.b64encode(encd)
  print(data)
  
  rjn = new(KEY, MODE_CBC, IV, blocksize=32)
  data = base64.b64decode(data)
  decd = rjn.decrypt(r_pad(data))
  print (decd)
  </code></pre>
  <p>This is the output:</p>
  <blockquote>
  <p>Dv0Y/AFXdFMlDrcldFCu8v5o9zAlLNgyM+vO+PFeSrqWdzP1S1cumviFiEjNAjz5njnMMC9lfxsBl71x5y+xCw==</p>
  <p>A padded string to BLOCKSIZE length.............................Å¿:è°⌐┘n┤«╞Px╜:æC┬♣╬Q┤▼«U_♦â☻ìr</p>
  </blockquote>
  <p>I need the output of the encrypted string to be something like this:</p>
  <blockquote>
  <p>Dv0Y/AFXdFMlDrcldFCu8v5o9zAlLNgyM+vO+PFeSrpO8Ve82mdUcc4rkzp9afDYc75NmkSd4mdflt38kceOdA==</p>
  <p>A padded string to BLOCKSIZE length</p>
  </blockquote>
  <p>I tried to make RIJNDAEL256 function out of this code:</p>
  <pre><code>EncryptRJ256(&quot;lkirwf897+22#bbtrm8814z5qq=498j5&quot;, &quot;741952hheeyy66#cs!9hjv887mxx7@8y&quot;, &quot;A padded string to BLOCKSIZE length.&quot;)
  
  Public Function EncryptRJ256(ByVal prm_key As String, ByVal prm_iv As String, ByVal prm_text_to_encrypt As String) As String
      Dim s As String = prm_text_to_encrypt
      Dim managed2 As New RijndaelManaged With {
      .Padding = PaddingMode.Zeros,
      .Mode = CipherMode.CBC,
      .BlockSize = 256
  }
      Dim stream As New MemoryStream
      Dim stream2 As New CryptoStream(stream, managed2.CreateEncryptor(Encoding.ASCII.GetBytes(prm_key), Encoding.ASCII.GetBytes(prm_iv)), CryptoStreamMode.Write)
      Dim bytes As Byte() = Encoding.ASCII.GetBytes(s)
      stream2.Write(bytes, 0, bytes.Length)
      stream2.FlushFinalBlock()
      Return Convert.ToBase64String(stream.ToArray)
  End Function
  </code></pre>
  <p>Can anyone please help? I am lost at this point. :/</p>