2

I am generating RSA keys with the openssl library. I have heard that it is possible to recognise the method used to encrypt a string with AES or DES but is it possible to identify whether a string is a RSA public key?

2

Yes. RSA public keys are typically generated as ASN.1 structures as per the definition in RFC3447, encoded in DER format.

The format can be validated as correct DER / ASN.1, and then decoded to validate that the structure and contained values match what is expected from an RSA public key. The values within the structure are simply the public semiprime (n) and the exponent (e).

By validating that the value of e is prime, and that the size of n matches an expected RSA key size (e.g. 1024 bits) then you can assume that the provided data was in fact an RSA public key.

For the case of OpenSSH, public keys start with a string identifier (e.g. "ssh-rsa") which makes detection easy. You can also extract and validate the n and e values in the same way as above, albeit using the OpenSSH key format specified in RFC4253 instead of the DER / ASN.1 format.

  • Great. Any ideas of how I get from a base64 string to the values of n and e? – maxisme Sep 25 '17 at 10:00
  • 1
    Assuming you want to do so programmatically, decode the base64 string to a binary stream and parse the DER-encoded ASN.1 structure contained within. The structure is defined in section A.1.1 of RFC3447. There are libraries for DER parsing in most languages, but if you want to implement your own I recommend reading up on X.690 which is the underlying data format specification. – Polynomial Sep 25 '17 at 10:04
  • For command-line cases you can just pass the RSA public key file into the openssl tool and it'll display the contents in a human-readable format. – Polynomial Sep 25 '17 at 10:06
  • I just used a php library: which returns this from the public key string: pastebin.com/dseik8J3 I am not quite sure what is going on! But is this what I am looking for? I also found this: holtstrom.com/michael/tools/asn1decoder.php which says "RSA" which is great – maxisme Sep 25 '17 at 10:47
  • alvestrand.no/objectid/1.2.840.113549.1.1.1.html this is the answer – maxisme Sep 25 '17 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.