title: Python RSA - Public Key Encryption Gives Error?
body: <p>I am trying to encrypt the JSON data with RSA public key and decrypt using private key,
  My code,</p>
  <pre><code>from Crypto.Cipher import PKCS1_OAEP
  from Crypto.PublicKey import RSA
  import binascii
  
  final_data = { 'custom_folder': {'check': 'test'} }
  
  data = json.dumps(final_data).encode('utf-8') # converted into bytes
  
  keys = RSA.generate(2048)
  pub_key = keys.publickey().exportKey('PEM') # generates public key
  pri_key = keys.exportKey('PEM') # generate private key
  
  encryptor = PKCS1_OAEP.new(key=keys) # using keys variable
  encrypted = encryptor.encrypt(data)
  print(&quot;Encrypted:&quot;, binascii.hexlify(encrypted))
  </code></pre>
  <p>When done using variable <code>keys</code> it encrypts the data like this,</p>
  <pre><code>Encrypted: b'46bdca64f486....'
  </code></pre>
  <p>But i want to use my public key to encrypt and send the data and at he other end decrypt using just the private key, when i replace the <code>keys</code> with the <code>pub_key</code> generates this error</p>
  <pre><code>encryptor = PKCS1_OAEP.new(key=pub_key) # replaced keys with public key
  
  # Error geenrated
  Traceback (most recent call last):
    encrypted = encryptor.encrypt(data)
    modBits = Crypto.Util.number.size(self._key.n)
  AttributeError: 'bytes' object has no attribute 'n'
  </code></pre>
  <p>How do i encrypt using a public key only?</p>