title: How to use AES Encryption and Decryption from Scala to Python
body: <p>I have a code in scala where I have my encryption and decryption code, It works fine and the code is:</p>
  <pre><code>import java.util.Base64
  import javax.crypto.Cipher
  import javax.crypto.spec.{IvParameterSpec, SecretKeySpec}
  class Encryption {
  val key = &quot;enIntVecTest2020&quot;
  val initVector = &quot;encryptionIntVec&quot;
  
  def encrypt(text: String): String = {
  
  val  iv = new IvParameterSpec(initVector.getBytes(&quot;UTF-8&quot;))
  val  skeySpec = new SecretKeySpec(key.getBytes(&quot;UTF-8&quot;), &quot;AES&quot;)
  
  val  cipher = Cipher.getInstance(&quot;AES/CBC/PKCS5PADDING&quot;)
  cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv)
  
  val  encrypted = cipher.doFinal(text.getBytes())
  return Base64.getEncoder().encodeToString(encrypted)
  }
  
  def decrypt(text:String) :String={
  val  iv = new IvParameterSpec(initVector.getBytes(&quot;UTF-8&quot;))
  val  skeySpec = new SecretKeySpec(key.getBytes(&quot;UTF-8&quot;), &quot;AES&quot;)
  
  val  cipher = Cipher.getInstance(&quot;AES/CBC/PKCS5PADDING&quot;)
  cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv)
  val  original = cipher.doFinal(Base64.getDecoder.decode(text))
  
  new String(original)
  }
  }
  
  val encryptobj = new Encryption()
  val pwd = &quot;#test@12345&quot;
  val result =encryptobj.encrypt(pwd)
  </code></pre>
  <p>pwd: String = #test@12345<br />
  result: String = lHhq1OzMSYnj+0XxiNzKhQ==</p>
  <pre><code>val pwd1  = encryptobj.decrypt(result)
  println(pwd1)
  </code></pre>
  <p>pwd1: String = #test@12345<br />
  #test@12345</p>
  <p>I tried in Python to achieve the same but does not give the expected encryption result, here is my code(I took the help of other similar answers):</p>
  <pre><code>from hashlib import sha256
  import base64
  from Crypto import Random
  from Crypto.Cipher import AES
  
  BS = 16
  pad = lambda s: bytes(s + (BS - len(s) % BS) * chr(BS - len(s) % BS), 'utf-8')
  unpad = lambda s : s[0:-ord(s[-1:])]
  
  class AESCipher:
  
      def __init__( self, key ):
          self.key = bytes(key, 'utf-8')
  
      def encrypt( self, raw ):
          raw = pad(raw)
          iv = &quot;encryptionIntVec&quot;.encode('utf-8')
          cipher = AES.new(self.key, AES.MODE_CBC, iv )
          return base64.b64encode( iv + cipher.encrypt( raw ) )
  
      def decrypt( self, enc ):
          enc = base64.b64decode(enc)
          iv = enc[:16]
          cipher = AES.new(self.key, AES.MODE_CBC, iv )
          return unpad(cipher.decrypt( enc[16:] )).decode('utf8')
  
      cipher = AESCipher('enIntVecTest2020')
      encrypted = cipher.encrypt('#test@12345')
      decrypted = cipher.decrypt(encrypted)
      
      print(encrypted)
  </code></pre>
  <p>b'ZW5jcnlwdGlvbkludFZlY5R4atTszEmJ4/tF8YjcyoU='</p>
  <p>As you can see both the encryption is not right, I don't know where I am doing wrong. Please help in achieving the same encrypting result in python as showing in scala, I would be much thankful.</p>