Which format should be SSH private key for LibGit2 LibGit2Sharp (+SSH)

早过忘川 提交于 2019-12-19 07:58:41

问题


I´m kind of stuck with an SSH private key issue and LibGit2Sharp-Ssh.

I have a .Net/C# application that uses LibGit2Sharp-Ssh to clone a Git repository.

I need to use SSH (https with user/password is not an option) and I also have a valid key, which is already working e.g. with Teamcity.

My code looks like this:

CloneOptions options = new CloneOptions
{
     Checkout = false,
     CredentialsProvider = (url, user, cred) => new SshUserKeyCredentials()
     {
          PrivateKey = privateKey,
          Passphrase = passphrase,
          PublicKey = publicKey,
          Username = "git"
     }
};
var clone = LibGit2Sharp.Repository.Clone(remoteUrl, localPath, options);

privateKey points to a private key file in "OpenSSH" format.

When Clone is executed i get:

LibGit2Sharp.LibGit2SharpException: "Failed to authenticate SSH session: Invalid key data, not base64 encoded"

I've tried all of the private key formats I could create with PuttyGen, but I always get the same result.

What might be the issue or in what format do I need to create the private key file?

My OpenSSH-format key looks like (truncated):

-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,27A4E70608469318

<Key-Data, like "sdhsdcQEHBg3uzfb...">
-----END RSA PRIVATE KEY-----

回答1:


privateKey point to a private key file in "OpenSSH" format.

privateKey needs to point to a key, not a path to a key. You need to read the key file and place the contents into a string that you can pass to privateKey.




回答2:


Instead of creating your private key with PuttyGen (ppk keys), Use PuttyGen to load said ppk file, and save it as OpenSSH file (id_rsa, id_rsa.pub)

Or, as I described here, use Git for Windows PATH to access ssh-keygen, and create one directly with the right format:

ssh-keygen -t rsa -C "key for my Git repo server" -q -P ""



回答3:


That´s how my OpenSSH-Key looks like (key data truncated):

-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,27A4E70608469318

<Key-Data, like "sdhsdcQEHBg3uzfb...">
-----END RSA PRIVATE KEY-----

Is there some way i can verify tha exported key?



来源:https://stackoverflow.com/questions/47463207/which-format-should-be-ssh-private-key-for-libgit2-libgit2sharp-ssh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!