Generate private / public SSH key with Python

Angel picture Angel · Aug 12, 2017 · Viewed 7.5k times · Source

I just need to create a private/public key pair with Python. I want to get the same result as with the following ssh-keygen command:

ssh-keygen -t rsa -b 4096 -C "sample.key" -N "" -f "key" && mv "key" "key.opk"

which generates the following:

key.opk

-----BEGIN RSA PRIVATE KEY-----
MIIJKAIBAAKCAgEAoipf1PVLTUHmW48doi5DSa3z4gKgXgtU51ofNRXwhe3EQkMt
Ia3AxVGzWQKdSjWjJdJm7mxNuO/5dbiOEX4MmhwNyLTl7+LTmhDy9M1jGAq61YAN
knkqIW+qK33+k1+Sr04PCryn16GzP3I8dBt09C4cgSLsvtcMyxHjjlGnzozLVTa5
jy1c9xRiJ6yQtHJdFosJEelEapuWLdIKsJMATaYIDQfCxplClrjpcGxdnlBGTAOB
cfJ/+S9G/UK7HIrzFMtfGVQkYZQ08Dtxj3gLGx/Ghpwt1NhBtUXW9gR4dQBBcirM
kCdGoV0tWpw2yktXBi86NKycXCinITNUKVxTl98m+bRf4dj8qgsjunkTzZ4oJ7e0
Y0fQS2hH6W7jp0aY+4DZi41Hj6moTaa926Ubbl5NUJsMfslqDRnrRHB2DuHcduN5
N83uSg9qLYwCLyPsYUgRTbU5HTWvb1R32W5FqLL4aI6bkdSl6oxMHLcALanBHC3Y
Flhycl8QRlY8ts9AfWfnvKsu85IUxlCsR7sHQbDAgVS434qKUJWhqg6t6WBL79Qi
SJgrZY9MjxpKfut5xf4LUU0dXyNFu/9tMetzstM1KtCBz9gTa/WXCMordl52xQFR
CtFLrQaMmyuCloXuPA9U4XFdKavW5q4X8pEYGTQANlDihLs40ekzZw4Gnx8CAwEA
AQKCAgEAnLRZ+s42wCdoVR3NQwV1nGNugv6KvZYxq4bZFVFoJeOHfYiwuhTPbV3v
85mlKSr/xvK06LzbtlSfOXToRFsMDngBkevNLqyAUicCwDEhCwCDXwDpHJvdbIYd
aP3N3dfDRYgm3PdFMghd0Hexl3lY4QUWImv/XsMs56cXwFWD1QMVEflaAtORPG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Zioon9+6Oy1+P+sm4cU9X4HW9Pv4nitlYjdMMm7DBP/YgVJk4F+imJ7yQKb
KC5dOg33sE/Uz7YPRIAbnFSFudgJiMendtK9qg645DOTdufAF2Jn79e1Its=
-----END RSA PRIVATE KEY-----

key.pub

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCiKl/U9UtNQeZbjx2iLkNJrfPiAqBeC1TnWh81FfCF7cRCQy0hrcDFUbNZAp1KNaMl0mbubE247/l1uI4RfgyaHA3ItOXv4tOaEPL0zWMYCrrVgA2SeSohb6orff6TX5KvTg8KvKfXobM/cjx0G3T0LhyBIuy+1wzLEeOOUafOjMtVNrmPLVz3FGInrJC0cl0WiwkR6URqm5Yt0gqwkwBNpggNB8LGmUKWuOlwbF2eUEZMA4Fx8n/5L0b9QrscivMUy18ZVCRhlDTwO3GPeAsbH8aGnC3U2EG1Rdb2BHh1AEFyKsyQJ0ahXS1anDbKS1cGLzo0rJxcKKchM1QpXFOX3yb5tF/h2PyqCyO6eRPNnignt7RjR9BLaEfpbuOnRpj7gNmLjUePqahNpr3bpRtuXk1Qmwx+yWoNGetEcHYO4dx243k3ze5KD2otjAIvI+xhSBFNtTkdNa9vVHfZbkWosvhojpuR1KXqjEwctwAtqcEcLdgWWHJyXxBGVjy2z0B9Z+e8qy7zkhTGUKxHuwdBsMCBVLjfiopQlaGqDq3pYEvv1CJImCtlj0yPGkp+63nF/gtRTR1fI0W7/20x63Oy0zUq0IHP2BNr9ZcIyit2XnbFAVEK0UutBoybK4KWhe48D1ThcV0pq9bmrhfykRgZNAA2UOKEuzjR6TNnDgafHw== sample.key

So far I have the following Python code:

from OpenSSL import crypto
k = crypto.PKey()
k.generate_key(crypto.TYPE_RSA, 4096)
print crypto.dump_privatekey(crypto.FILETYPE_PEM, k)

output

-----BEGIN PRIVATE KEY-----
MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQDEA/w1LIvlF6ja
3Qzl2c89VdMAFpgZDVLvm0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nn1vUAfKAOekpvwDeDdZDgrku1yWa6
98JEmiXxTXVH5GrAg2ysuQ+N3VLQVHM9DmTaTG18Z8oh5TS/q74w8JEY8CL8QdG4
nNULoi51nN5OzX1smBp1mKEjd9mvur9DK+n5PmwwWytBMsBDoknCI0GOwbR6NZdF
TLL4kIxCBOswa7zidEyRPLSPox5L14Krcv2ECj5pyobx9GDGJmJ8EbzIqYuVboCH
1pHbw0hUGqkQW6bTL1+aaPlMKZt2Lue8NTlo7VUCggEAbUoYBWKoSnAEmSM6CNq1
Jn4VWy58iEhW/SvUwP3987d9rtbxeGB7RcqDWXtrzJNwJrYbyrsnSPkbr/AnIsOB
tq158VAhFamfnUSb+EK4d8Ok+F0bQJiuSWR1xXWA7XC7CyFRkj7R1yJjq5wU+PFx
qcXNRpmvvkdu3NtZ9jzrQvWNMCNFmkL/7XlZDVCHqP9OatuN473TBX1Bv7Uo5CU5
+lrdr9L0IR/SD97rLNvwdtLnUhHIPddvcML27KA+sR8RCF+i0dFhHhodp15Fuzlr
oDQBhmE1VYEcmb/vRWUoRkk9LFpRePBwp9waFbpN/9u0y8kWiLM8VT1LikT1EUsm
QQKCAQEA0tw15tVa8pch+P0N6HNBd4QbKrupMRg0B37IdD3kQiPsmo0pfKeSEm8O
kx/5FzPkPbnZtjLPszPHVHqTDXeSCMTSru1ddShy47RVKXR4RjhyK9T/wLc5cq/T
iMfVm5Iu+bfTZIt4QrHX2uNHPGusjD7NVTMiOJTbHV8rvnNozrzdbSPoQjxmewAv
5uLGcjsGfvWfbROkFcIDr/sEbr4950F/lQ6HsnxymJ6K2byAN03liOKVSUCVdSyp
bj91bAOjSMa1CohVy3uDQguG1YmZEHrOrGuIsUMfaNfwH/jpW81hVy3tJZy8mGX3
0LX5Cn0Lua+2YzXi4kWYLoQL6FVQug==
-----END PRIVATE KEY-----

But I also need the public key.

Answer

Ry- picture Ry- · Aug 12, 2017

You did this to get the private key:

print crypto.dump_privatekey(crypto.FILETYPE_PEM, k)

Do this to get the public key:

print crypto.dump_publickey(crypto.FILETYPE_PEM, k)