2

I'm setting up SFTP delivery of files to a server using public/private keys generated with ssh-keygen on mac. The private key I generated looks different from other private keys already in the codebase (it doesn't have headers and it's an "OPENSSH PRIVATE KEY" instead of "RSA PRIVATE KEY".

My java FtpUtil is throwing invalid privatekey error.

Is this because the key I generated is not correctly formatted in some way? Is there a way to change this private key to use headers?

This is what other private keys in the codebase look like:

-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-128-CBC,E26BE59A622AEDE6A899FE18AD369AA0

<key text>

-----END RSA PRIVATE KEY-----

This is what my private key looks like:

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

<key text>

-----END OPENSSH PRIVATE KEY-----

I'm getting the following exception thrown:

Caused by: com.jcraft.jsch.JSchException: invalid privatekey: [B@5c0f79f0
    at com.jcraft.jsch.KeyPair.load(KeyPair.java:664)
    at com.jcraft.jsch.IdentityFile.newInstance(IdentityFile.java:46)
    at com.jcraft.jsch.JSch.addIdentity(JSch.java:442)
    at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.initJschSession(DefaultSftpSessionFactory.java:410)
    at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.getSession(DefaultSftpSessionFactory.java:385)
    ... 46 more

John Xu
  • 411
  • 1
  • 4
  • 10
  • 2
    Dupe https://stackoverflow.com/questions/55167874/ant-scp-task-jschexception-invalid-privatekey and (partly) https://stackoverflow.com/questions/53134212/invalid-privatekey-when-using-jgit-and-jsch – dave_thompson_085 Apr 17 '19 at 23:26

1 Answers1

1

It looks like this private key is not correctly formatted indeed. Maybe it would be better to describe how did you generate private key and how is it processed within code?

Anyway if it's possible you can try to generate encrypted RSA private key using following:

openssl genrsa -des3 -out private_key.pem 2048
gebond
  • 30
  • 1
  • 5