SSH keys: why is id_rsa larger than id_rsa.pub

rsasshssh-keygenssh-keys

My private key (~/.ssh/id_rsa) is a 1766-byte file, but my public key (~/.ssh/id_rsa.pub) is only 396 bytes in length. Why the massive difference? Is it because the private key is encrypted using AES? Isn't AES ciphertext usually around the same length as the plaintext?

Best Answer

Your private key has more information than your public key does. Whereas the public key only conveys the encryption exponent (e) and the modulus (n), the private key additionally includes a decryption exponent (d) and the two prime factors (p and q) of the modulus. The private key essentially has a public key inside it.

[Encryption: ciphertext = message^e (mod n); Decryption: message = ciphertext^d (mod n)]

To see all of the data in your private key file:

$ openssl rsa -in id_rsa -text -noout

Edit: The private key file apparently doesn't have the encryption exponent, but it has exponents d_1 and d_2, where d_1 = d (mod p-1) and d_2 = d (mod q-1). These are used to speed up decryption -- you can split your decryption exponentiation into smaller parallel exponentiation calls, which ends up being faster than one big m=c^d (mod n) for big d and big n.