title: PHP to Python port of password encryption
body: <p>I want to convert the following code from PHP to Python.</p>
  
  <pre><code>public static function encryptPassword($password, $publicKeyId, $publicKey)
  {
      $key = openssl_random_pseudo_bytes(32);
      $iv = openssl_random_pseudo_bytes(12);
      $time = time();
  
      openssl_public_encrypt($key ,$encryptedAesKey, base64_decode($publicKey));
      $encrypted = openssl_encrypt($password, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $iv, $tag, strval($time));
  
      $payload = base64_encode("\x01" | pack('n', intval($publicKeyId)) . $iv . pack('s', strlen($encryptedAesKey)) . $encryptedAesKey . $tag . $encrypted);
  
      return sprintf('#PWD:1:%s:%s', $time, $payload);
  }
  </code></pre>
  
  <p>I have tried the following :</p>
  
  <pre><code>import base64
  import datetime
  import struct
  import time
  
  from Cryptodome.Random import get_random_bytes
  from Cryptodome.Cipher import AES, PKCS1_v1_5
  from Cryptodome.PublicKey import RSA
  
  def main():
      print(encpass())
  
  
  
  def encpass():
      password = ""
      publickeyid = 
      publickey = ""
  
      session_key = get_random_bytes(32)
      iv = get_random_bytes(12)
      time = str(int(datetime.datetime.now().timestamp()))
      decoded_publickey = base64.b64decode(publickey.encode())
  
      recipient_key = RSA.import_key(decoded_publickey)
      cipher_rsa = PKCS1_v1_5.new(recipient_key)
      enc_session_key = cipher_rsa.encrypt(session_key)
  
      cipher_aes = AES.new(session_key, AES.MODE_GCM, nonce=iv)
      cipher_aes.update(time.encode())
      ciphertext, tag = cipher_aes.encrypt_and_digest(password.encode("utf8"))
  
      payload = base64.b64encode((b"\x01\x00"
                                  + struct.pack('H', publickeyid))
                                 + iv
                                 + struct.pack('h', len(enc_session_key))
                                 + enc_session_key
                                 + tag
                                 + ciphertext)    
  
      return f"#PWD:1:{time}:{payload.decode()}"
  </code></pre>
  
  <p>Would like to know what is wrong in comparison to the PHP code ?</p>
  
  <p>As I am sure it is working but the output is not exactly like the php function..</p>
  
  <p>Thanks</p>
  
  <p>I have updated the code now , i think somewhere in encoding I am wrong , can you detect ?</p>