I have a module and an exponent of the RSA public key embedded in a binary, and I am trying to extract the entire blob and create a useful .pem public key.
I am currently extracting the full 260 bytes (4 bytes for exponent, 256 bytes for module) and encoding as base64. I do this using the following shell command:
tail -c $((filesize - start_of_key_data)) filename | head -c $size_of_key_data | base64 > outkey
This gives me the following line:
<<<<<< modulus & exponent extracted from binary file, base64-encoded >>>>>> tZyrQA6cZFJfVm6FyXwtZaLQYg8EecuO+ObrHTwc8JO+XrgnpNAdmlhbAEPxSNnjwhNnbYGYGL4F vzmnZXzZU71Key42HQPh1k2Zx1UDbrH5ciODKx1ZbuEx8K24SHnL1nY/H75hwhT/ZRRVGQDvYDT+ sgzw2vmV66+dflw1Zs8BLhqjLjczdHvjeVXsDRJ9Mvvd/dhFH8UlTf4JpLGya9nsNIfNBBIf1Lll RWwCTiEIbaOMgWcLjLV/2tk/j5Dra/oQnVf/2hVsEF/hXEx41YjeEW/warweoDVG7zaxrHEc/k/r ZCUCZKxf8nBKdqax/gRICvkG6e5xg2GQw0W/ZwABAAE=
Now, when I take the key keypair key.pem, the originally extracted module and exponent, and display the public part like this
openssl rsa -in key.pem -pubout -out pubkey.pem
I get this line (I skipped the header and footer lines:
<<<<<<<<< valid public key data extracted from keypair >>>>>>>>> MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtZyrQA6cZFJfVm6FyXwt ZaLQYg8EecuO+ObrHTwc8JO+XrgnpNAdmlhbAEPxSNnjwhNnbYGYGL4FvzmnZXzZ U71Key42HQPh1k2Zx1UDbrH5ciODKx1ZbuEx8K24SHnL1nY/H75hwhT/ZRRVGQDv YDT+sgzw2vmV66+dflw1Zs8BLhqjLjczdHvjeVXsDRJ9Mvvd/dhFH8UlTf4JpLGy a9nsNIfNBBIf1LllRWwCTiEIbaOMgWcLjLV/2tk/j5Dra/oQnVf/2hVsEF/hXEx4 1YjeEW/warweoDVG7zaxrHEc/k/rZCUCZKxf8nBKdqax/gRICvkG6e5xg2GQw0W/ ZwIDAQAB
You can see that the key data I extracted and base64-encoded are actually present in the data of the valid public key data extracted from key.pem using openssl. However, at the beginning there are 45 characters that my own extracted data does not have -
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA
and the last 8 characters are also different.
ZwIDAQAB
Can someone offer some tips on how to convert a module and metric to a usable public key?
(The goal is to do this in a bash script, and not in python or C, as I have seen, many suggest.)