Use RSA private key to generate public key?
openssl genrsa -out mykey.pem 1024
will actually produce a public - private key pair. The pair is stored in the generated mykey.pem
file.
openssl rsa -in mykey.pem -pubout > mykey.pub
will extract the public key and print that out. Here is a link to a page that describes this better.
EDIT: Check the examples section here. To just output the public part of a private key:
openssl rsa -in key.pem -pubout -out pubkey.pem
To get a usable public key for SSH purposes, use ssh-keygen:
ssh-keygen -y -f key.pem > key.pub
Generate public ssh key from private key
The option -y
outputs the public key. From the linux manual for the ssh-keygen
command:
-y
---- This option will read a private OpenSSH format file and print
an
OpenSSH public key to stdout.
ssh-keygen -y -f ~/.ssh/id_rsa > ~/.ssh/id_rsa.pub
As a side note, the comment of the public key is lost. I've had a site which required the comment (Launchpad?), so you need to edit ~/.ssh/id_rsa.pub and append a comment to the first line with a space between the comment and key data. An example public key is shown truncated below.
ssh-rsa AAAA..../VqDjtS5 ubuntu@ubuntu
If you want a scripted way to add a comment and also add the pubkey to your authorized_keys
file, you can do...
ssh-keygen -y -f ~/.ssh/id_rsa | \
sed 's/$/ comment-goes-here/' | \
tee ~/.ssh/id_rsa.pub | \
tee -a authorized_keys
Can I generate private key from the last public key using puttygen?
You cannot generate private key from public key but you can generate public key from the private key using puttygen. As @alfasin mentioned if you could generate the private key from public key then RSA would be useless and this would make you vulnerable to attack.FYI the public key is used for encryption and private key is used for decryption.
this wikipedia page will help you better understand the reason.
C# create ssh-rsa public key from existing private key string
SSH key format is a bit complex. Moreover, .NET doesn't have a method to directly get the key in this format. However, something like this will work (I tried in a .NET 6 console application, and got the public key exactly how you want it.):
// See https://aka.ms/new-console-template for more information
using System.Security.Cryptography;
using System.Text;
static byte[] ToBytes(int i)
{
byte[] bytes = BitConverter.GetBytes(i);
if (BitConverter.IsLittleEndian)
{
Array.Reverse(bytes);
}
return bytes;
}
string privateKey = "-----BEGIN RSA PRIVATE KEY----- MIIEogIBAAKCAQEAvyN0aQKoYl/LAZ/1dQt0rWuSNyOty88k3439HT3rcT/vhaSk d5lbnNKiYTzdDE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5GIh02h6Z OJGTPW6V3bn2RrzrRQSyu+2pgBohlnUw2uGw1b1UUwX/QZFbs7zvcGELwy8P6OE1 eIAPKUBKb6W55jnz/VwfUQKBgGPTpQyPkAj1vNO2iLWrag/dtApOXJ0yljd5/8cA TP3dsWShbk3h+yoFTbznt7xpuf//NTN5c8d+LkSdZvrAk18LhIyidX8xl4pOeTui G/JpzXFmXrDKrHm7V6ZsYLrwwNwVBLFDe/KLojNDlPKhRbRuSONYTU4cZQeXfA/1 9/6/AoGAarF4JSdpzMzfacpLy2nsOM6XmL76B218uKANSHQy9k1X/Hp1u1StY8tQ H4+DSrRUQBb4sdxkCRXVvMH3zttDGoIrSUvDqN3k4opcP8nmzMc/EDwD3xFgri/p yBXBhE99r1B0p7fneXt58tTqtcevk5dQPzyF9SdsfUxD5PrnZRI= -----END RSA PRIVATE KEY-----";
var rsa = RSA.Create();
rsa.ImportFromPem(privateKey.ToCharArray());
byte[] sshrsaBytes = Encoding.Default.GetBytes("ssh-rsa");
byte[] n = rsa.ExportParameters(false).Modulus;
byte[] e = rsa.ExportParameters(false).Exponent;
string buffer64;
using (var ms = new MemoryStream())
{
ms.Write(ToBytes(sshrsaBytes.Length), 0, 4);
ms.Write(sshrsaBytes, 0, sshrsaBytes.Length);
ms.Write(ToBytes(e.Length), 0, 4);
ms.Write(e, 0, e.Length);
ms.Write(ToBytes(n.Length + 1), 0, 4);
ms.Write(new byte[] { 0 }, 0, 1);
ms.Write(n, 0, n.Length);
ms.Flush();
buffer64 = Convert.ToBase64String(ms.ToArray());
}
string comment = "administrator@LovelyTrust";
string publicKey = $"ssh-rsa {buffer64} {comment}";
Console.WriteLine(publicKey);
ToBytes
method is simply checks your machine's endianness and converts bytes into correct order if necessary.
We first create an RSA
object and import the private key, just like you did. After that, the complex part I mentioned starts, and continues until the end of the using
block. Finally, we concatenate that value with ssh-rsa word from the left and the comment (this is the value entered in Key comment text field if you use PuTTY Key Generator) from the right.
This code is heavily inspired from the code of SshKeyGenerator library. Unfortunately, the library itself does not support key import. On the other hand, if you want randomly generated SSH private and public key pairs, using directly the library will be probably easier and more convenient.
Generate private / public SSH key with Python
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)
Generate Private key with OpenSSL and Public key ssh-keygen for SSH
After doing some research and experiments I landed on the simplest solution.
Generate secure private key using openssl with a password length of 32 or more characters, then use ssh-keygen command to get my required output.
ssh-keygen -y -f private.pem > public_key.pub
It works accurately!
How do I get the public key of a pem file?
Copy the public key to clipboard.
Linux
ssh-keygen -f private.pem -y | xclip
MacOS
ssh-keygen -f private.pem -y | pbcopy
Save to file
ssh-keygen -f private.pem -y > public.pub
Note that if your permissions are vague on the .pem
file, then ssh-keygen
will generate an empty .pub
file.
You can usually fix this with:
chmod 400 private.pem
Related Topics
Collecting the Data for a Partiulcar Process from Pmu for Every 1 Milli Second
Permission Denied in a Folder for a User After Chown and Chmod
Redirect Two or More Stdout to a Single Stdin
Direct Control of Hci Device (Bypass Bluetooth Drivers) on Linux
How to Redirect the Telnet Console Logs to a File Linux
Which Is the Best Way to Make Config Changes in Conf Files in Ansible
Linux, Gnu Gcc, Ld, Version Scripts and the Elf Binary Format -- How Does It Work
Bash: No Such File or Directory
How to Acess the Physical Address from Linux Kernel Space
Pkill Returns 255 in Combination with Another Command via Remote Ssh
How to Use a Seq_File in Linux Kernel Modules
Docker: Ssh Access Directly into Container
Difference Between Linux Kernel and Unix Kernel(Such as Freebsd) from Programmer's Point of View
Find Command to Find Files and Concatenate Them