RSA Public Key Parsing. I Need Help

Parsing of the public key raises an error. I have not been able to know
why. I have looked at the jwt docs, the public key is valid. someone please help. it seems am missing something.

My code is in a github gist. Here is the link github gist

your code didn’t give me any error :confused: check your system.

Hey @sadick, like @geosoft1, I am also receiving no errors.

Could you please post the error you are actually receiving? It might help us understand what the issue is a little bit better. For example, even running your application from the wrong directory can cause an error.

Lastly, are you running a recent version of Go and the jwt library?

Am running Go Build version 1.6.2. and jwt-go version 3.0.0
Here is the screenshot of the error am getting

I have checked and everything seems to be okey.
May be i will try to run it on another machine to see if it still gives me the same error.

i have a small suspicion :confused:…because the error came from encoding/pem (see Decode function) i guess some newline characters from rsa files can be misinterpreted. you work on Windows, right ? on Linux your files work well.

Since I am not on windows I can’t know for certain if this is the issue, but I’m thinking that since it looks like you are on windows, it might be the format you have saved your keys as. Can you try calling the public key as app.pem and the private as app.key and see if there is any difference.

Edit: After re-reading the code I realize you are using ioutil.ReadFile so I don’t think my suggestion will help at all probably, but I think George’s response might be the issue.

I have looked at the encoding/pem function an according to it i see everything has been handled well from removal of white space to newline characters and the carriage return.
This is what my public key bytes look like when printed in the console

And here is what is looks like in a text editor

So I can now see clearly that there is a problem with the spaces in between.

This is how I solved the issue

  1. I copy pasted the public key to a new text file in notepad and run the program again using this new file. It worked.

NB: I was generating the public key with the below openssl command
openssl rsa -in app.rsa -pubout >
I changed the extension from to .txt and it worked again

And now this is how the public key looks in the command prompt


Actually generating the public key with a .txt extension helped.
see the reply to @geosoft1
I appreciate your help I had been stuck.

Glad to hear you got it working.

My initial thought was that since I know that sometimes when you change an extension, you can be asked if you would like to change the actual file type to that extension type, so I thought maybe by changing it, it could make sure the encoding was correct, which in this case, probably corrected the new line characters.

Ah… I just realized that .pub is actually a Microsoft Publisher Document File type’s extension. This is probably what was causing your issue.

A major disadvantage of the file type is its lack of portability.

1 Like

I hadn’t checked that @radovskyb. And i guess when openssl tells the windows os to write the .pub file the OS itself already has opinions on how that file should be written to and formated. I can now see clearly why the extension is important.

Thanks for the link

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.