How does RSA public and private key structure look like?

So I was reading this wiki about the RSA algorithm and I read this article to understand it better. My goal was to create a simple program that would generate private and public keys. I got to the point where I have the e and n variables for the public key but I have no idea what should I do with them. What's the rule to follow while connecting two of this variables? Sure I can just do it my way because it's not a big deal to transform them into strings and put "&" between them, but this way this key would be readable only by my program and this is not what I try to accomplish. I can't find anything about it on the internet so I ask here for help.

• You want to read PKCS#1 for that. – SEJPM Mar 1 '18 at 18:31

Probably the most widely used standard for this is defined in PKCS#1 (available as RFC8017 as well as PDF from RSA Labs).

In apppendix A, the ASN.1 syntax for the keys is defined to be

RSAPublicKey ::= SEQUENCE {
modulus           INTEGER,  -- n
publicExponent    INTEGER   -- e
}


for public keys. Usually DER is used as the ASN.1 encoding rule. And of course, implementing ASN.1 in all its generality is not particularly easy, but implementing this subfunctionality shouldn't be too hard.

• That's a really helpful answer thanks! But I've found one difficulty in doing it. My e and n variables are big.Int (in golang) type so i can't just transform them to integer because they are too big (n is 1024 bit like in standard. How did they think that i could fit that into integer. What should I do? – A. Szokalski Mar 2 '18 at 14:24
• @A.Szokalski Note that the above is just a description of the encoding. ASN.1 INTEGERs are actually big-endian encoded with prefixed size. You may want to have a look at this ressource for the details. – SEJPM Mar 2 '18 at 14:45
• Is there any working example of this so I can see how it works? – A. Szokalski Mar 2 '18 at 14:51
• I looked at this and it shows that integer takes values like 256 so it's far behind from my 1024bit – A. Szokalski Mar 2 '18 at 14:53
• Oh I think I get it 256 isn't this INTEGER but it's value in the end. The integer is this: 02 02 01 00. Now it makes sense. But still not sure if it will handle 1024 bit – A. Szokalski Mar 2 '18 at 14:56

Although PKCS1's ASN.1 is a quite common representation of RSA keys, it is not the only one used (and certainly not the only one possible).

The standards for XML digital signature and encryption unsurprisingly use XML not ASN.1 for data structure: https://www.w3.org/TR/xmldsig-core1/#sec-RSAKeyValue

The standards for JSON Web Signature and Encryption (and Tokens) unsurprisingly use JSON for data structure: https://tools.ietf.org/html/rfc7517#appendix-A.1

SSH has its own format, based on the data structure used elsewhere in SSH: https://tools.ietf.org/html/rfc4253#section-6.6 . OpenSSH, the most widely used implementation of SSH, also uses this wire format encoded in base64 in files, notably known_hosts and authorized_keys. There is also a 'portable' (but actually rarely used) file format in https://tools.ietf.org/html/rfc4716 .

PGP also has its own format based on PGP data structure: https://tools.ietf.org/html/rfc4880#section-5.5.2

There are lots of Qs (and almost as many As :-) on stackoverflow and sometimes other stacks about having a wrong one of these formats and needing to convert to another.