“Invalid privatekey” when using JSch

前端 未结 5 747
攒了一身酷
攒了一身酷 2020-11-27 13:25

I\'m using the following code to work with Git in a Java application. I have a valid key (use it all the time), and this specific code has work for me before with the same

5条回答
  •  情歌与酒
    2020-11-27 13:42

    Recent versions of OpenSSH (7.8 and newer) generate keys in new OpenSSH format by default, which start with:

    -----BEGIN OPENSSH PRIVATE KEY-----
    

    JSch does not support this key format.


    You can use ssh-keygen to convert the key to the classic OpenSSH format:

    ssh-keygen -p -f file -m pem -P passphrase -N passphrase
    

    (if the key is not encrypted with a passphrase, use "" instead of passphrase)

    For Windows users: Note that ssh-keygen.exe is now built-in in Windows 10. And can be downloaded from Microsoft Win32-OpenSSH project for older versions of Windows.


    On Windows, you can also use PuTTYgen (from PuTTY package):

    • Start PuTTYgen
    • Load the key
    • Go to Conversions > Export OpenSSH key.
      For RSA keys, it will use the classic format.

    If you are creating a new key with ssh-keygen, just add -m PEM to generate the new key in the classic format:

    ssh-keygen -m PEM
    

提交回复
热议问题