title: how to encrypt and decrypt in python & vb.net compatible with AES?
body: <p>Question for anyone who believes in Python and VB 2008:</p>
  
  <p>I'm building a system to raspberry pi board that needs to be operated by a remote computer with Windows operating system, but can not make the code in python aes to be compatible with vb.net code.</p>
  
  <p>I would like to know how do I generate a code for vb which has result compatible with the code from Python with AES.</p>
  
  <p>How to encrypt and decrypt in python &amp; vb.net compatible with the AES code below?</p>
  
  <p>python code with AES:</p>
  
  <pre><code>from Crypto.Cipher import AES
  import base64
  
  
  def EncodeAES(secret, s):
      BLOCK_SIZE = 32
      PADDING = '{'
      pad = BLOCK_SIZE - len(s)
      pad = pad % BLOCK_SIZE
      pad = pad * PADDING
      pad = s + pad
      cipher = AES.new(secret)
      c = cipher.encrypt(pad)
      enc = base64.b64encode(c)
      return enc
  
  
  def DecodeAES(secret, e):
      PADDING = '{'
      cipher = AES.new(secret)
      b64 = base64.b64decode(e)
      c = cipher.decrypt(b64)
      dec = c.rstrip(PADDING)
      return dec
  
  secret2 = 'x\x90\xd4~\x18\x8a\xb6\x9f\xc7\x82/DI\x1d\xa7\xb9\xdf\xad\x80S\x95\x90\x13}T,\x89/\xcf\x1a\xe9\xc2'
  
  # create a cipher object using the random secret
  # generate a random secret key
  #BLOCK_SIZE1 = 32
  #secret2 = os.urandom(BLOCK_SIZE1)
  print (('secret string:', secret2))
  
  # encode a string
  encoded = EncodeAES(secret2, 'password')
  print (('Encrypted string:', encoded))
  
  # decode the encoded string
  decoded = DecodeAES(secret2, encoded)
  print (('Decrypted string:', decoded))
  </code></pre>
  
  <p>I found a DES code (vb.net &amp; python compatible, only for sample):</p>
  
  <p>vb.net DES:</p>
  
  <pre><code>Imports Microsoft.VisualBasic.CompilerServices
  Imports System
  Imports System.Diagnostics
  Imports System.Security.Cryptography
  Imports System.Text
  
  Public Class Form1
  
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
          Dim temp As String = ""
          Dim from_pyton As String = "rhxUtKPuxriRaPd/MDgliw=="
          temp = Crypto.Encrypt("I love security")
          MsgBox(temp)
  
          temp = Crypto.Decrypt(from_pyton)
          MsgBox(temp)
  
          End
      End Sub
  End Class
  
  Public Class Crypto
      Private Shared DES As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
  
      Private Shared MD5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider()
  
      Public Shared Function MD5Hash(ByVal value As String) As Byte()
          Return Crypto.MD5.ComputeHash(Encoding.ASCII.GetBytes(value))
      End Function
  
      Public Shared Function Encrypt(ByVal stringToEncrypt As String) As String
          Crypto.DES.Key = Crypto.MD5Hash("L6#F&amp;,q2$xLx")
          Crypto.DES.Mode = CipherMode.ECB
          Dim bytes As Byte() = Encoding.ASCII.GetBytes(stringToEncrypt)
          Return Convert.ToBase64String(Crypto.DES.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length))
      End Function
  
      Public Shared Function Decrypt(ByVal encryptedString As String) As String
          Dim result As String
          Try
              Crypto.DES.Key = Crypto.MD5Hash("L6#F&amp;,q2$xLx")
              Crypto.DES.Mode = CipherMode.ECB
              Dim array As Byte() = Convert.FromBase64String(encryptedString)
              result = Encoding.ASCII.GetString(Crypto.DES.CreateDecryptor().TransformFinalBlock(array, 0, array.Length))
              Return result
          Catch expr_4D As Exception
              ProjectData.SetProjectError(expr_4D)
              ProjectData.ClearProjectError()
          End Try
          result = Nothing
          Return result
      End Function
  End Class
  </code></pre>
  
  <p>python DES compatible: (need install pyDes lib: <a href="http://twhiteman.netfirms.com/des.html" rel="nofollow">http://twhiteman.netfirms.com/des.html</a>)</p>
  
  <pre><code>from pyDes import *
  import hashlib
  import base64
  key = hashlib.md5("L6#F&amp;,q2$xLx").digest()
  data = "I love security"
  k = triple_des(key, ECB, "\0\0\0\0\0\0\0\0", pad=None, padmode=PAD_PKCS5)
  d = k.encrypt(data)
  base64Encrypted = base64.b64encode(d)
  print ("Encrypted: %r" % base64Encrypted)
  base64Decrypted = base64.b64decode(base64Encrypted)
  print ("Decrypted: %r" % k.decrypt(base64Decrypted))
  </code></pre>