1

I have a public key that looks like this:

BgIAAACkAABSU0ExAAQAAAEAAQCH3cm40A7P6GzlRrDMC1hEgB9kwPYLCei0z/NxnEwzj/brpcmhMXoebyW7GLoAgNaVigi5/+UMbuXwva9e6WpAZu+45a+wAuBJGetnlhfhgGWs8WpaE6qYpB94m3UUWdAB5rwSvC4gxHzHcGCk5M3ilNxA5Hk3jcXcvwzum+fHqg==

How do I encrypt a string with that key using Python?

I have been trying varyations on this:

from Crypto.PublicKey import RSA
rsakey = RSA.importKey(key)

But I keep getting the error ValueError: RSA key format is not supported

Brant
  • 5,721
  • 4
  • 36
  • 39
  • 1
    Where did you get this key and are you sure it's an RSA key? – univerio Jul 29 '14 at 21:09
  • possible duplicate of [How to print a public key as string and encrypt with it?](http://stackoverflow.com/questions/11777428/how-to-print-a-public-key-as-string-and-encrypt-with-it) – Robᵩ Jul 29 '14 at 21:20
  • possible duplicate of [How to read a RSA public key in PEM + PKCS#1 format](http://stackoverflow.com/questions/10569189/how-to-read-a-rsa-public-key-in-pem-pkcs1-format) – Roland Smith Jul 29 '14 at 23:51
  • Looks like some kind of Windows RSA key blob. – President James K. Polk Jul 30 '14 at 01:42
  • As far as I know, it's an RSA key. I tried http://stackoverflow.com/questions/10569189/how-to-read-a-rsa-public-key-in-pem-pkcs1-format before posting this question. It also did not work for me. It returned "ValueError: Not a DER SEQUENCE.". – Brant Jul 30 '14 at 12:13
  • It seems to be a RSA public key generated with `ssh-keygen` – jyz Jul 10 '15 at 12:37

1 Answers1

0

You need to use the following as the input instead:

-----BEGIN PUBLIC KEY-----
BgIAAACkAABSU0ExAAQAAAEAAQCH3cm40A7P6GzlRrDMC1hEgB9kwPYLCei0z/NxnEwzj/brpcmhMXoebyW7GLoAgNaVigi5/+UMbuXwva9e6WpAZu+45a+wAuBJGetnlhfhgGWs8WpaE6qYpB94m3UUWdAB5rwSvC4gxHzHcGCk5M3ilNxA5Hk3jcXcvwzum+fHqg==
-----END PUBLIC KEY-----
Kyle Piira
  • 596
  • 6
  • 8