code: def _advapi32_encrypt(cipher, key, data, iv, padding):
      
  
      context_handle = None
      key_handle = None
  
      try:
          context_handle, key_handle = _advapi32_create_handles(cipher, key, iv)
  
          out_len = new(advapi32, 'DWORD *', len(data))
          res = advapi32.CryptEncrypt(
              key_handle,
              null(),
              True,
              0,
              null(),
              out_len,
              0
          )
          handle_error(res)
  
          buffer_len = deref(out_len)
          buffer = buffer_from_bytes(buffer_len)
          write_to_buffer(buffer, data)
  
          pointer_set(out_len, len(data))
          res = advapi32.CryptEncrypt(
              key_handle,
              null(),
              True,
              0,
              buffer,
              out_len,
              buffer_len
          )
          handle_error(res)
  
          output = bytes_from_buffer(buffer, deref(out_len))
  
          # Remove padding when not required. CryptoAPI doesn't support this, so
          # we just manually remove it.
          if cipher == 'aes' and not padding:
              if output[-16:] != (b'\x10' * 16):
                  raise ValueError('Invalid padding generated by OS crypto library')
              output = output[:-16]
  
          return output
  
      finally:
          if key_handle:
              advapi32.CryptDestroyKey(key_handle)
          if context_handle:
              close_context_handle(context_handle)
docs: Encrypts plaintext via CryptoAPI
  
      :param cipher:
          A unicode string of "aes", "des", "tripledes_2key", "tripledes_3key",
          "rc2", "rc4"
  
      :param key:
          The encryption key - a byte string 5-16 bytes long
  
      :param data:
          The plaintext - a byte string
  
      :param iv:
          The initialization vector - a byte string - unused for RC4
  
      :param padding:
          Boolean, if padding should be used - unused for RC4
  
      :raises:
          ValueError - when any of the parameters contain an invalid value
          TypeError - when any of the parameters are of the wrong type
          OSError - when an error is returned by the OS crypto library
  
      :return:
          A byte string of the ciphertext