6

I am having trouble using the rsa module in python, I need to load two public and private keys from a file, this is how I open the private key file for example:

private_key_file = "privKey.pem"
keydata = open(private_key_file).read()
privKey = rsa.PrivateKey.load_pkcs1(keydata, 'PEM')

and I get this error message: ValueError: No PEM start marker "b'-----BEGIN RSA PRIVATE KEY-----'" found

although the file content start with "-----BEGIN RSA PRIVATE KEY-----" one note is that the file does not contain the leading characters b' I don't know where it comes from or whether it's relevant in this particular problem!.

any help would be appreciated

Ice Queen
  • 61
  • 2
  • 1
    Maybe this helps: http://stackoverflow.com/questions/16482800/how-to-load-in-python-rsa-a-public-rsa-key-from-a-file-generated-with-openssl – ρss Apr 17 '14 at 07:57
  • Leading 'b' in string could be related to that [question](http://stackoverflow.com/questions/6269765/what-does-the-b-character-do-in-front-of-a-string-literal). Check if your python version and the version required/supported/.. by the rsa library match. – Oleg Estekhin Apr 17 '14 at 08:05
  • 1
    Could you get a programmers editor and save the file as ASCII? Maybe the encoding is not valid and contains a byte order mark. Try and use Windows line endings as well (\r\n). – Maarten Bodewes Apr 17 '14 at 12:19
  • 1
    Thank you @pss, I had to open the file as binary using the flag "rb", and it worked with me. thank all who commented and I really appreciate your help – Ice Queen Apr 17 '14 at 23:37
  • 2
    @IceQueen Welcome, if you have found a solution then please post it and accept it as an answer. This helps other users too. – ρss Apr 18 '14 at 11:56
  • Do you have a password on the private key? If so, you might try removing it with: `openssl rsa -in key-with-password.pem -out key-without-password.pem`. – jww Apr 20 '14 at 10:46
  • pss - does Ice Queen have enough points to answer her own question? (It would have helped, because I spent some time trying to find her an example or duplicate). – jww Apr 20 '14 at 10:48
  • I'll close it as a dupe, but I guess it could be retained instead of deleted so that the comments and the title can be preserved. – Maarten Bodewes Mar 29 '18 at 11:59

0 Answers0