I have a file with the public exponent and modulus in it. They're not in pem or xml or der format, they're just the values written in at their offsets.
How can I make a public key out of them with M2Crypto? I also have the private key in the same format. I've managed to use code that someone posted here on Stackoverflow to generate a PEM file with php, but this seems like an extremely ridiculous way to do it.
This isn't a one-time thing either, I need to be able to read the public exponent and modulus from files in this format to check the signature.
Thank you very much to Lars here: http://blog.oddbit.com/2011/05/09/signing-data-with-ssh-agent/
e
is a Pythonlong
of the public exponent.n
is a Pythonlong
of the public Modulus.The code he posted was:
hex will generate a hex string of the sort
0xA45E
, so he's just grabbing everything after the0x
.I'm reading the key from a file, so I don't have it as a long. I ended up using:
Worked like a charm!
The accepted format of
new_pub_key
, as per the documentation, needs to beI'm not sure if this is a typo, but for my exponent of (in hex)
00010001
ended up being000003010001
. I think it's a byte count, not bit count. They also stripped the first 0x00. I don't know if that's standard or if because it was an empty byte.edit: I think I have a bit of a better understanding of the format.
If the first byte is negative, a zero byte is added to the beginning. If there are any leading (at the beginning) zero bytes, they are stripped unless the first byte would become negative, in which case, only one zero byte is left.
Some examples: